diff --git a/src/impls/codec.rs b/src/impls/codec.rs index 29be462..4d1b5fe 100644 --- a/src/impls/codec.rs +++ b/src/impls/codec.rs @@ -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 Push<&[u8]> for CodecRegion diff --git a/src/impls/columns.rs b/src/impls/columns.rs index be3e40a..2ba536a 100644 --- a/src/impls/columns.rs +++ b/src/impls/columns.rs @@ -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 Default for ColumnsRegion diff --git a/src/impls/deduplicate.rs b/src/impls/deduplicate.rs index 199bb25..eb15128 100644 --- a/src/impls/deduplicate.rs +++ b/src/impls/deduplicate.rs @@ -70,6 +70,13 @@ impl Region for CollapseSequence { fn heap_size(&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 Push for CollapseSequence @@ -135,8 +142,10 @@ impl, O: OffsetContainer> Default } } -impl, O: OffsetContainer> Region - for ConsecutiveOffsetPairs +impl Region for ConsecutiveOffsetPairs +where + R: Region, + O: OffsetContainer, { type ReadItem<'a> = R::ReadItem<'a> where @@ -181,6 +190,13 @@ impl, O: OffsetContainer> 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 Push for ConsecutiveOffsetPairs diff --git a/src/impls/mirror.rs b/src/impls/mirror.rs index a54bcfb..f2c0b86 100644 --- a/src/impls/mirror.rs +++ b/src/impls/mirror.rs @@ -73,6 +73,13 @@ impl Region for MirrorRegion { fn heap_size(&self, _callback: F) { // No storage } + + fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b> + where + Self: 'a, + { + item + } } impl Push for MirrorRegion { diff --git a/src/impls/option.rs b/src/impls/option.rs index 5d7b987..b9bb98f 100644 --- a/src/impls/option.rs +++ b/src/impls/option.rs @@ -66,6 +66,13 @@ impl Region for OptionRegion { fn heap_size(&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 Push> for OptionRegion diff --git a/src/impls/result.rs b/src/impls/result.rs index dd579ae..2379174 100644 --- a/src/impls/result.rs +++ b/src/impls/result.rs @@ -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 Push> for ResultRegion diff --git a/src/impls/slice.rs b/src/impls/slice.rs index 8dab8be..aa18728 100644 --- a/src/impls/slice.rs +++ b/src/impls/slice.rs @@ -103,6 +103,13 @@ impl> Region for SliceRegion { 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> Default for SliceRegion { diff --git a/src/impls/slice_copy.rs b/src/impls/slice_copy.rs index ac54c8e..1636582 100644 --- a/src/impls/slice_copy.rs +++ b/src/impls/slice_copy.rs @@ -71,6 +71,13 @@ impl Region for OwnedRegion { self.slices.capacity() * size_of_t, ); } + + fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b> + where + Self: 'a, + { + item + } } impl Default for OwnedRegion { diff --git a/src/impls/string.rs b/src/impls/string.rs index b6aabb1..79b512c 100644 --- a/src/impls/string.rs +++ b/src/impls/string.rs @@ -77,6 +77,13 @@ where fn heap_size(&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 { diff --git a/src/impls/tuple.rs b/src/impls/tuple.rs index b6d3417..8e7e98f 100644 --- a/src/impls/tuple.rs +++ b/src/impls/tuple.rs @@ -64,6 +64,13 @@ macro_rules! tuple_flatcontainer { fn heap_size(&self, mut callback: Fn) { $(self.[].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)] diff --git a/src/lib.rs b/src/lib.rs index 561b878..85b4053 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,6 +69,11 @@ pub trait Region: Default { /// Heap size, size - capacity fn heap_size(&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. @@ -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: ::Region::reborrow(item.name), + age: ::Region::reborrow(item.age), + hobbies: as Containerized>::Region::reborrow(item.hobbies), + } + } } impl Push<&Person> for PersonRegion {