StackRef

Struct StackRef 

Source
pub struct StackRef<'a, T> { /* private fields */ }
Expand description

Returned by StackFrameAllocator, StackFrameGeneralAllocator, and StackFrameDictAllocator

A wrapper for references to data within one of these allocators. Ensures compile-time safety for the lifetime of these references. StackRefs can only live as long as the current StackFrame regardless if the StackRef points to a piece of data within that frame.

§Safety

Whenever calling get_mut, the caller must ensure that the borrow checker rules are followed. The user can avoid get_mut by only using Allocators where values are wrapped in a type with interior mutability

Implementations§

Source§

impl<'a, T> StackRef<'a, T>

Source

pub fn get(&self) -> &'a T

Grabs an immutable reference to the value StackRef points to

StackRef’s will guarantee that any reference created by a StackRef is valid until the next StackFrame is popped. See also get_mut.

§Examples
 
let stack = StackFrameAllocator::<usize>::new();
let a = stack.push(80085).get();
let b = stack.push(420).get();
let c = stack.push(69).get();
 
assert_eq!(*a, 80085);
assert_eq!(*b, 420);
assert_eq!(*c, 69);
Source

pub fn get_mut(&mut self) -> &'a mut T

Grabs a mutable reference to the value StackRef points to.

For the StackFrameAllocator, only one StackRef for a given value can exist at any given moment, so this is a safe operation, because the borrow checker at compile time can verify that there’s only one mutable reference to the value. The reference is also guaranteed to be valid until the frame the value is in drops.

§Examples
 
let stack = StackFrameAllocator::<usize>::new();
let mut a = stack.push(1).get_mut();
let mut b = stack.push(2).get_mut();
let mut c = stack.push(3).get_mut();
 
assert_eq!(*a, 1);
assert_eq!(*b, 2);
assert_eq!(*c, 3);
 
*a = 80085;
*b = 420;
*c = 69;
 
assert_eq!(*a, 80085);
assert_eq!(*b, 420);
assert_eq!(*c, 69);

Auto Trait Implementations§

§

impl<'a, T> Freeze for StackRef<'a, T>

§

impl<'a, T> RefUnwindSafe for StackRef<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> !Send for StackRef<'a, T>

§

impl<'a, T> !Sync for StackRef<'a, T>

§

impl<'a, T> Unpin for StackRef<'a, T>

§

impl<'a, T> UnwindSafe for StackRef<'a, T>
where T: RefUnwindSafe,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.