Skip to main content

candid/types/
mod.rs

1//! Provides Candid type conversion and serialization.
2//!  * `CandidType` trait converts a Rust type to Candid type `types::Type`. The implementation for user-defined data types can be derived from `candid_derive` crate.
3//!  * `Serializer` trait serializes a Rust value to Candid binary format.
4//!    We do not use Serde's `Serialize` trait because Candid requires serializing types along with the values.
5//!    This is difficult to achieve in `Serialize`, especially for enum types.
6
7use serde::ser::Error;
8
9mod impls;
10pub mod internal;
11pub mod subtype;
12pub mod type_env;
13#[cfg_attr(docsrs, doc(cfg(feature = "value")))]
14#[cfg(feature = "value")]
15pub mod value;
16
17pub use self::internal::{
18    get_type, Field, FuncMode, Function, Label, SharedLabel, Type, TypeId, TypeInner,
19};
20pub use type_env::TypeEnv;
21
22pub mod bounded_vec;
23pub mod leb128;
24#[cfg(feature = "bignum")]
25pub mod number;
26pub mod principal;
27pub mod reference;
28pub mod reserved;
29pub mod result;
30
31pub mod arc;
32pub mod rc;
33
34pub trait CandidType {
35    // memoized type derivation
36    fn ty() -> Type {
37        let id = Self::id();
38        if let Some(t) = self::internal::find_type(&id) {
39            match *t {
40                TypeInner::Unknown => TypeInner::Knot(id).into(),
41                _ => t,
42            }
43        } else {
44            self::internal::env_add(id.clone(), TypeInner::Unknown.into());
45            let t = Self::_ty();
46            self::internal::env_add(id.clone(), t.clone());
47            self::internal::env_id(id, t.clone());
48            t
49        }
50    }
51    fn id() -> TypeId {
52        TypeId::of::<Self>()
53    }
54    fn _ty() -> Type;
55    // only serialize the value encoding
56    fn idl_serialize<S>(&self, serializer: S) -> Result<(), S::Error>
57    where
58        S: Serializer;
59}
60
61pub trait Serializer: Sized {
62    type Error: Error;
63    type Compound: Compound<Error = Self::Error>;
64    fn serialize_bool(self, v: bool) -> Result<(), Self::Error>;
65    #[cfg_attr(docsrs, doc(cfg(feature = "bignum")))]
66    #[cfg(feature = "bignum")]
67    fn serialize_int(self, v: &crate::Int) -> Result<(), Self::Error>;
68    fn serialize_i128(self, v: i128) -> Result<(), Self::Error>;
69    #[cfg_attr(docsrs, doc(cfg(feature = "bignum")))]
70    #[cfg(feature = "bignum")]
71    fn serialize_nat(self, v: &crate::Nat) -> Result<(), Self::Error>;
72    fn serialize_u128(self, v: u128) -> Result<(), Self::Error>;
73    fn serialize_nat8(self, v: u8) -> Result<(), Self::Error>;
74    fn serialize_nat16(self, v: u16) -> Result<(), Self::Error>;
75    fn serialize_nat32(self, v: u32) -> Result<(), Self::Error>;
76    fn serialize_nat64(self, v: u64) -> Result<(), Self::Error>;
77    fn serialize_int8(self, v: i8) -> Result<(), Self::Error>;
78    fn serialize_int16(self, v: i16) -> Result<(), Self::Error>;
79    fn serialize_int32(self, v: i32) -> Result<(), Self::Error>;
80    fn serialize_int64(self, v: i64) -> Result<(), Self::Error>;
81    fn serialize_float32(self, v: f32) -> Result<(), Self::Error>;
82    fn serialize_float64(self, v: f64) -> Result<(), Self::Error>;
83    fn serialize_text(self, v: &str) -> Result<(), Self::Error>;
84    fn serialize_null(self, v: ()) -> Result<(), Self::Error>;
85    fn serialize_empty(self) -> Result<(), Self::Error>;
86    fn serialize_option<T>(self, v: Option<&T>) -> Result<(), Self::Error>
87    where
88        T: CandidType + ?Sized;
89    fn serialize_struct(self) -> Result<Self::Compound, Self::Error>;
90    fn serialize_vec(self, len: usize) -> Result<Self::Compound, Self::Error>;
91    fn serialize_blob(self, v: &[u8]) -> Result<(), Self::Error>;
92    fn serialize_variant(self, index: u64) -> Result<Self::Compound, Self::Error>;
93    fn serialize_principal(self, v: &[u8]) -> Result<(), Self::Error>;
94    fn serialize_function(self, v: &[u8], meth: &str) -> Result<(), Self::Error>;
95}
96
97pub trait Compound {
98    type Error;
99    fn serialize_element<T>(&mut self, v: &T) -> Result<(), Self::Error>
100    where
101        T: CandidType + ?Sized;
102    // Used for simulating serde(with = "serde_bytes"). We can remove this when specialization is stable in Rust,
103    // or generalize this function to take a closure for with.
104    #[doc(hidden)]
105    fn serialize_blob(&mut self, blob: &[u8]) -> Result<(), Self::Error>;
106}