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/// Setup the stdlib hooks.
36pub fn setup() {
37    if !DONE.swap(true, Ordering::SeqCst) {
38        printer::hook()
39    }
40}
41
42/// See documentation for [spawn].
43#[deprecated(
44    since = "0.3.4",
45    note = "Use the spawn() function instead, it does the same thing but is more appropriately named."
46)]
47pub fn block_on<F: 'static + std::future::Future<Output = ()>>(future: F) {
48    ic_cdk_executor::spawn(future);
49}
50
51/// Spawn an asynchronous task that drives the provided future to
52/// completion.
53pub fn spawn<F: 'static + std::future::Future<Output = ()>>(future: F) {
54    ic_cdk_executor::spawn(future);
55}
56
57/// Format and then print the formatted message
58#[cfg(target_arch = "wasm32")]
59#[macro_export]
60macro_rules! println {
61    ($fmt:expr) => ($crate::print(format!($fmt)));
62    ($fmt:expr, $($arg:tt)*) => ($crate::print(format!($fmt, $($arg)*)));
63}
64
65/// Format and then print the formatted message
66#[cfg(not(target_arch = "wasm32"))]
67#[macro_export]
68macro_rules! println {
69    ($fmt:expr) => (std::println!($fmt));
70    ($fmt:expr, $($arg:tt)*) => (std::println!($fmt, $($arg)*));
71}
72
73/// Format and then print the formatted message
74#[cfg(target_arch = "wasm32")]
75#[macro_export]
76macro_rules! eprintln {
77    ($fmt:expr) => ($crate::print(format!($fmt)));
78    ($fmt:expr, $($arg:tt)*) => ($crate::print(format!($fmt, $($arg)*)));
79}
80
81/// Format and then print the formatted message
82#[cfg(not(target_arch = "wasm32"))]
83#[macro_export]
84macro_rules! eprintln {
85    ($fmt:expr) => (std::eprintln!($fmt));
86    ($fmt:expr, $($arg:tt)*) => (std::eprintln!($fmt, $($arg)*));
87}