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 bounded_cell;
10pub mod serialize;
11pub mod structures;
12pub mod types;
13pub mod utils;
14
15///
16/// impl_storable_bounded
17///
18/// Implement bounded stable storage encoding for one serde-backed type.
19///
20
21#[macro_export]
22macro_rules! impl_storable_bounded {
23    ($ident:ident, $max_size:expr, $is_fixed_size:expr) => {
24        impl $crate::cdk::structures::storable::Storable for $ident {
25            const BOUND: $crate::cdk::structures::storable::Bound =
26                $crate::cdk::structures::storable::Bound::Bounded {
27                    max_size: $max_size,
28                    is_fixed_size: $is_fixed_size,
29                };
30
31            fn to_bytes(&self) -> ::std::borrow::Cow<'_, [u8]> {
32                let bytes = $crate::cdk::serialize::serialize(self).unwrap_or_else(|err| {
33                    panic!("impl_storable_bounded: serialize failed: {err}");
34                });
35
36                ::std::borrow::Cow::Owned(bytes)
37            }
38
39            fn into_bytes(self) -> Vec<u8> {
40                $crate::cdk::serialize::serialize(&self).unwrap_or_else(|err| {
41                    panic!("impl_storable_bounded: serialize failed: {err}");
42                })
43            }
44
45            fn from_bytes(bytes: ::std::borrow::Cow<'_, [u8]>) -> Self {
46                $crate::cdk::serialize::deserialize(&bytes).unwrap_or_else(|err| {
47                    panic!("impl_storable_bounded: deserialize failed: {err}");
48                })
49            }
50        }
51    };
52}
53
54///
55/// impl_storable_unbounded
56///
57/// Implement unbounded stable storage encoding for one serde-backed type.
58///
59
60#[macro_export]
61macro_rules! impl_storable_unbounded {
62    ($ident:ident) => {
63        impl $crate::cdk::structures::storable::Storable for $ident {
64            const BOUND: $crate::cdk::structures::storable::Bound =
65                $crate::cdk::structures::storable::Bound::Unbounded;
66
67            fn to_bytes(&self) -> ::std::borrow::Cow<'_, [u8]> {
68                let bytes = $crate::cdk::serialize::serialize(self).unwrap_or_else(|err| {
69                    panic!("impl_storable_unbounded: serialize failed: {err}");
70                });
71
72                ::std::borrow::Cow::Owned(bytes)
73            }
74
75            fn into_bytes(self) -> Vec<u8> {
76                $crate::cdk::serialize::serialize(&self).unwrap_or_else(|err| {
77                    panic!("impl_storable_unbounded: serialize failed: {err}");
78                })
79            }
80
81            fn from_bytes(bytes: ::std::borrow::Cow<'_, [u8]>) -> Self {
82                $crate::cdk::serialize::deserialize(&bytes).unwrap_or_else(|err| {
83                    panic!("impl_storable_unbounded: deserialize failed: {err}");
84                })
85            }
86        }
87    };
88}