Struct ctxmap::CtxMapView
source · [−]pub struct CtxMapView<'a, S: Schema>(_);Expand description
Mutable reference to CtxMap where the value has changed.
Use CtxMapViwe instead of &mut CtxMap because &mut CtxMap,
whose value has been changed, will be broken if std::mem::swap is used.
Implementations
sourceimpl<'a, S: Schema> CtxMapView<'a, S>
impl<'a, S: Schema> CtxMapView<'a, S>
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 to CtxMap 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);
m.with(&KEY_A, &40, |m| {
assert_eq!(m[&KEY_A], 40);
});
assert_eq!(m[&KEY_A], 30);
});
assert_eq!(m[&KEY_A], 20);sourcepub fn viwe(&mut self) -> CtxMapView<'_, S>
pub fn viwe(&mut self) -> CtxMapView<'_, S>
Return CtxMapView with modified lifetime.
Methods from Deref<Target = CtxMap<S>>
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<'a, S> !RefUnwindSafe for CtxMapView<'a, S>
impl<'a, S> !Send for CtxMapView<'a, S>
impl<'a, S> !Sync for CtxMapView<'a, S>
impl<'a, S> Unpin for CtxMapView<'a, S>
impl<'a, S> !UnwindSafe for CtxMapView<'a, 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