pub struct Key(/* private fields */);Expand description
A per-thread unique key used to access data inside KeyCells.
This type enforces the existence of only a single instance of itself per
thread. By doing so it guards against mutable aliasing when borrowing from
KeyCell.
Implementations§
Source§impl Key
impl Key
Sourcepub fn split_ro<'a: 'b, 'b, T: Meta>(
item: &'b RoGuard<'a, T>,
) -> (&'b KeyCell<T>, &'b Key)
pub fn split_ro<'a: 'b, 'b, T: Meta>( item: &'b RoGuard<'a, T>, ) -> (&'b KeyCell<T>, &'b Key)
Split an Ro into its consitutuent KeyCell and immutable Key.
Similar to split_rw. Unlike split_rw, the Ro
remains accessible because multiple simultaneous immutable borrows
are allowed.
§Examples
use mutcy::{Key, KeyCell, Ro};
fn function(kc1: Ro<i32>, kc2: &KeyCell<i32>) {
let (_, key) = Key::split_ro(kc1);
println!("{}", **kc1);
println!("{}", *kc2.ro(key));
}
let kc1 = KeyCell::new(0, ());
let kc2 = KeyCell::new(10, ());
let mut key = Key::acquire();
let mut kc1_borrow = kc1.ro(&mut key);
function(&kc1_borrow, &kc2);Sourcepub fn split_rw<'a: 'b, 'b, T: Meta>(
item: &'b mut RwGuard<'a, T>,
) -> (&'b KeyCell<T>, &'b mut Key)
pub fn split_rw<'a: 'b, 'b, T: Meta>( item: &'b mut RwGuard<'a, T>, ) -> (&'b KeyCell<T>, &'b mut Key)
Split an Rw into its consitutuent KeyCell and mutable Key.
Whenever we want to borrow another item while already holding a Rw
we need to unborrow. This function unborrows, granting you access to
the key that was used to borrow the initial KeyCell.
§Examples
use mutcy::{Key, KeyCell, Rw};
fn function(kc1: Rw<i32>, kc2: &KeyCell<i32>) {
**kc1 += 1; // kc1 = 1
let (kc1_ref, key) = Key::split_rw(kc1);
let mut kc2_mut = kc2.rw(key);
*kc2_mut += 1;
// Relinquish the split.
**kc1 += 1; // kc1 = 2;
// Compile error.
// *kc2_mut += 1;
}
let kc1 = KeyCell::new(0, ());
let kc2 = KeyCell::new(10, ());
let mut key = Key::acquire();
let mut kc1_borrow = kc1.rw(&mut key);
function(&mut kc1_borrow, &kc2);Trait Implementations§
Auto Trait Implementations§
impl Freeze for Key
impl RefUnwindSafe for Key
impl !Send for Key
impl !Sync for Key
impl Unpin for Key
impl UnwindSafe for Key
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more