Skip to content

Commit

Permalink
Remove reserve_items from CopyOnto
Browse files Browse the repository at this point in the history
It could only be implemented for a subset of `CopyOnto` implementations,
namely those defined on references. The `ReserveItems` trait provides a
better alternative.

Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed Feb 4, 2024
1 parent 0869fec commit 0decbff
Show file tree
Hide file tree
Showing 7 changed files with 0 additions and 124 deletions.
14 changes: 0 additions & 14 deletions src/impls/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,13 @@ macro_rules! implement_for {
fn copy_onto(self, _target: &mut MirrorRegion<Self>) -> $index_type {
self
}

#[inline(always)]
fn reserve_items<I>(_target: &mut MirrorRegion<Self>, _items: I)
where
I: Iterator<Item = Self> + Clone,
{
}
}

impl<'a> CopyOnto<MirrorRegion<$index_type>> for &'a $index_type {
#[inline(always)]
fn copy_onto(self, _target: &mut MirrorRegion<$index_type>) -> $index_type {
*self
}

#[inline(always)]
fn reserve_items<I>(_target: &mut MirrorRegion<$index_type>, _items: I)
where
I: Iterator<Item = Self> + Clone,
{
}
}

impl<'a> ReserveItems<MirrorRegion<$index_type>> for $index_type {
Expand Down
19 changes: 0 additions & 19 deletions src/impls/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ where
Err(e) => Err(e.copy_onto(&mut target.errs)),
}
}

fn reserve_items<I>(_target: &mut ResultRegion<TC, EC>, _items: I)
where
I: Iterator<Item = Self> + Clone,
{
// CopyOnto::reserve_items(&mut target.oks, items.clone().flat_map(|r| r.ok()));
// CopyOnto::reserve_items(&mut target.errs, items.flat_map(|r| r.err()));
}
}

impl<'a, T: 'a, TC, E: 'a, EC> CopyOnto<ResultRegion<TC, EC>> for &'a Result<T, E>
Expand All @@ -91,17 +83,6 @@ where
Err(e) => Err(e.copy_onto(&mut target.errs)),
}
}

fn reserve_items<I>(target: &mut ResultRegion<TC, EC>, items: I)
where
I: Iterator<Item = Self> + Clone,
{
CopyOnto::reserve_items(&mut target.oks, items.clone().flat_map(|r| r.as_ref().ok()));
CopyOnto::reserve_items(
&mut target.errs,
items.clone().flat_map(|r| r.as_ref().err()),
);
}
}

impl<'a, T: 'a, TC, E: 'a, EC> ReserveItems<ResultRegion<TC, EC>> for &'a Result<T, E>
Expand Down
35 changes: 0 additions & 35 deletions src/impls/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,6 @@ where
.extend(self.iter().map(|t| t.copy_onto(&mut target.inner)));
(start, target.slices.len())
}

fn reserve_items<I>(target: &mut SliceRegion<C>, items: I)
where
I: Iterator<Item = Self> + Clone,
{
target.slices.reserve(items.clone().map(|i| i.len()).sum());
CopyOnto::reserve_items(&mut target.inner, items.flat_map(|i| i.iter()));
}
}

impl<'a, T, R: Region> ReserveItems<SliceRegion<R>> for &'a [T]
Expand All @@ -97,13 +89,6 @@ where
fn copy_onto(self, target: &mut SliceRegion<C>) -> <SliceRegion<C> as Region>::Index {
self.as_slice().copy_onto(target)
}

fn reserve_items<I>(target: &mut SliceRegion<C>, items: I)
where
I: Iterator<Item = Self> + Clone,
{
CopyOnto::reserve_items(target, items.map(Deref::deref))
}
}

impl<'a, T: 'a, R: Region> ReserveItems<SliceRegion<R>> for &'a Vec<T>
Expand All @@ -127,13 +112,6 @@ where
fn copy_onto(self, target: &mut SliceRegion<C>) -> <SliceRegion<C> as Region>::Index {
self.as_slice().copy_onto(target)
}

fn reserve_items<I>(_target: &mut SliceRegion<C>, _items: I)
where
I: Iterator<Item = Self> + Clone,
{
//CopyOnto::reserve_items(target, items.map(Deref::deref))
}
}

impl<'a, C: Region + 'a> CopyOnto<SliceRegion<C>> for &'a (&'a C, &'a [C::Index])
Expand All @@ -151,19 +129,6 @@ where
);
(start, target.slices.len())
}

fn reserve_items<I>(target: &mut SliceRegion<C>, items: I)
where
I: Iterator<Item = Self> + Clone,
{
target
.slices
.reserve(items.clone().map(|(_c, is)| is.len()).sum());
CopyOnto::reserve_items(
&mut target.inner,
items.flat_map(|(c, is)| is.iter().map(|i| c.index(*i))),
)
}
}

impl<'a, C: Region + 'a> ReserveItems<SliceRegion<C>> for &'a (C, &'a [C::Index])
Expand Down
7 changes: 0 additions & 7 deletions src/impls/slice_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ where
target.slices.extend(self);
(start, target.slices.len())
}

fn reserve_items<I>(target: &mut CopyRegion<T>, items: I)
where
I: Iterator<Item = Self> + Clone,
{
target.slices.reserve(items.clone().map(|i| i.len()).sum());
}
}

impl<T: Copy> ReserveItems<CopyRegion<T>> for &[T] {
Expand Down
21 changes: 0 additions & 21 deletions src/impls/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ impl CopyOnto<StringRegion> for &String {
fn copy_onto(self, target: &mut StringRegion) -> <StringRegion as Region>::Index {
self.as_str().copy_onto(target)
}

fn reserve_items<I>(target: &mut StringRegion, items: I)
where
I: Iterator<Item = Self> + Clone,
{
CopyOnto::reserve_items(target, items.map(String::as_str));
}
}

impl ReserveItems<StringRegion> for &String {
Expand All @@ -66,27 +59,13 @@ impl CopyOnto<StringRegion> for &str {
fn copy_onto(self, target: &mut StringRegion) -> <StringRegion as Region>::Index {
self.as_bytes().copy_onto(&mut target.inner)
}

fn reserve_items<I>(target: &mut StringRegion, items: I)
where
I: Iterator<Item = Self> + Clone,
{
CopyOnto::reserve_items(&mut target.inner, items.map(str::as_bytes))
}
}

impl CopyOnto<StringRegion> for &&str {
#[inline]
fn copy_onto(self, target: &mut StringRegion) -> <StringRegion as Region>::Index {
self.as_bytes().copy_onto(&mut target.inner)
}

fn reserve_items<I>(target: &mut StringRegion, items: I)
where
I: Iterator<Item = Self> + Clone,
{
CopyOnto::reserve_items(&mut target.inner, items.map(|s| s.as_bytes()))
}
}

impl ReserveItems<StringRegion> for &str {
Expand Down
9 changes: 0 additions & 9 deletions src/impls/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ macro_rules! tuple_flatcontainer {
let ($($name,)*) = self;
($($name.copy_onto(&mut target.[<container $name>]),)*)
}

fn reserve_items<It>(_target: &mut [<Tuple $($name)* Region>]<$([<$name _C>]),*>, _items: It)
where
It: Iterator<Item = Self> + Clone {
}
}

#[allow(non_camel_case_types)]
Expand All @@ -84,10 +79,6 @@ macro_rules! tuple_flatcontainer {
let ($($name,)*) = self;
($($name.copy_onto(&mut target.[<container $name>]),)*)
}
fn reserve_items<It>(_target: &mut [<Tuple $($name)* Region>]<$([<$name _C>]),*>, _items: It)
where
It: Iterator<Item = Self> + Clone {
}
}

#[allow(non_camel_case_types)]
Expand Down
19 changes: 0 additions & 19 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ pub trait CopyOnto<C: Region> {
/// Copy self into the target container, returning an index that allows to
/// look up the corresponding read item.
fn copy_onto(self, target: &mut C) -> C::Index;

/// Ensure that the region can absorb `items` without reallocation.
fn reserve_items<I>(target: &mut C, items: I)
where
I: Iterator<Item = Self> + Clone;
}

pub trait ReserveItems<R: Region> {
Expand Down Expand Up @@ -258,13 +253,6 @@ mod tests {
let hobbies = (&self.hobbies).copy_onto(&mut target.hobbies);
(name, age, hobbies)
}

fn reserve_items<I>(_target: &mut PersonRegion, _items: I)
where
I: Iterator<Item = Self> + Clone,
{
todo!()
}
}

impl<'a> ReserveItems<PersonRegion> for &'a Person {
Expand All @@ -285,13 +273,6 @@ mod tests {
let hobbies = self.hobbies.copy_onto(&mut target.hobbies);
(name, age, hobbies)
}

fn reserve_items<I>(_target: &mut PersonRegion, _items: I)
where
I: Iterator<Item = Self> + Clone,
{
todo!()
}
}

#[test]
Expand Down

0 comments on commit 0decbff

Please sign in to comment.