ic_cdk/
lib.rs

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