1use crate::candid::utils::{ArgumentDecoder, ArgumentEncoder};
2pub use crate::stable::*;
3use crate::{candid, CallResponse, Context, Principal, StableMemoryError};
4
5#[inline(always)]
6fn get_context() -> &'static mut impl Context {
7 #[cfg(not(target_family = "wasm"))]
8 return crate::inject::get_context();
9 #[cfg(target_family = "wasm")]
10 return crate::wasm::IcContext::context();
11}
12
13#[inline(always)]
15pub fn trap(message: &str) -> ! {
16 get_context().trap(message)
17}
18
19#[inline(always)]
21pub fn print<S: AsRef<str>>(s: S) {
22 get_context().print(s)
23}
24
25#[inline(always)]
27pub fn id() -> Principal {
28 get_context().id()
29}
30
31#[inline(always)]
33pub fn time() -> u64 {
34 get_context().time()
35}
36
37#[inline(always)]
39pub fn balance() -> u64 {
40 get_context().balance()
41}
42
43#[inline(always)]
45pub fn caller() -> Principal {
46 get_context().caller()
47}
48
49#[inline(always)]
51pub fn msg_cycles_available() -> u64 {
52 get_context().msg_cycles_available()
53}
54
55#[inline(always)]
57pub fn msg_cycles_accept(amount: u64) -> u64 {
58 get_context().msg_cycles_accept(amount)
59}
60
61#[inline(always)]
64pub fn msg_cycles_refunded() -> u64 {
65 get_context().msg_cycles_refunded()
66}
67
68#[inline(always)]
70pub fn stable_store<T>(data: T) -> Result<(), candid::Error>
71where
72 T: ArgumentEncoder,
73{
74 get_context().stable_store(data)
75}
76
77#[inline(always)]
80pub fn stable_restore<T>() -> Result<T, String>
81where
82 T: for<'de> ArgumentDecoder<'de>,
83{
84 get_context().stable_restore()
85}
86
87#[inline(always)]
89pub fn call_raw<S: Into<String>>(
90 id: Principal,
91 method: S,
92 args_raw: Vec<u8>,
93 cycles: u64,
94) -> CallResponse<Vec<u8>> {
95 get_context().call_raw(id, method, args_raw, cycles)
96}
97
98#[inline(always)]
100pub fn call<T: ArgumentEncoder, R: for<'a> ArgumentDecoder<'a>, S: Into<String>>(
101 id: Principal,
102 method: S,
103 args: T,
104) -> CallResponse<R> {
105 get_context().call_with_payment(id, method, args, 0)
106}
107
108#[inline(always)]
109pub fn call_with_payment<T: ArgumentEncoder, R: for<'a> ArgumentDecoder<'a>, S: Into<String>>(
110 id: Principal,
111 method: S,
112 args: T,
113 cycles: u64,
114) -> CallResponse<R> {
115 get_context().call_with_payment(id, method, args, cycles)
116}
117
118#[inline(always)]
120pub fn set_certified_data(data: &[u8]) {
121 get_context().set_certified_data(data)
122}
123
124#[inline(always)]
126pub fn data_certificate() -> Option<Vec<u8>> {
127 get_context().data_certificate()
128}
129
130#[inline(always)]
132pub fn spawn<F: 'static + std::future::Future<Output = ()>>(future: F) {
133 get_context().spawn(future)
134}
135
136#[inline(always)]
139pub fn stable_size() -> u32 {
140 get_context().stable_size()
141}
142
143#[inline(always)]
148pub fn stable_grow(new_pages: u32) -> Result<u32, StableMemoryError> {
149 get_context().stable_grow(new_pages)
150}
151
152#[inline(always)]
154pub fn stable_write(offset: u32, buf: &[u8]) {
155 get_context().stable_write(offset, buf)
156}
157
158#[inline(always)]
160pub fn stable_read(offset: u32, buf: &mut [u8]) {
161 get_context().stable_read(offset, buf)
162}
163
164pub fn stable_bytes() -> Vec<u8> {
171 let size = (stable_size() as usize) << 16;
172 let mut vec = Vec::with_capacity(size);
173 unsafe {
174 vec.set_len(size);
175 }
176
177 stable_read(0, vec.as_mut_slice());
178
179 vec
180}
181
182pub fn with<T: 'static + Default, U, F: FnOnce(&T) -> U>(callback: F) -> U {
214 get_context().with(callback)
215}
216
217pub fn maybe_with<T: 'static, U, F: FnOnce(&T) -> U>(callback: F) -> Option<U> {
220 get_context().maybe_with(callback)
221}
222
223pub fn with_mut<T: 'static + Default, U, F: FnOnce(&mut T) -> U>(callback: F) -> U {
256 get_context().with_mut(callback)
257}
258
259pub fn maybe_with_mut<T: 'static, U, F: FnOnce(&mut T) -> U>(callback: F) -> Option<U> {
262 get_context().maybe_with_mut(callback)
263}
264
265pub fn take<T: 'static>() -> Option<T> {
267 get_context().take::<T>()
268}
269
270pub fn swap<T: 'static>(value: T) -> Option<T> {
272 get_context().swap(value)
273}
274
275#[inline(always)]
277#[deprecated(
278 since = "0.4.8",
279 note = "Unsafe memory methods are deprecated, use the safer ic_kit::ic::swap method."
280)]
281pub fn store<T: 'static>(data: T) {
282 get_context().store(data)
283}
284
285#[inline(always)]
287#[deprecated(
288 since = "0.4.8",
289 note = "Unsafe memory methods are deprecated, use the safer ic_kit::ic::maybe_with method."
290)]
291pub fn get_maybe<T: 'static>() -> Option<&'static T> {
292 get_context().get_maybe()
293}
294
295#[inline(always)]
298#[deprecated(
299 since = "0.4.8",
300 note = "Unsafe memory methods are deprecated, use the safer ic_kit::ic::with method."
301)]
302pub fn get<T: 'static + Default>() -> &'static T {
303 get_context().get_mut()
304}
305
306#[inline(always)]
310#[deprecated(
311 since = "0.4.8",
312 note = "Unsafe memory methods are deprecated, use the safer ic_kit::ic::with method."
313)]
314pub fn get_mut<T: 'static + Default>() -> &'static mut T {
315 get_context().get_mut()
316}
317
318#[inline(always)]
320#[deprecated(
321 since = "0.4.8",
322 note = "Unsafe memory methods are deprecated, use the safer ic_kit::ic::take method."
323)]
324pub fn delete<T: 'static + Default>() -> bool {
325 get_context().delete::<T>()
326}