Skip to content

Commit

Permalink
Introduce reborrow to enable lifetime variance
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 1b1bc72 commit 3885d89
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/impls/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ where
self.inner.heap_size(&mut callback);
self.codec.heap_size(callback);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
C: 'a,
{
item
}
}

impl<C: Codec, R> Push<&[u8]> for CodecRegion<C, R>
Expand Down
7 changes: 7 additions & 0 deletions src/impls/columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ where
}
self.indices.heap_size(callback);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
item
}
}

impl<R> Default for ColumnsRegion<R>
Expand Down
20 changes: 18 additions & 2 deletions src/impls/deduplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ impl<R: Region> Region for CollapseSequence<R> {
fn heap_size<F: FnMut(usize, usize)>(&self, callback: F) {
self.inner.heap_size(callback);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
R::reborrow(item)
}
}

impl<R, T> Push<T> for CollapseSequence<R>
Expand Down Expand Up @@ -135,8 +142,10 @@ impl<R: Region<Index = (usize, usize)>, O: OffsetContainer<usize>> Default
}
}

impl<R: Region<Index = (usize, usize)>, O: OffsetContainer<usize>> Region
for ConsecutiveOffsetPairs<R, O>
impl<R, O> Region for ConsecutiveOffsetPairs<R, O>
where
R: Region<Index = (usize, usize)>,
O: OffsetContainer<usize>,
{
type ReadItem<'a> = R::ReadItem<'a>
where
Expand Down Expand Up @@ -181,6 +190,13 @@ impl<R: Region<Index = (usize, usize)>, O: OffsetContainer<usize>> Region
self.offsets.heap_size(&mut callback);
self.inner.heap_size(callback);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
R::reborrow(item)
}
}

impl<R, O, T> Push<T> for ConsecutiveOffsetPairs<R, O>
Expand Down
7 changes: 7 additions & 0 deletions src/impls/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ impl<T: Index> Region for MirrorRegion<T> {
fn heap_size<F: FnMut(usize, usize)>(&self, _callback: F) {
// No storage
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
item
}
}

impl<T: Index> Push<T> for MirrorRegion<T> {
Expand Down
7 changes: 7 additions & 0 deletions src/impls/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ impl<R: Region> Region for OptionRegion<R> {
fn heap_size<F: FnMut(usize, usize)>(&self, callback: F) {
self.inner.heap_size(callback);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
item.map(R::reborrow)
}
}

impl<T, TR> Push<Option<T>> for OptionRegion<TR>
Expand Down
7 changes: 7 additions & 0 deletions src/impls/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ where
self.oks.heap_size(&mut callback);
self.errs.heap_size(callback);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
item.map(T::reborrow).map_err(E::reborrow)
}
}

impl<T, TC, E, EC> Push<Result<T, E>> for ResultRegion<TC, EC>
Expand Down
7 changes: 7 additions & 0 deletions src/impls/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ impl<C: Region, O: OffsetContainer<C::Index>> Region for SliceRegion<C, O> {
self.slices.heap_size(&mut callback);
self.inner.heap_size(callback);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
item
}
}

impl<C: Region, O: OffsetContainer<C::Index>> Default for SliceRegion<C, O> {
Expand Down
7 changes: 7 additions & 0 deletions src/impls/slice_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ impl<T> Region for OwnedRegion<T> {
self.slices.capacity() * size_of_t,
);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
item
}
}

impl<T> Default for OwnedRegion<T> {
Expand Down
7 changes: 7 additions & 0 deletions src/impls/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ where
fn heap_size<F: FnMut(usize, usize)>(&self, callback: F) {
self.inner.heap_size(callback);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
item
}
}

impl Containerized for String {
Expand Down
7 changes: 7 additions & 0 deletions src/impls/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ macro_rules! tuple_flatcontainer {
fn heap_size<Fn: FnMut(usize, usize)>(&self, mut callback: Fn) {
$(self.[<container $name>].heap_size(&mut callback);)*
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b> where Self: 'a {
let ($($name,)*) = item;
(
$($name::reborrow($name),)*
)
}
}

#[allow(non_camel_case_types)]
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ pub trait Region: Default {

/// Heap size, size - capacity
fn heap_size<F: FnMut(usize, usize)>(&self, callback: F);

/// Converts a read item into one with a narrower lifetime.
fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a;
}

/// A trait to let types express a default container type.
Expand Down Expand Up @@ -450,6 +455,17 @@ mod tests {
self.age_container.heap_size(&mut callback);
self.hobbies.heap_size(callback);
}

fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>
where
Self: 'a,
{
PersonRef {
name: <String as Containerized>::Region::reborrow(item.name),
age: <u16 as Containerized>::Region::reborrow(item.age),
hobbies: <Vec<String> as Containerized>::Region::reborrow(item.hobbies),
}
}
}

impl Push<&Person> for PersonRegion {
Expand Down

0 comments on commit 3885d89

Please sign in to comment.