Skip to content

Commit

Permalink
TryPush depends on Push
Browse files Browse the repository at this point in the history
Signed-off-by: Moritz Hoffmann <[email protected]>
  • Loading branch information
antiguru committed Jul 3, 2024
1 parent 2387429 commit 7d13d6c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 35 deletions.
33 changes: 0 additions & 33 deletions src/impls/slice_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,6 @@ where
+ for<'a> PushStorage<PushIter<[T; N]>>
+ std::ops::Index<std::ops::Range<usize>, Output = [T]>,
{
#[inline]
fn try_push(&mut self, item: [T; N]) -> Result<<OwnedRegion<T> as Region>::Index, [T; N]> {
if self.can_push(&item) {
let start = self.slices.len();
self.slices.push_storage(PushIter(item));
Ok((start, self.slices.len()))
} else {
Err(item)
}
}

#[inline]
fn can_push(&self, item: &[T; N]) -> bool {
self.slices.capacity() - self.slices.len() >= item.len()
Expand All @@ -172,14 +161,6 @@ where
+ for<'a> PushStorage<&'a [T]>
+ std::ops::Index<std::ops::Range<usize>, Output = [T]>,
{
#[inline]
fn try_push<'a>(
&mut self,
item: &'a [T; N],
) -> Result<<OwnedRegion<T> as Region>::Index, &'a [T; N]> {
self.try_push(item.as_slice()).map_err(|_| item)
}

#[inline]
fn can_push(&self, item: &&[T; N]) -> bool {
self.can_push(&item.as_slice())
Expand Down Expand Up @@ -235,20 +216,6 @@ where
+ for<'a> PushStorage<&'a [T]>
+ std::ops::Index<std::ops::Range<usize>, Output = [T]>,
{
#[inline]
fn try_push<'a>(
&mut self,
item: &'a [T],
) -> Result<<OwnedRegion<T, S> as Region>::Index, &'a [T]> {
if self.can_push(&item) {
let start = self.slices.len();
self.slices.push_storage(item);
Ok((start, self.slices.len()))
} else {
Err(item)
}
}

#[inline]
fn can_push(&self, item: &&[T]) -> bool {
self.slices.capacity() - self.slices.len() >= item.len()
Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,17 @@ pub trait Push<T>: Region {
}

/// Push an item `T` into a region.
pub trait TryPush<T>: Region {
pub trait TryPush<T>: Push<T> {
/// Push `item` into self, returning an index that allows to look up the
/// corresponding read item.
fn try_push(&mut self, item: T) -> Result<Self::Index, T>;
#[inline]
fn try_push(&mut self, item: T) -> Result<Self::Index, T> {
if self.can_push(&item) {
Ok(self.push(item))
} else {
Err(item)
}
}

/// Test if an item can be pushed into the region without reallocation.
#[must_use]
Expand Down

0 comments on commit 7d13d6c

Please sign in to comment.