1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use crate;
thread_local!
/// 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`.
/// Like [`with`], but does not initialize the data with the default value and simply returns None,
/// if there is no value associated with the type.
/// 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`.
/// Like [`with_mut`], but does not initialize the data with the default value and simply returns
/// None, if there is no value associated with the type.
/// Remove the current value associated with the type and return it.
/// Swaps the value associated with type `T` with the given value, returns the old one.
/// Like [`crate::ic::with`] but passes the immutable reference of multiple variables to the
/// closure as a tuple.
///
/// # Example
/// ```
/// use ic_kit::ic;
///
/// #[derive(Default)]
/// struct S1 {
/// a: u64,
/// }
///
/// #[derive(Default)]
/// struct S2 {
/// a: u64,
/// }
///
/// ic::with_many(|(a, b): (&S1, &S2)| {
/// // Now we have access to both S1 and S2.
/// println!("S1: {}, S2: {}", a.a, b.a);
/// });
/// ```
/// Like [`crate::ic::with_mut`] but passes the mutable reference of multiple variables to the
/// closure as a tuple.
///
/// # Example
/// ```
/// use ic_kit::ic;
///
/// #[derive(Default)]
/// struct S1 {
/// a: u64,
/// }
///
/// #[derive(Default)]
/// struct S2 {
/// a: u64,
/// }
///
/// ic::with_many_mut(|(a, b): (&mut S1, &mut S2)| {
/// // Now we have access to both S1 and S2 and can mutate them.
/// a.a += 1;
/// b.a += 1;
/// });
/// ```