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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use crate::candid::utils::{ArgumentDecoder, ArgumentEncoder};
pub use crate::stable::*;
use crate::{candid, CallResponse, Context, Principal, StableMemoryError};
#[inline(always)]
fn get_context() -> &'static mut impl Context {
#[cfg(not(target_family = "wasm"))]
return crate::inject::get_context();
#[cfg(target_family = "wasm")]
return crate::wasm::IcContext::context();
}
#[inline(always)]
pub fn trap(message: &str) -> ! {
get_context().trap(message)
}
#[inline(always)]
pub fn print<S: AsRef<str>>(s: S) {
get_context().print(s)
}
#[inline(always)]
pub fn id() -> Principal {
get_context().id()
}
#[inline(always)]
pub fn time() -> u64 {
get_context().time()
}
#[inline(always)]
pub fn balance() -> u64 {
get_context().balance()
}
#[inline(always)]
pub fn caller() -> Principal {
get_context().caller()
}
#[inline(always)]
pub fn msg_cycles_available() -> u64 {
get_context().msg_cycles_available()
}
#[inline(always)]
pub fn msg_cycles_accept(amount: u64) -> u64 {
get_context().msg_cycles_accept(amount)
}
#[inline(always)]
pub fn msg_cycles_refunded() -> u64 {
get_context().msg_cycles_refunded()
}
#[inline(always)]
pub fn stable_store<T>(data: T) -> Result<(), candid::Error>
where
T: ArgumentEncoder,
{
get_context().stable_store(data)
}
#[inline(always)]
pub fn stable_restore<T>() -> Result<T, String>
where
T: for<'de> ArgumentDecoder<'de>,
{
get_context().stable_restore()
}
#[inline(always)]
pub fn call_raw<S: Into<String>>(
id: Principal,
method: S,
args_raw: Vec<u8>,
cycles: u64,
) -> CallResponse<Vec<u8>> {
get_context().call_raw(id, method, args_raw, cycles)
}
#[inline(always)]
pub fn call<T: ArgumentEncoder, R: for<'a> ArgumentDecoder<'a>, S: Into<String>>(
id: Principal,
method: S,
args: T,
) -> CallResponse<R> {
get_context().call_with_payment(id, method, args, 0)
}
#[inline(always)]
pub fn call_with_payment<T: ArgumentEncoder, R: for<'a> ArgumentDecoder<'a>, S: Into<String>>(
id: Principal,
method: S,
args: T,
cycles: u64,
) -> CallResponse<R> {
get_context().call_with_payment(id, method, args, cycles)
}
#[inline(always)]
pub fn set_certified_data(data: &[u8]) {
get_context().set_certified_data(data)
}
#[inline(always)]
pub fn data_certificate() -> Option<Vec<u8>> {
get_context().data_certificate()
}
#[inline(always)]
pub fn spawn<F: 'static + std::future::Future<Output = ()>>(future: F) {
get_context().spawn(future)
}
#[inline(always)]
pub fn stable_size() -> u32 {
get_context().stable_size()
}
#[inline(always)]
pub fn stable_grow(new_pages: u32) -> Result<u32, StableMemoryError> {
get_context().stable_grow(new_pages)
}
#[inline(always)]
pub fn stable_write(offset: u32, buf: &[u8]) {
get_context().stable_write(offset, buf)
}
#[inline(always)]
pub fn stable_read(offset: u32, buf: &mut [u8]) {
get_context().stable_read(offset, buf)
}
pub fn stable_bytes() -> Vec<u8> {
let size = (stable_size() as usize) << 16;
let mut vec = Vec::with_capacity(size);
unsafe {
vec.set_len(size);
}
stable_read(0, vec.as_mut_slice());
vec
}
pub fn with<T: 'static + Default, U, F: FnOnce(&T) -> U>(callback: F) -> U {
get_context().with(callback)
}
pub fn maybe_with<T: 'static, U, F: FnOnce(&T) -> U>(callback: F) -> Option<U> {
get_context().maybe_with(callback)
}
pub fn with_mut<T: 'static + Default, U, F: FnOnce(&mut T) -> U>(callback: F) -> U {
get_context().with_mut(callback)
}
pub fn maybe_with_mut<T: 'static, U, F: FnOnce(&mut T) -> U>(callback: F) -> Option<U> {
get_context().maybe_with_mut(callback)
}
pub fn take<T: 'static>() -> Option<T> {
get_context().take::<T>()
}
pub fn swap<T: 'static>(value: T) -> Option<T> {
get_context().swap(value)
}
#[inline(always)]
#[deprecated(
since = "0.4.8",
note = "Unsafe memory methods are deprecated, use the safer ic_kit::ic::swap method."
)]
pub fn store<T: 'static>(data: T) {
get_context().store(data)
}
#[inline(always)]
#[deprecated(
since = "0.4.8",
note = "Unsafe memory methods are deprecated, use the safer ic_kit::ic::maybe_with method."
)]
pub fn get_maybe<T: 'static>() -> Option<&'static T> {
get_context().get_maybe()
}
#[inline(always)]
#[deprecated(
since = "0.4.8",
note = "Unsafe memory methods are deprecated, use the safer ic_kit::ic::with method."
)]
pub fn get<T: 'static + Default>() -> &'static T {
get_context().get_mut()
}
#[inline(always)]
#[deprecated(
since = "0.4.8",
note = "Unsafe memory methods are deprecated, use the safer ic_kit::ic::with method."
)]
pub fn get_mut<T: 'static + Default>() -> &'static mut T {
get_context().get_mut()
}
#[inline(always)]
#[deprecated(
since = "0.4.8",
note = "Unsafe memory methods are deprecated, use the safer ic_kit::ic::take method."
)]
pub fn delete<T: 'static + Default>() -> bool {
get_context().delete::<T>()
}