pub fn with<T: 'static + Default, U, F: FnOnce(&T) -> U>(callback: F) -> UExpand description
Pass an immutable 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 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 get(&self) -> u64 {
*self.count
}
}
MockContext::new()
.with_data(Counter { count: 17 })
.inject();
assert_eq!(ic::with(Counter::get), 17);