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<'a: 'b, 'b, T: Meta>(
item: &'b mut KeyCellMut<'a, T>,
) -> (&'b KeyCell<T>, &'b mut Key)
pub fn split<'a: 'b, 'b, T: Meta>( item: &'b mut KeyCellMut<'a, T>, ) -> (&'b KeyCell<T>, &'b mut Key)
Split a KeyMut into its consitutuent KeyCell and Key.
Whenever we want to borrow another item while already holding a KeyMut
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, KeyMut};
fn function(kc1: KeyMut<i32>, kc2: &KeyCell<i32>) {
**kc1 += 1; // kc1 = 1
let (kc1_ref, key) = Key::split(kc1);
let mut kc2_mut = kc2.borrow_mut(key);
*kc2_mut += 1;
// Relinquish the split.
**kc1 += 1;
// 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.borrow_mut(&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