Struct KeyCell

Source
pub struct KeyCell<T: Meta>(/* private fields */);
Expand description

A RefCell-like wrapper that can only be accessed via Key.

Implementations§

Source§

impl<T: Meta> KeyCell<T>

Source

pub fn new(item: T, attached: T::Data) -> Self

Creates a new KeyCell from an object and its associated metadata.

§Examples
use mutcy::KeyCell;

let kc = KeyCell::new(123, ());
Source

pub fn borrow<'a>(&'a self, _key: &'a Key) -> KeyCellRef<'a, T>

Immutably borrows the wrapped value.

§Examples
use mutcy::{Key, KeyCell};

let key = Key::acquire();
let kc = KeyCell::new(123, ());

let borrowed1 = kc.borrow(&key);
let borrowed2 = kc.borrow(&key);
Source

pub fn borrow_mut<'a>(&'a self, key: &'a mut Key) -> KeyCellMut<'a, T>

Mutably borrows the wrapped value.

Requires a &mut Key that ensures that no other borrows exist simultaneously.

§Examples
use mutcy::{Key, KeyCell};

let mut key = Key::acquire();
let kc = KeyCell::new(123, ());

let borrowed1 = kc.borrow_mut(&mut key);

let borrowed2 = kc.borrow(&key);

// This does not compile.
// let _ = *borrowed1;
Source

pub fn meta(&self) -> &T::Data

Acquire metadata. See Meta.

§Examples
use mutcy::{KeyCell, Meta};
struct A;

impl Meta for A {
    type Data = i32;
}

let kc = KeyCell::new(A, 123);

assert_eq!(*kc.meta(), 123);

Auto Trait Implementations§

§

impl<T> !Freeze for KeyCell<T>

§

impl<T> !RefUnwindSafe for KeyCell<T>

§

impl<T> Send for KeyCell<T>
where <T as Meta>::Data: Send, T: Send,

§

impl<T> !Sync for KeyCell<T>

§

impl<T> Unpin for KeyCell<T>
where <T as Meta>::Data: Unpin, T: Unpin,

§

impl<T> UnwindSafe for KeyCell<T>
where <T as Meta>::Data: UnwindSafe, T: UnwindSafe,

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.