Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reborrow functions to Container #568

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions container/src/columnation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ mod container {
fn drain(&mut self) -> Self::DrainIter<'_> {
(*self).iter()
}

#[inline(always)]
fn reborrow<'b, 'a: 'b>(item: Self::Item<'a>) -> Self::Item<'b> where Self: 'a {
item
}

#[inline(always)]
fn reborrow_ref<'b, 'a: 'b>(item: Self::ItemRef<'a>) -> Self::ItemRef<'b> where Self: 'a {
item
}
}

impl<T: Columnation + 'static> SizableContainer for TimelyStack<T> {
Expand Down
10 changes: 10 additions & 0 deletions container/src/flatcontainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ impl<R: Region + Clone + 'static> Container for FlatStack<R> {
fn drain<'a>(&'a mut self) -> Self::DrainIter<'a> {
IntoIterator::into_iter(&*self)
}

#[inline(always)]
fn reborrow<'b, 'a: 'b>(item: Self::Item<'a>) -> Self::Item<'b> where Self: 'a {
R::reborrow(item)
}

#[inline(always)]
fn reborrow_ref<'b, 'a: 'b>(item: Self::ItemRef<'a>) -> Self::ItemRef<'b> where Self: 'a {
R::reborrow(item)
}
}

impl<R: Region + Clone + 'static> SizableContainer for FlatStack<R> {
Expand Down
38 changes: 37 additions & 1 deletion container/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ pub trait Container: Default + Clone + 'static {
/// Returns an iterator that drains the contents of this container.
/// Drain leaves the container in an undefined state.
fn drain(&mut self) -> Self::DrainIter<'_>;

/// Reborrow [`Item`](Container::Item) with a shorter lifetime.
fn reborrow<'b, 'a: 'b>(item: Self::Item<'a>) -> Self::Item<'b> where Self: 'a;

/// Reborrow [`ItemRef`](Container::ItemRef) with a shorter lifetime.
fn reborrow_ref<'b, 'a: 'b>(item: Self::ItemRef<'a>) -> Self::ItemRef<'b> where Self: 'a;
}

/// A container that can absorb items of a specific type.
Expand Down Expand Up @@ -90,7 +96,7 @@ pub trait ContainerBuilder: Default + 'static {
type Container: Container;
/// Add an item to a container.
///
/// The restriction to [`SizeableContainer`] only exists so that types
/// The restriction to [`SizableContainer`] only exists so that types
/// relying on [`CapacityContainerBuilder`] only need to constrain their container
/// to [`Container`] instead of [`SizableContainer`], which otherwise would be a pervasive
/// requirement.
Expand Down Expand Up @@ -215,6 +221,16 @@ impl<T: Clone + 'static> Container for Vec<T> {
fn drain(&mut self) -> Self::DrainIter<'_> {
self.drain(..)
}

#[inline(always)]
fn reborrow<'b, 'a: 'b>(item: Self::Item<'a>) -> Self::Item<'b> where Self: 'a {
item
}

#[inline(always)]
fn reborrow_ref<'b, 'a: 'b>(item: Self::ItemRef<'a>) -> Self::ItemRef<'b> where Self: 'a {
item
}
}

impl<T: Clone + 'static> SizableContainer for Vec<T> {
Expand Down Expand Up @@ -291,6 +307,16 @@ mod rc {
fn drain(&mut self) -> Self::DrainIter<'_> {
self.iter()
}

#[inline(always)]
fn reborrow<'b, 'a: 'b>(item: Self::Item<'a>) -> Self::Item<'b> where Self: 'a {
T::reborrow_ref(item)
}

#[inline(always)]
fn reborrow_ref<'b, 'a: 'b>(item: Self::ItemRef<'a>) -> Self::ItemRef<'b> where Self: 'a {
T::reborrow_ref(item)
}
}
}

Expand Down Expand Up @@ -332,6 +358,16 @@ mod arc {
fn drain(&mut self) -> Self::DrainIter<'_> {
self.iter()
}

#[inline(always)]
fn reborrow<'b, 'a: 'b>(item: Self::Item<'a>) -> Self::Item<'b> where Self: 'a {
T::reborrow_ref(item)
}

#[inline(always)]
fn reborrow_ref<'b, 'a: 'b>(item: Self::ItemRef<'a>) -> Self::ItemRef<'b> where Self: 'a {
T::reborrow_ref(item)
}
}
}

Expand Down
Loading