1
// begin rustme snippet: example
2
use serde_derive::{Deserialize, Serialize};
3
5
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Default)]
4
pub struct User {
5
    id: u64,
6
    name: String,
7
}
8

            
9
1
fn main() {
10
1
    // Pot's main space saving feature is being able to reuse previously encoded
11
1
    // fields. Pot also supports persisting "symbol maps" in many powerful ways.
12
1
    // This example shows how to compute a symbol map that can be pre-shared to
13
1
    // keep payloads smaller.
14
1
    let mut preshared_map = pot::ser::SymbolMap::new();
15
1
    // Load the symbols from an instance of `User`.
16
1
    preshared_map.populate_from(&User::default()).unwrap();
17
1
    println!("Preshared symbols: {preshared_map:?}");
18
1

            
19
1
    let original_user = User {
20
1
        id: 42,
21
1
        name: String::from("ecton"),
22
1
    };
23
1
    let encoded_without_map = pot::to_vec(&original_user).unwrap();
24
1
    let encoded_with_map = preshared_map.serialize_to_vec(&original_user).unwrap();
25
1
    println!(
26
1
        "Default User encoded without map: {} bytes",
27
1
        encoded_without_map.len()
28
1
    );
29
1
    println!(
30
1
        "Default User encoded with map:    {} bytes",
31
1
        encoded_with_map.len()
32
1
    );
33
1
    assert!(encoded_with_map.len() < encoded_without_map.len());
34

            
35
    // Serialize the map and "send" it to the receiver.
36
1
    let preshared_map_bytes = pot::to_vec(&preshared_map).unwrap();
37
1

            
38
1
    // Deserialize the symbol map.
39
1
    let mut deserializer_map: pot::de::SymbolMap = pot::from_slice(&preshared_map_bytes).unwrap();
40
1
    // Deserialize the payload using the map.
41
1
    let user: User = deserializer_map
42
1
        .deserialize_slice(&encoded_with_map)
43
1
        .unwrap();
44
1
    assert_eq!(user, original_user);
45
1
}
46

            
47
1
#[test]
48
1
fn runs() {
49
1
    main();
50
1
}