Skip to main content

canic_core/cdk/
mod.rs

1//! Module: cdk
2//!
3//! Responsibility: Canic's internal Candid, stable-structure, value-type, and hash substrate.
4//! Does not own: IC runtime APIs, application policy, endpoint auth, or stable schema design.
5//! Boundary: shared implementation support for Canic-owned runtime crates.
6
7pub use candid;
8
9pub mod serialize;
10pub mod structures;
11pub mod types;
12pub mod utils;
13
14///
15/// impl_storable_bounded
16///
17/// Implement bounded stable storage encoding for one serde-backed type.
18///
19
20#[macro_export]
21macro_rules! impl_storable_bounded {
22    ($ident:ident, $max_size:expr, $is_fixed_size:expr) => {
23        impl $crate::cdk::structures::storable::Storable for $ident {
24            const BOUND: $crate::cdk::structures::storable::Bound =
25                $crate::cdk::structures::storable::Bound::Bounded {
26                    max_size: $max_size,
27                    is_fixed_size: $is_fixed_size,
28                };
29
30            fn to_bytes(&self) -> ::std::borrow::Cow<'_, [u8]> {
31                let bytes = $crate::cdk::serialize::serialize(self).unwrap_or_else(|err| {
32                    panic!("impl_storable_bounded: serialize failed: {err}");
33                });
34
35                ::std::borrow::Cow::Owned(bytes)
36            }
37
38            fn into_bytes(self) -> Vec<u8> {
39                $crate::cdk::serialize::serialize(&self).unwrap_or_else(|err| {
40                    panic!("impl_storable_bounded: serialize failed: {err}");
41                })
42            }
43
44            fn from_bytes(bytes: ::std::borrow::Cow<'_, [u8]>) -> Self {
45                $crate::cdk::serialize::deserialize(&bytes).unwrap_or_else(|err| {
46                    panic!("impl_storable_bounded: deserialize failed: {err}");
47                })
48            }
49        }
50    };
51}
52
53///
54/// impl_storable_unbounded
55///
56/// Implement unbounded stable storage encoding for one serde-backed type.
57///
58
59#[macro_export]
60macro_rules! impl_storable_unbounded {
61    ($ident:ident) => {
62        impl $crate::cdk::structures::storable::Storable for $ident {
63            const BOUND: $crate::cdk::structures::storable::Bound =
64                $crate::cdk::structures::storable::Bound::Unbounded;
65
66            fn to_bytes(&self) -> ::std::borrow::Cow<'_, [u8]> {
67                let bytes = $crate::cdk::serialize::serialize(self).unwrap_or_else(|err| {
68                    panic!("impl_storable_unbounded: serialize failed: {err}");
69                });
70
71                ::std::borrow::Cow::Owned(bytes)
72            }
73
74            fn into_bytes(self) -> Vec<u8> {
75                $crate::cdk::serialize::serialize(&self).unwrap_or_else(|err| {
76                    panic!("impl_storable_unbounded: serialize failed: {err}");
77                })
78            }
79
80            fn from_bytes(bytes: ::std::borrow::Cow<'_, [u8]>) -> Self {
81                $crate::cdk::serialize::deserialize(&bytes).unwrap_or_else(|err| {
82                    panic!("impl_storable_unbounded: deserialize failed: {err}");
83                })
84            }
85        }
86    };
87}