From b94ec0fc81d91d78244fdd2dfd6329c9ea9ee217 Mon Sep 17 00:00:00 2001 From: Moritz Hoffmann Date: Mon, 27 May 2024 14:17:29 -0400 Subject: [PATCH] Remove older experiments Signed-off-by: Moritz Hoffmann --- src/impls/columns.rs | 1 + src/impls/option.rs | 16 +--------- src/impls/result.rs | 21 +----------- src/impls/slice.rs | 24 ++------------ src/impls/slice_copy.rs | 23 +------------ src/impls/string.rs | 15 ++------- src/lib.rs | 71 ++++------------------------------------- 7 files changed, 16 insertions(+), 155 deletions(-) diff --git a/src/impls/columns.rs b/src/impls/columns.rs index 2b17013..bf6d43d 100644 --- a/src/impls/columns.rs +++ b/src/impls/columns.rs @@ -277,6 +277,7 @@ where item.clone_onto(target); } other.extend(self.iter().skip(r).map(IntoOwned::into_owned)); + other.truncate(self.len()); } fn borrow_as(owned: &'a Self::Owned) -> Self { diff --git a/src/impls/option.rs b/src/impls/option.rs index b1d893a..04599e3 100644 --- a/src/impls/option.rs +++ b/src/impls/option.rs @@ -3,7 +3,7 @@ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use crate::{Containerized, IntoOwned, OpinionatedRegion, Push, Region, ReserveItems}; +use crate::{Containerized, IntoOwned, Push, Region, ReserveItems}; impl Containerized for Option { type Region = OptionRegion; @@ -76,20 +76,6 @@ impl Region for OptionRegion { } } -impl OpinionatedRegion for OptionRegion { - fn item_to_owned(item: Self::ReadItem<'_>) -> Self::Owned { - item.map(R::item_to_owned) - } - - fn item_to_owned_into(item: Self::ReadItem<'_>, target: &mut Self::Owned) { - match (item, target) { - (Some(item), Some(target)) => R::item_to_owned_into(item, target), - (Some(item), target) => *target = Some(R::item_to_owned(item)), - (None, target) => *target = None, - } - } -} - impl<'a, T> IntoOwned<'a> for Option where T: IntoOwned<'a>, diff --git a/src/impls/result.rs b/src/impls/result.rs index 3981026..755e147 100644 --- a/src/impls/result.rs +++ b/src/impls/result.rs @@ -3,7 +3,7 @@ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use crate::{Containerized, IntoOwned, OpinionatedRegion, Push, Region, ReserveItems}; +use crate::{Containerized, IntoOwned, Push, Region, ReserveItems}; impl Containerized for Result { type Region = ResultRegion; @@ -113,25 +113,6 @@ where } } -impl OpinionatedRegion for ResultRegion -where - T: OpinionatedRegion, - E: OpinionatedRegion, -{ - fn item_to_owned(item: Self::ReadItem<'_>) -> Self::Owned { - item.map(T::item_to_owned).map_err(E::item_to_owned) - } - - fn item_to_owned_into(item: Self::ReadItem<'_>, target: &mut Self::Owned) { - match (item, target) { - (Ok(item), Ok(target)) => T::item_to_owned_into(item, target), - (Err(item), Err(target)) => E::item_to_owned_into(item, target), - (Ok(item), target) => *target = Ok(T::item_to_owned(item)), - (Err(item), target) => *target = Err(E::item_to_owned(item)), - } - } -} - impl Push> for ResultRegion where TC: Region + Push, diff --git a/src/impls/slice.rs b/src/impls/slice.rs index 27e92fb..4759da7 100644 --- a/src/impls/slice.rs +++ b/src/impls/slice.rs @@ -7,7 +7,7 @@ use std::ops::{Deref, Range}; use serde::{Deserialize, Serialize}; use crate::impls::offsets::OffsetContainer; -use crate::{Containerized, IntoOwned, OpinionatedRegion, Push, ReadToOwned, Region, ReserveItems}; +use crate::{Containerized, IntoOwned, Push, Region, ReserveItems}; impl Containerized for Vec { type Region = SliceRegion; @@ -113,27 +113,6 @@ impl> Region for SliceRegion { } } -impl> OpinionatedRegion for SliceRegion -where - for<'a> ::ReadItem<'a>: ReadToOwned, -{ - fn item_to_owned(item: Self::ReadItem<'_>) -> Self::Owned { - item.iter().map(ReadToOwned::read_to_owned).collect() - } - fn item_to_owned_into(item: Self::ReadItem<'_>, target: &mut Self::Owned) { - let mut valid = 0; - for (index, element) in item.iter().enumerate() { - if target.len() > index { - C::item_to_owned_into(element, &mut target[index]); - } else { - target.push(C::item_to_owned(element)); - } - valid += 1; - } - target.truncate(valid); - } -} - impl> Default for SliceRegion { fn default() -> Self { Self { @@ -272,6 +251,7 @@ where item.clone_onto(target); } other.extend(self.iter().skip(r).map(IntoOwned::into_owned)); + other.truncate(self.len()); } fn borrow_as(owned: &'a Self::Owned) -> Self { diff --git a/src/impls/slice_copy.rs b/src/impls/slice_copy.rs index ee7618f..40f9421 100644 --- a/src/impls/slice_copy.rs +++ b/src/impls/slice_copy.rs @@ -3,7 +3,7 @@ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; -use crate::{CopyIter, OpinionatedRegion, Push, Region, ReserveItems}; +use crate::{CopyIter, Push, Region, ReserveItems}; /// A container for owned types. /// @@ -84,27 +84,6 @@ where } } -impl OpinionatedRegion for OwnedRegion -where - [T]: ToOwned>, -{ - fn item_to_owned(item: Self::ReadItem<'_>) -> Self::Owned { - item.to_vec() - } - fn item_to_owned_into(item: Self::ReadItem<'_>, target: &mut Self::Owned) { - let mut valid = 0; - for (index, element) in item.iter().enumerate() { - if target.len() > index { - target[index].clone_from(element); - } else { - target.push(element.clone()); - } - valid += 1; - } - target.truncate(valid); - } -} - impl Default for OwnedRegion { fn default() -> Self { Self { diff --git a/src/impls/string.rs b/src/impls/string.rs index da3b9d9..c37d499 100644 --- a/src/impls/string.rs +++ b/src/impls/string.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::impls::slice_copy::OwnedRegion; -use crate::{Containerized, OpinionatedRegion, Push, Region, ReserveItems}; +use crate::{Containerized, Push, Region, ReserveItems}; /// A region to store strings and read `&str`. /// @@ -87,15 +87,6 @@ where } } -impl OpinionatedRegion for StringRegion { - fn item_to_owned(item: Self::ReadItem<'_>) -> Self::Owned { - item.to_string() - } - fn item_to_owned_into(item: Self::ReadItem<'_>, target: &mut Self::Owned) { - ::clone_into(item, target); - } -} - impl Containerized for String { type Region = StringRegion; } @@ -184,7 +175,7 @@ where #[cfg(test)] mod tests { - use crate::{Push, ReadToOwned, Region, ReserveItems, StringRegion}; + use crate::{IntoOwned, Push, Region, ReserveItems, StringRegion}; #[test] fn test_inner() { @@ -244,7 +235,7 @@ mod tests { let idx = r.push("abc"); let reference = r.index(idx); - let owned = reference.read_to_owned(); + let owned = reference.into_owned(); let idx = r.push(owned); assert_eq!("abc", r.index(idx)); } diff --git a/src/lib.rs b/src/lib.rs index d28d46c..a0aceec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -41,7 +41,7 @@ impl Index for T {} /// /// Implement the [`Push`] trait for all types that can be copied into a region. pub trait Region: Default { - /// TODO + /// An owned type that can be constructed from a read item. type Owned; /// The type of the data that one gets out of the container. @@ -355,45 +355,6 @@ impl Clone for FlatStack { #[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)] pub struct CopyIter(pub I); -/// Conversion of references to owned data. Similar to [`ToOwned`], but without the requirement -/// that target can be borrowed to self. -/// -/// The clumsy names originate from `to_owned` already being in scope. -pub trait ReadToOwned { - /// The owned type. Static lifetime to indicate that the lifetime of the owned object must not - /// depend on self. - type Owned; - /// Convert self into an owned representation. - fn read_to_owned(self) -> Self::Owned; - /// Convert self into an owned representation, re-using an existing allocation. - fn read_to_owned_into(&self, target: &mut Self::Owned); -} - -/// TODO -pub trait OpinionatedRegion: Region { - //where for<'a> ::ReadItem<'a>: ReadToOwned { - - /// TODO - fn item_to_owned(item: Self::ReadItem<'_>) -> Self::Owned; - /// TODO - fn item_to_owned_into(item: Self::ReadItem<'_>, target: &mut Self::Owned); -} - -impl ReadToOwned for &T -where - T::Owned: 'static, -{ - type Owned = T::Owned; - - fn read_to_owned(self) -> Self::Owned { - self.to_owned() - } - - fn read_to_owned_into(&self, target: &mut Self::Owned) { - ::clone_into(self, target); - } -} - #[cfg(test)] mod tests { use crate::impls::deduplicate::{CollapseSequence, ConsecutiveOffsetPairs}; @@ -497,26 +458,6 @@ mod tests { } } - impl ReadToOwned for PersonRef<'_> { - type Owned = Person; - fn read_to_owned(self) -> Person { - Person { - name: self.name.to_string(), - age: self.age, - hobbies: self.hobbies.iter().map(|s| s.to_string()).collect(), - } - } - fn read_to_owned_into(&self, target: &mut Person) { - target.name.clear(); - target.name.push_str(self.name); - target.age = self.age; - target.hobbies.clear(); - target - .hobbies - .extend(self.hobbies.iter().map(str::to_string)); - } - } - impl Region for PersonRegion { type Owned = Person; type ReadItem<'a> = PersonRef<'a> where Self: 'a; @@ -820,11 +761,13 @@ mod tests { fn owned_roundtrip(region: &mut R, index: R::Index) where - for<'a> R: Region + Push<<::ReadItem<'a> as ReadToOwned>::Owned>, - for<'a> R::ReadItem<'a>: ReadToOwned, + for<'a> R: Region + Push<<::ReadItem<'a> as IntoOwned<'a>>::Owned>, + for<'a> R::ReadItem<'a>: IntoOwned<'a, Owned = O>, { - let item = region.index(index).read_to_owned(); - region.push(item); + let item = region.index(index); + let owned = item.into_owned(); + let index = region.push(owned); + assert_eq!(item, region.index(index)); } #[test]