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>
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 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
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);Sourcepub fn view(&mut self) -> CtxMapView<'_, S>
pub fn view(&mut self) -> CtxMapView<'_, S>
Get CtxMapView that references self.
Sourcepub fn get<T: ?Sized, const MUT: bool>(
&self,
key: &'static Key<S, T, MUT>,
) -> Option<&T>
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);Sourcepub fn get_mut<T: ?Sized>(
&mut self,
key: &'static KeyMut<S, T>,
) -> Option<&mut T>
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§
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> 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