Skip to content

Commit

Permalink
Remove older experiments
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed May 27, 2024
1 parent 055b903 commit b94ec0f
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 155 deletions.
1 change: 1 addition & 0 deletions src/impls/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 1 addition & 15 deletions src/impls/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Containerized> Containerized for Option<T> {
type Region = OptionRegion<T::Region>;
Expand Down Expand Up @@ -76,20 +76,6 @@ impl<R: Region> Region for OptionRegion<R> {
}
}

impl<R: OpinionatedRegion> OpinionatedRegion for OptionRegion<R> {
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<T>
where
T: IntoOwned<'a>,
Expand Down
21 changes: 1 addition & 20 deletions src/impls/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Containerized, E: Containerized> Containerized for Result<T, E> {
type Region = ResultRegion<T::Region, E::Region>;
Expand Down Expand Up @@ -113,25 +113,6 @@ where
}
}

impl<T, E> OpinionatedRegion for ResultRegion<T, E>
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<T, TC, E, EC> Push<Result<T, E>> for ResultRegion<TC, EC>
where
TC: Region + Push<T>,
Expand Down
24 changes: 2 additions & 22 deletions src/impls/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Containerized> Containerized for Vec<T> {
type Region = SliceRegion<T::Region>;
Expand Down Expand Up @@ -113,27 +113,6 @@ impl<C: Region, O: OffsetContainer<C::Index>> Region for SliceRegion<C, O> {
}
}

impl<C: OpinionatedRegion, O: OffsetContainer<C::Index>> OpinionatedRegion for SliceRegion<C, O>
where
for<'a> <C as Region>::ReadItem<'a>: ReadToOwned<Owned = C::Owned>,
{
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<C: Region, O: OffsetContainer<C::Index>> Default for SliceRegion<C, O> {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 1 addition & 22 deletions src/impls/slice_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -84,27 +84,6 @@ where
}
}

impl<T: Clone> OpinionatedRegion for OwnedRegion<T>
where
[T]: ToOwned<Owned = Vec<T>>,
{
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<T> Default for OwnedRegion<T> {
fn default() -> Self {
Self {
Expand Down
15 changes: 3 additions & 12 deletions src/impls/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
///
Expand Down Expand Up @@ -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) {
<str as ToOwned>::clone_into(item, target);
}
}

impl Containerized for String {
type Region = StringRegion;
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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));
}
Expand Down
71 changes: 7 additions & 64 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<T: Copy> 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.
Expand Down Expand Up @@ -355,45 +355,6 @@ impl<R: Region + Clone> Clone for FlatStack<R> {
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd)]
pub struct CopyIter<I>(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> <Self as Region>::ReadItem<'a>: ReadToOwned<Owned=Self::Owned> {

/// TODO
fn item_to_owned(item: Self::ReadItem<'_>) -> Self::Owned;
/// TODO
fn item_to_owned_into(item: Self::ReadItem<'_>, target: &mut Self::Owned);
}

impl<T: ToOwned + ?Sized> 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) {
<T as ToOwned>::clone_into(self, target);
}
}

#[cfg(test)]
mod tests {
use crate::impls::deduplicate::{CollapseSequence, ConsecutiveOffsetPairs};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -820,11 +761,13 @@ mod tests {

fn owned_roundtrip<R, O>(region: &mut R, index: R::Index)
where
for<'a> R: Region + Push<<<R as Region>::ReadItem<'a> as ReadToOwned>::Owned>,
for<'a> R::ReadItem<'a>: ReadToOwned<Owned = O>,
for<'a> R: Region + Push<<<R as Region>::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]
Expand Down

0 comments on commit b94ec0f

Please sign in to comment.