diff --git a/src/impls/columns.rs b/src/impls/columns.rs index bf6d43d..32a8170 100644 --- a/src/impls/columns.rs +++ b/src/impls/columns.rs @@ -267,16 +267,18 @@ where { type Owned = Vec; - 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()); } diff --git a/src/impls/mirror.rs b/src/impls/mirror.rs index 8ea70f0..ace235d 100644 --- a/src/impls/mirror.rs +++ b/src/impls/mirror.rs @@ -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 { diff --git a/src/impls/option.rs b/src/impls/option.rs index 04599e3..c534305 100644 --- a/src/impls/option.rs +++ b/src/impls/option.rs @@ -82,11 +82,11 @@ where { type Owned = Option; - 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)), diff --git a/src/impls/result.rs b/src/impls/result.rs index 755e147..975c6b3 100644 --- a/src/impls/result.rs +++ b/src/impls/result.rs @@ -95,11 +95,11 @@ where { type Owned = Result; - 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), diff --git a/src/impls/slice.rs b/src/impls/slice.rs index 4759da7..e009ebc 100644 --- a/src/impls/slice.rs +++ b/src/impls/slice.rs @@ -241,16 +241,18 @@ where { type Owned = Vec; - 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()); } diff --git a/src/impls/tuple.rs b/src/impls/tuple.rs index acbe4fb..16febfa 100644 --- a/src/impls/tuple.rs +++ b/src/impls/tuple.rs @@ -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>]);)* diff --git a/src/lib.rs b/src/lib.rs index 3c39097..e4f99de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; /// 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) { ::clone_into(self, other) } fn borrow_as(owned: &'a Self::Owned) -> Self { @@ -435,7 +438,7 @@ 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, @@ -443,7 +446,7 @@ mod tests { } } - 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); @@ -766,8 +769,7 @@ mod tests { for<'a> R: Region + Push<<::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));