pub struct CtxMap<S: Schema> { /* private fields */ }Expand description
A collection that can store references of different types and lifetimes.
Implementations
sourceimpl<S: Schema> CtxMap<S>
impl<S: Schema> CtxMap<S>
sourcepub fn new() -> Self
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);sourcepub fn with<T: ?Sized + 'static, U>(
&mut self,
key: &'static Key<S, T>,
value: &T,
f: impl FnOnce(&mut CtxMapView<'_, S>) -> U
) -> U
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);sourcepub fn view(&mut self) -> CtxMapView<'_, S>
pub fn view(&mut self) -> CtxMapView<'_, S>
Get CtxMapView that references self.
sourcepub fn get<T: ?Sized>(&self, key: &'static Key<S, T>) -> Option<&T>
pub fn get<T: ?Sized>(&self, key: &'static Key<S, T>) -> 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);Trait Implementations
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more