Struct index_ext::tag::Generative
source · pub struct Generative<'lt> { /* private fields */ }
Expand description
A generative lifetime.
This is a simple implementor of Tag
that allows a local but entirely safe and macro-free
use of check indices. The compiler introduces new lifetimes and the design of these types
ensure that no other object with the same can be created.
Implementations§
source§impl<'lt> Generative<'lt>
impl<'lt> Generative<'lt>
sourcepub fn with_len(len: usize, token: Guard<'lt>) -> ExactSize<Self>
pub fn with_len(len: usize, token: Guard<'lt>) -> ExactSize<Self>
Construct a size with a generative guard and explicit length.
The Guard
instance is a token that verifies that no other instance with that particular
lifetime exists. It is thus not possible to safely construct a second ExactSize
with the
same tag but a different length. This uniquely ties the value len
to that lifetime.
sourcepub fn with_ref<'slice, T>(
slice: &'slice [T],
token: Guard<'lt>
) -> (&'slice Slice<T, Self>, ExactSize<Self>)
pub fn with_ref<'slice, T>( slice: &'slice [T], token: Guard<'lt> ) -> (&'slice Slice<T, Self>, ExactSize<Self>)
Consume a generativity token to associated a lifetime with the slice’s length.
This isn’t fundamentally different from using Self::with_len
and Slice::new
, and
you might want to see those documentations, but it clarifies that this combination is
infallible.
This essentially shares the generative uniqueness of the lifetime among all values relying on the length predicate of the same slice.
Usage
This allows you to do the same as with_ref
but ad-hoc within a function body without
introducing any new scope.
use generativity::make_guard;
use index_ext::tag::Generative;
let data = (0..117).collect::<Vec<_>>();
make_guard!(guard);
let (slice, size) = Generative::with_ref(&data, guard);
let index = size.into_len().range_to_self();
// … Later, no bounds check here.
let data = &slice[index];
sourcepub fn with_mut<'slice, T>(
slice: &'slice mut [T],
token: Guard<'lt>
) -> (&'slice mut Slice<T, Self>, ExactSize<Self>)
pub fn with_mut<'slice, T>( slice: &'slice mut [T], token: Guard<'lt> ) -> (&'slice mut Slice<T, Self>, ExactSize<Self>)
Consume a generativity token to associated a lifetime with the mutable slice’s length.
This isn’t fundamentally different from using Self::with_len
and Slice::new_mut
,
and you might want to see those documentations, but it clarifies that this combination is
infallible.
This essentially shares the generative uniqueness of the lifetime among all values relying on the length predicate of the same slice.
Usage
This allows you to do the same as with_mut
but ad-hoc within a function body without
introducing any new scope.
use generativity::make_guard;
use index_ext::tag::Generative;
let mut data = (0..117).collect::<Vec<_>>();
make_guard!(guard);
let (mut slice, size) = Generative::with_mut(&mut data, guard);
let index = size.into_len().range_to_self();
// … Later, no bounds check here.
let data = &mut slice[index];
Trait Implementations§
source§impl<'lt> Clone for Generative<'lt>
impl<'lt> Clone for Generative<'lt>
source§fn clone(&self) -> Generative<'lt>
fn clone(&self) -> Generative<'lt>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreimpl<'lt> Copy for Generative<'lt>
impl Tag for Generative<'_>
SAFETY: This is invariant over the lifetime. There are no other coercions. See https://doc.rust-lang.org/nomicon/coercions.html#coercions