Skip to content

Commit

Permalink
IntoOwned takes self by ref
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed May 28, 2024
1 parent 6e5f732 commit 1c4ee3f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 30 deletions.
10 changes: 6 additions & 4 deletions src/impls/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,16 +267,18 @@ where
{
type Owned = Vec<R::Owned>;

fn into_owned(self) -> Self::Owned {
self.iter().map(IntoOwned::into_owned).collect()
fn into_owned(&self) -> Self::Owned {
self.iter()
.map(|item| IntoOwned::into_owned(&item))
.collect()
}

fn clone_onto(self, other: &mut Self::Owned) {
fn clone_onto(&self, other: &mut Self::Owned) {
let r = std::cmp::min(self.len(), other.len());
for (item, target) in self.iter().zip(other.iter_mut()) {
item.clone_onto(target);
}
other.extend(self.iter().skip(r).map(IntoOwned::into_owned));
other.extend(self.iter().skip(r).map(|item| IntoOwned::into_owned(&item)));
other.truncate(self.len());
}

Expand Down
8 changes: 4 additions & 4 deletions src/impls/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ macro_rules! implement_for {
impl<'a> IntoOwned<'a> for $index_type {
type Owned = $index_type;

fn into_owned(self) -> Self::Owned {
self
fn into_owned(&self) -> Self::Owned {
*self
}

fn clone_onto(self, other: &mut Self::Owned) {
*other = self;
fn clone_onto(&self, other: &mut Self::Owned) {
*other = *self;
}

fn borrow_as(owned: &'a Self::Owned) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions src/impls/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ where
{
type Owned = Option<T::Owned>;

fn into_owned(self) -> Self::Owned {
self.map(IntoOwned::into_owned)
fn into_owned(&self) -> Self::Owned {
self.as_ref().map(IntoOwned::into_owned)
}

fn clone_onto(self, other: &mut Self::Owned) {
fn clone_onto(&self, other: &mut Self::Owned) {
match (self, other) {
(Some(item), Some(target)) => T::clone_onto(item, target),
(Some(item), target) => *target = Some(T::into_owned(item)),
Expand Down
6 changes: 3 additions & 3 deletions src/impls/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ where
{
type Owned = Result<T::Owned, E::Owned>;

fn into_owned(self) -> Self::Owned {
self.map(T::into_owned).map_err(E::into_owned)
fn into_owned(&self) -> Self::Owned {
self.as_ref().map(T::into_owned).map_err(E::into_owned)
}

fn clone_onto(self, other: &mut Self::Owned) {
fn clone_onto(&self, other: &mut Self::Owned) {
match (self, other) {
(Ok(item), Ok(target)) => T::clone_onto(item, target),
(Err(item), Err(target)) => E::clone_onto(item, target),
Expand Down
10 changes: 6 additions & 4 deletions src/impls/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,18 @@ where
{
type Owned = Vec<C::Owned>;

fn into_owned(self) -> Self::Owned {
self.iter().map(IntoOwned::into_owned).collect()
fn into_owned(&self) -> Self::Owned {
self.iter()
.map(|item| IntoOwned::into_owned(&item))
.collect()
}

fn clone_onto(self, other: &mut Self::Owned) {
fn clone_onto(&self, other: &mut Self::Owned) {
let r = std::cmp::min(self.len(), other.len());
for (item, target) in self.iter().zip(other.iter_mut()) {
item.clone_onto(target);
}
other.extend(self.iter().skip(r).map(IntoOwned::into_owned));
other.extend(self.iter().skip(r).map(|item| IntoOwned::into_owned(&item)));
other.truncate(self.len());
}

Expand Down
4 changes: 2 additions & 2 deletions src/impls/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ macro_rules! tuple_flatcontainer {
{
type Owned = ($($name::Owned,)*);

fn into_owned(self) -> Self::Owned {
fn into_owned(&self) -> Self::Owned {
let ($($name,)*) = self;
(
$($name.into_owned(),)*
)
}

fn clone_onto(self, other: &mut Self::Owned) {
fn clone_onto(&self, other: &mut Self::Owned) {
let ($($name,)*) = self;
let ($([<$name _other>],)*) = other;
$($name.clone_onto([<$name _other>]);)*
Expand Down
22 changes: 12 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,22 @@ pub trait IntoOwned<'a> {
/// Owned type into which this type can be converted.
type Owned;
/// Conversion from an instance of this type to the owned type.
fn into_owned(self) -> Self::Owned;
fn into_owned(&self) -> Self::Owned;

Check failure on line 116 in src/lib.rs

View workflow job for this annotation

GitHub Actions / cargo test on macos, rust stable

methods called `into_*` usually take `self` by value
/// Clones `self` onto an existing instance of the owned type.
fn clone_onto(self, other: &mut Self::Owned);
fn clone_onto(&self, other: &mut Self::Owned);
/// Borrows an owned instance as oneself.
fn borrow_as(owned: &'a Self::Owned) -> Self;
}

impl<'a, T: ToOwned + ?Sized> IntoOwned<'a> for &'a T {
impl<'a, T> IntoOwned<'a> for &'a T
where
T: ToOwned + ?Sized,
{
type Owned = T::Owned;
fn into_owned(self) -> Self::Owned {
self.to_owned()
fn into_owned(&self) -> Self::Owned {
(*self).to_owned()
}
fn clone_onto(self, other: &mut Self::Owned) {
fn clone_onto(&self, other: &mut Self::Owned) {
<T as ToOwned>::clone_into(self, other)
}
fn borrow_as(owned: &'a Self::Owned) -> Self {
Expand Down Expand Up @@ -435,15 +438,15 @@ mod tests {
impl<'a> IntoOwned<'a> for PersonRef<'a> {
type Owned = Person;

fn into_owned(self) -> Self::Owned {
fn into_owned(&self) -> Self::Owned {
Person {
name: self.name.into_owned(),
age: self.age,
hobbies: self.hobbies.into_owned(),
}
}

fn clone_onto(self, other: &mut Self::Owned) {
fn clone_onto(&self, other: &mut Self::Owned) {
self.name.clone_onto(&mut other.name);
other.age = self.age;
self.hobbies.clone_onto(&mut other.hobbies);
Expand Down Expand Up @@ -766,8 +769,7 @@ mod tests {
for<'a> R: Region + Push<<<R as Region>::ReadItem<'a> as IntoOwned<'a>>::Owned>,
for<'a> R::ReadItem<'a>: IntoOwned<'a, Owned = O> + Eq + Debug,
{
let item = region.index(index);
let owned = item.into_owned();
let owned = region.index(index).into_owned();
let index2 = region.push(owned);
let item = region.index(index);
assert_eq!(item, region.index(index2));
Expand Down

0 comments on commit 1c4ee3f

Please sign in to comment.