ic_cdk/
lib.rs

1#![doc = include_str!("../README.md")]
2#![warn(
3    elided_lifetimes_in_paths,
4    missing_debug_implementations,
5    missing_docs,
6    unsafe_op_in_unsafe_fn,
7    clippy::undocumented_unsafe_blocks,
8    clippy::missing_safety_doc
9)]
10#![cfg_attr(docsrs, feature(doc_cfg))]
11
12#[cfg(target_feature = "atomics")]
13compile_error!("This version of the CDK does not support multithreading.");
14
15pub mod api;
16mod macros;
17mod printer;
18pub mod storage;
19
20use std::sync::atomic::{AtomicBool, Ordering};
21
22#[doc(inline)]
23pub use api::call::call;
24#[doc(inline)]
25pub use api::call::notify;
26#[doc(inline)]
27pub use api::{caller, id, print, trap};
28
29#[doc(inline)]
30pub use macros::*;
31
32static DONE: AtomicBool = AtomicBool::new(false);
33
34/// Setup the stdlib hooks.
35pub fn setup() {
36    if !DONE.swap(true, Ordering::SeqCst) {
37        printer::hook()
38    }
39}
40
41/// See documentation for [spawn].
42#[deprecated(
43    since = "0.3.4",
44    note = "Use the spawn() function instead, it does the same thing but is more appropriately named."
45)]
46pub fn block_on<F: 'static + std::future::Future<Output = ()>>(future: F) {
47    ic_cdk_executor::spawn(future);
48}
49
50/// Spawn an asynchronous task that drives the provided future to
51/// completion.
52pub fn spawn<F: 'static + std::future::Future<Output = ()>>(future: F) {
53    ic_cdk_executor::spawn(future);
54}
55
56/// Format and then print the formatted message
57#[cfg(target_arch = "wasm32")]
58#[macro_export]
59macro_rules! println {
60    ($fmt:expr) => ($crate::print(format!($fmt)));
61    ($fmt:expr, $($arg:tt)*) => ($crate::print(format!($fmt, $($arg)*)));
62}
63
64/// Format and then print the formatted message
65#[cfg(not(target_arch = "wasm32"))]
66#[macro_export]
67macro_rules! println {
68    ($fmt:expr) => (std::println!($fmt));
69    ($fmt:expr, $($arg:tt)*) => (std::println!($fmt, $($arg)*));
70}
71
72/// Format and then print the formatted message
73#[cfg(target_arch = "wasm32")]
74#[macro_export]
75macro_rules! eprintln {
76    ($fmt:expr) => ($crate::print(format!($fmt)));
77    ($fmt:expr, $($arg:tt)*) => ($crate::print(format!($fmt, $($arg)*)));
78}
79
80/// Format and then print the formatted message
81#[cfg(not(target_arch = "wasm32"))]
82#[macro_export]
83macro_rules! eprintln {
84    ($fmt:expr) => (std::eprintln!($fmt));
85    ($fmt:expr, $($arg:tt)*) => (std::eprintln!($fmt, $($arg)*));
86}