@@ -20,13 +20,68 @@ pub trait FeltWriter {
2020
2121/// Any type that can be serialized into a series of [Felt]s. This trait corresponds to the
2222/// `serialize` function of the Cairo `Serde` trait.
23+ ///
24+ /// This trait can be derived as long as all the fields in type implement [`Encode`].
25+ ///
26+ /// # Example
27+ ///
28+ /// This example demonstrates deriving the trait and then using it to serialize an instance into
29+ /// [`Vec<Felt>`].
30+ ///
31+ /// ```rust
32+ /// use starknet_core::codec::Encode;
33+ /// # use starknet_core::types::Felt;
34+ ///
35+ /// #[derive(Encode)]
36+ /// # #[starknet(core = "starknet_core")]
37+ /// struct CairoType {
38+ /// a: u32,
39+ /// b: Option<bool>,
40+ /// }
41+ ///
42+ /// let instance = CairoType {
43+ /// a: 3,
44+ /// b: Some(true),
45+ /// };
46+ /// let mut serialized = vec![];
47+ /// instance.encode(&mut serialized);
48+ ///
49+ /// assert_eq!(vec![Felt::THREE, Felt::ZERO, Felt::ONE], serialized);
50+ /// ```
2351pub trait Encode {
2452 /// Converts the type into a list of [`Felt`] and append them into the writer.
2553 fn encode < W : FeltWriter > ( & self , writer : & mut W ) -> Result < ( ) , Error > ;
2654}
2755
2856/// Any type that can be deserialized from a series of [Felt]s. This trait corresponds to the
2957/// `deserialize` function of the Cairo `Serde` trait.
58+ ///
59+ /// This trait can be derived as long as all the fields in type implement [`Encode`].
60+ ///
61+ /// # Example
62+ ///
63+ /// This example demonstrates deriving the trait and then using it to deserialize an instance from
64+ /// [`Vec<Felt>`].
65+ ///
66+ /// ```rust
67+ /// use starknet_core::codec::Decode;
68+ /// # use starknet_core::types::Felt;
69+ ///
70+ /// #[derive(Debug, PartialEq, Eq, Decode)]
71+ /// # #[starknet(core = "starknet_core")]
72+ /// struct CairoType {
73+ /// a: u32,
74+ /// b: Option<bool>,
75+ /// }
76+ ///
77+ /// assert_eq!(
78+ /// CairoType {
79+ /// a: 3,
80+ /// b: Some(true)
81+ /// },
82+ /// CairoType::decode(&[Felt::THREE, Felt::ZERO, Felt::ONE]).unwrap()
83+ /// );
84+ /// ```
3085pub trait Decode < ' a > : Sized {
3186 /// Converts into the type from a list of [`Felt`].
3287 fn decode < T > ( reader : T ) -> Result < Self , Error >
0 commit comments