Skip to main content

canic_core/cdk/
mod.rs

1///
2/// Canic IC SDK facade
3///
4pub use candid;
5pub use ic_cdk::{
6    api, call, eprintln, export_candid, futures, init, inspect_message, post_upgrade, println,
7    query, trap, update,
8};
9pub use ic_cdk_management_canister as mgmt;
10pub use ic_cdk_timers as timers;
11pub use icrc_ledger_types;
12
13pub mod serialize;
14pub mod spec;
15pub mod structures;
16pub mod types;
17pub mod utils;
18
19///
20/// Storable helpers
21///
22
23#[macro_export]
24macro_rules! impl_storable_bounded {
25    ($ident:ident, $max_size:expr, $is_fixed_size:expr) => {
26        impl $crate::cdk::structures::storable::Storable for $ident {
27            const BOUND: $crate::cdk::structures::storable::Bound =
28                $crate::cdk::structures::storable::Bound::Bounded {
29                    max_size: $max_size,
30                    is_fixed_size: $is_fixed_size,
31                };
32
33            fn to_bytes(&self) -> ::std::borrow::Cow<'_, [u8]> {
34                let bytes = $crate::cdk::serialize::serialize(self).unwrap_or_else(|err| {
35                    panic!("impl_storable_bounded: serialize failed: {err}");
36                });
37
38                ::std::borrow::Cow::Owned(bytes)
39            }
40
41            fn into_bytes(self) -> Vec<u8> {
42                $crate::cdk::serialize::serialize(&self).unwrap_or_else(|err| {
43                    panic!("impl_storable_bounded: serialize failed: {err}");
44                })
45            }
46
47            fn from_bytes(bytes: ::std::borrow::Cow<'_, [u8]>) -> Self {
48                $crate::cdk::serialize::deserialize(&bytes).unwrap_or_else(|err| {
49                    panic!("impl_storable_bounded: deserialize failed: {err}");
50                })
51            }
52        }
53    };
54}
55
56#[macro_export]
57macro_rules! impl_storable_unbounded {
58    ($ident:ident) => {
59        impl $crate::cdk::structures::storable::Storable for $ident {
60            const BOUND: $crate::cdk::structures::storable::Bound =
61                $crate::cdk::structures::storable::Bound::Unbounded;
62
63            fn to_bytes(&self) -> ::std::borrow::Cow<'_, [u8]> {
64                let bytes = $crate::cdk::serialize::serialize(self).unwrap_or_else(|err| {
65                    panic!("impl_storable_unbounded: serialize failed: {err}");
66                });
67
68                ::std::borrow::Cow::Owned(bytes)
69            }
70
71            fn into_bytes(self) -> Vec<u8> {
72                $crate::cdk::serialize::serialize(&self).unwrap_or_else(|err| {
73                    panic!("impl_storable_unbounded: serialize failed: {err}");
74                })
75            }
76
77            fn from_bytes(bytes: ::std::borrow::Cow<'_, [u8]>) -> Self {
78                $crate::cdk::serialize::deserialize(&bytes).unwrap_or_else(|err| {
79                    panic!("impl_storable_unbounded: deserialize failed: {err}");
80                })
81            }
82        }
83    };
84}