-
Notifications
You must be signed in to change notification settings - Fork 103
/
serde.rs
46 lines (40 loc) · 871 Bytes
/
serde.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use starknet::{
core::{
codec::{Decode, Encode},
types::Felt,
},
macros::felt,
};
#[derive(Debug, Eq, PartialEq, Encode, Decode)]
struct CairoType {
a: Felt,
b: Option<u32>,
c: Vec<bool>,
d: [u8; 2],
}
fn main() {
let instance = CairoType {
a: felt!("123456789"),
b: Some(100),
c: vec![false, true],
d: [3, 4],
};
let mut serialized = vec![];
instance.encode(&mut serialized).unwrap();
assert_eq!(
serialized,
[
felt!("123456789"),
felt!("0"),
felt!("100"),
felt!("2"),
felt!("0"),
felt!("1"),
felt!("2"),
felt!("3"),
felt!("4"),
]
);
let restored = CairoType::decode(&serialized).unwrap();
assert_eq!(instance, restored);
}