CtxMap

Struct CtxMap 

Source
pub struct CtxMap<S: Schema> { /* private fields */ }
Expand description

A collection that can store references of different types and lifetimes.

Implementations§

Source§

impl<S: Schema> CtxMap<S>

Source

pub fn new() -> Self

Create a new CtxMap with initial values.

§Example
ctxmap::schema!(S);
ctxmap::key!(S { KEY_A: u16 = 20 });
ctxmap::key!(S { KEY_B: u8 });

let m = ctxmap::CtxMap::new();
assert_eq!(m[&KEY_A], 20);
assert_eq!(m.get(&KEY_A), Some(&20));
assert_eq!(m.get(&KEY_B), None);
Source

pub fn with<T: ?Sized + 'static, U>( &mut self, key: &'static Key<S, T>, value: &T, f: impl FnOnce(&mut CtxMapView<'_, S>) -> U, ) -> U

Sets a value corresponding to the key only while f is being called.

§Example
ctxmap::schema!(S);
ctxmap::key!(S { KEY_A: u16 = 20 });

let mut m = ctxmap::CtxMap::new();
assert_eq!(m[&KEY_A], 20);
m.with(&KEY_A, &30, |m| {
    assert_eq!(m[&KEY_A], 30);
});
assert_eq!(m[&KEY_A], 20);
Source

pub fn with_mut<T: ?Sized + 'static, U, const MUT: bool>( &mut self, key: &'static Key<S, T, MUT>, value: &mut T, f: impl FnOnce(&mut CtxMapView<'_, S>) -> U, ) -> U

Sets a mutable value corresponding to the key only while f is being called.

§Example
ctxmap::schema!(S);
ctxmap::key!(S { mut KEY_A: u16 = 20 });

let mut m = ctxmap::CtxMap::new();
assert_eq!(m[&KEY_A], 20);
m[&KEY_A] = 25;
assert_eq!(m[&KEY_A], 25);
m.with_mut(&KEY_A, &mut 30, |m| {
    assert_eq!(m[&KEY_A], 30);
    m[&KEY_A] = 35;
    assert_eq!(m[&KEY_A], 35);
});
assert_eq!(m[&KEY_A], 25);
Source

pub fn view(&mut self) -> CtxMapView<'_, S>

Get CtxMapView that references self.

Source

pub fn get<T: ?Sized, const MUT: bool>( &self, key: &'static Key<S, T, MUT>, ) -> Option<&T>

Returns a reference to the value corresponding to the key.

§Example
ctxmap::schema!(S);
ctxmap::key!(S { KEY_A: u16 });

let mut m = ctxmap::CtxMap::new();
assert_eq!(m.get(&KEY_A), None);
m.with(&KEY_A, &10, |m| {
    assert_eq!(m.get(&KEY_A), Some(&10));
});
assert_eq!(m.get(&KEY_A), None);
Source

pub fn get_mut<T: ?Sized>( &mut self, key: &'static KeyMut<S, T>, ) -> Option<&mut T>

Returns a mutable reference to the value corresponding to the key.

§Example
ctxmap::schema!(S);
ctxmap::key!(S { mut KEY_A: u16 });

let mut m = ctxmap::CtxMap::new();
assert_eq!(m.get_mut(&KEY_A), None);
m.with_mut(&KEY_A, &mut 10, |m| {
    assert_eq!(m.get_mut(&KEY_A), Some(&mut 10));
});
assert_eq!(m.get_mut(&KEY_A), None);

Trait Implementations§

Source§

impl<S: Schema> Default for CtxMap<S>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S, T, const MUT: bool> Index<&'static Key<S, T, MUT>> for CtxMap<S>
where S: Schema, T: ?Sized + 'static,

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: &'static Key<S, T, MUT>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<S, T> IndexMut<&'static Key<S, T, true>> for CtxMap<S>
where S: Schema, T: ?Sized + 'static,

Source§

fn index_mut(&mut self, index: &'static KeyMut<S, T>) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<S> !Freeze for CtxMap<S>

§

impl<S> !RefUnwindSafe for CtxMap<S>

§

impl<S> !Send for CtxMap<S>

§

impl<S> !Sync for CtxMap<S>

§

impl<S> Unpin for CtxMap<S>
where S: Unpin,

§

impl<S> !UnwindSafe for CtxMap<S>

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.