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>

source

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.

source

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];
source

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>

source§

fn clone(&self) -> Generative<'lt>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'lt> Copy for Generative<'lt>

source§

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

Auto Trait Implementations§

§

impl<'lt> RefUnwindSafe for Generative<'lt>

§

impl<'lt> Send for Generative<'lt>

§

impl<'lt> Sync for Generative<'lt>

§

impl<'lt> Unpin for Generative<'lt>

§

impl<'lt> UnwindSafe for Generative<'lt>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.