1 #[macro_use]
2 extern crate serde_derive;
3
4 use serde::Serialize;
5 use serde_cbor::ser::SliceWrite;
6 use serde_cbor::{self, Serializer};
7
8 #[derive(Debug, PartialEq, Serialize, Deserialize)]
9 struct Example {
10 foo: Foo,
11 payload: u8,
12 }
13
14 #[derive(Debug, PartialEq, Serialize, Deserialize)]
15 struct Foo {
16 x: u8,
17 color: Color,
18 }
19
20 #[derive(Debug, PartialEq, Serialize, Deserialize)]
21 enum Color {
22 Red,
23 Blue,
24 Yellow(u8),
25 }
26
27 const EXAMPLE: Example = Example {
28 foo: Foo {
29 x: 0xAA,
30 color: Color::Yellow(40),
31 },
32 payload: 0xCC,
33 };
34
35 #[cfg(feature = "std")]
36 mod std_tests {
37 use super::*;
38
39 #[test]
test()40 fn test() {
41 let serialized = serde_cbor::ser::to_vec_packed(&EXAMPLE).expect("bennofs 1");
42 println!("{:?}", serialized);
43 let deserialized: Example = serde_cbor::from_slice(&serialized).expect("bennofs 2");
44 assert_eq!(EXAMPLE, deserialized);
45 }
46 }
47
48 #[test]
test()49 fn test() {
50 let mut slice = [0u8; 64];
51 let writer = SliceWrite::new(&mut slice);
52 let mut serializer = Serializer::new(writer).packed_format();
53 EXAMPLE.serialize(&mut serializer).unwrap();
54 let writer = serializer.into_inner();
55 let end = writer.bytes_written();
56 let slice = writer.into_inner();
57 let deserialized: Example =
58 serde_cbor::de::from_slice_with_scratch(&slice[..end], &mut []).unwrap();
59 assert_eq!(EXAMPLE, deserialized);
60 }
61