Struct Key

Source
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

Source

pub fn acquire() -> Self

Acquire the thread-local Key.

§Panics

Panics if an instance already exists for the current thread. Calling acquire after dropping the key will not panic.

§Examples
use mutcy::Key;

let key = Key::acquire();
Source

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§

Source§

impl Drop for Key

Source§

fn drop(&mut self)

Drops the key. Once the key is dropped, the next acquire will not panic.

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