Skip to main content

canic_core/cdk/
mod.rs

1//! Module: cdk
2//!
3//! Responsibility: Canic's IC CDK facade and stable-structure helper macros.
4//! Does not own: application policy, endpoint auth, or stable schema design.
5//! Boundary: re-exports IC-facing SDKs behind the Canic core surface.
6
7pub use candid;
8pub use ic_cdk::{
9    api, call, eprintln, export_candid, futures, init, inspect_message, post_upgrade, println,
10    query, trap, update,
11};
12pub use ic_cdk_timers as timers;
13
14pub mod serialize;
15pub mod spec;
16pub mod structures;
17pub mod types;
18pub mod utils;
19
20///
21/// impl_storable_bounded
22///
23/// Implement bounded stable storage encoding for one serde-backed type.
24///
25
26#[macro_export]
27macro_rules! impl_storable_bounded {
28    ($ident:ident, $max_size:expr, $is_fixed_size:expr) => {
29        impl $crate::cdk::structures::storable::Storable for $ident {
30            const BOUND: $crate::cdk::structures::storable::Bound =
31                $crate::cdk::structures::storable::Bound::Bounded {
32                    max_size: $max_size,
33                    is_fixed_size: $is_fixed_size,
34                };
35
36            fn to_bytes(&self) -> ::std::borrow::Cow<'_, [u8]> {
37                let bytes = $crate::cdk::serialize::serialize(self).unwrap_or_else(|err| {
38                    panic!("impl_storable_bounded: serialize failed: {err}");
39                });
40
41                ::std::borrow::Cow::Owned(bytes)
42            }
43
44            fn into_bytes(self) -> Vec<u8> {
45                $crate::cdk::serialize::serialize(&self).unwrap_or_else(|err| {
46                    panic!("impl_storable_bounded: serialize failed: {err}");
47                })
48            }
49
50            fn from_bytes(bytes: ::std::borrow::Cow<'_, [u8]>) -> Self {
51                $crate::cdk::serialize::deserialize(&bytes).unwrap_or_else(|err| {
52                    panic!("impl_storable_bounded: deserialize failed: {err}");
53                })
54            }
55        }
56    };
57}
58
59///
60/// impl_storable_unbounded
61///
62/// Implement unbounded stable storage encoding for one serde-backed type.
63///
64
65#[macro_export]
66macro_rules! impl_storable_unbounded {
67    ($ident:ident) => {
68        impl $crate::cdk::structures::storable::Storable for $ident {
69            const BOUND: $crate::cdk::structures::storable::Bound =
70                $crate::cdk::structures::storable::Bound::Unbounded;
71
72            fn to_bytes(&self) -> ::std::borrow::Cow<'_, [u8]> {
73                let bytes = $crate::cdk::serialize::serialize(self).unwrap_or_else(|err| {
74                    panic!("impl_storable_unbounded: serialize failed: {err}");
75                });
76
77                ::std::borrow::Cow::Owned(bytes)
78            }
79
80            fn into_bytes(self) -> Vec<u8> {
81                $crate::cdk::serialize::serialize(&self).unwrap_or_else(|err| {
82                    panic!("impl_storable_unbounded: serialize failed: {err}");
83                })
84            }
85
86            fn from_bytes(bytes: ::std::borrow::Cow<'_, [u8]>) -> Self {
87                $crate::cdk::serialize::deserialize(&bytes).unwrap_or_else(|err| {
88                    panic!("impl_storable_unbounded: deserialize failed: {err}");
89                })
90            }
91        }
92    };
93}