Skip to content

Commit

Permalink
More trait implementations
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed Feb 8, 2024
1 parent 30366af commit df7b24f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/impls/slice_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,28 @@ where

impl<T: Copy> ReserveItems<CopyRegion<T>> for &[T] {
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> CopyOnto<CopyRegion<T>> for &Vec<T>
where
I: Iterator<Item = Self> + Clone,
T: Copy,
{
fn copy_onto(self, target: &mut CopyRegion<T>) -> <CopyRegion<T> as Region>::Index {
let start = target.slices.len();
target.slices.extend_from_slice(self);
(start, target.slices.len())
}
}

impl<T: Copy> ReserveItems<CopyRegion<T>> for &Vec<T> {
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());
}
Expand Down
11 changes: 10 additions & 1 deletion src/impls/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,19 @@ impl CopyOnto<StringRegion> for &&str {
}

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

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

0 comments on commit df7b24f

Please sign in to comment.