pub fn with_mut<T: 'static + Default, U, F: FnOnce(&mut T) -> U>(
callback: F,
) -> UExpand description
Pass a mutable reference to the value associated with the given type to the closure.
If no value is currently associated to the type T, this method will insert the default
value in its place before invoking the callback. Use maybe_with_mut if you don’t want the
default value to be inserted or if your type does not implement the Default trait.
This is a safe replacement for the previously known ic_kit::ic::get API, and you can use it
instead of lazy_static or local_thread.
§Example
use ic_kit::*;
#[derive(Default)]
struct Counter {
count: u64
}
impl Counter {
fn increment(&mut self) -> u64 {
self.count += 1;
*self.count
}
}
MockContext::new()
.with_data(Counter { count: 17 })
.inject();
assert_eq!(ic::with_mut(Counter::increment), 18);