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 futures;
17mod macros;
18mod printer;
19pub mod storage;
20
21use std::sync::atomic::{AtomicBool, Ordering};
22
23#[doc(inline)]
24pub use api::call::call;
25#[doc(inline)]
26pub use api::call::notify;
27#[doc(inline)]
28pub use api::{caller, id, print, trap};
29
30#[doc(inline)]
31pub use macros::*;
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    futures::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    futures::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}