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>
impl<'a, T> StackRef<'a, T>
Sourcepub fn get(&self) -> &'a T
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);Sourcepub fn get_mut(&mut self) -> &'a mut T
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);