File: bennofs.rs

package info (click to toggle)
rust-serde-cbor 0.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 348 kB
  • sloc: makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,408 bytes parent folder | download | duplicates (4)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#[macro_use]
extern crate serde_derive;

use serde::Serialize;
use serde_cbor::ser::SliceWrite;
use serde_cbor::{self, Serializer};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Example {
    foo: Foo,
    payload: u8,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Foo {
    x: u8,
    color: Color,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Color {
    Red,
    Blue,
    Yellow(u8),
}

const EXAMPLE: Example = Example {
    foo: Foo {
        x: 0xAA,
        color: Color::Yellow(40),
    },
    payload: 0xCC,
};

#[cfg(feature = "std")]
mod std_tests {
    use super::*;

    #[test]
    fn test() {
        let serialized = serde_cbor::ser::to_vec_packed(&EXAMPLE).expect("bennofs 1");
        println!("{:?}", serialized);
        let deserialized: Example = serde_cbor::from_slice(&serialized).expect("bennofs 2");
        assert_eq!(EXAMPLE, deserialized);
    }
}

#[test]
fn test() {
    let mut slice = [0u8; 64];
    let writer = SliceWrite::new(&mut slice);
    let mut serializer = Serializer::new(writer).packed_format();
    EXAMPLE.serialize(&mut serializer).unwrap();
    let writer = serializer.into_inner();
    let end = writer.bytes_written();
    let slice = writer.into_inner();
    let deserialized: Example =
        serde_cbor::de::from_slice_with_scratch(&slice[..end], &mut []).unwrap();
    assert_eq!(EXAMPLE, deserialized);
}