Debian Codegraph @

third_party/rust/serde_bytes/tests/test_derive.rs

raw
1 #![allow(clippy::derive_partial_eq_without_eq, clippy::ref_option_ref)]
2
3 use serde_bytes::{ByteBuf, Bytes};
4 use serde_derive::{Deserialize, Serialize};
5 use serde_test::{assert_tokens, Token};
6 use std::borrow::Cow;
7
8 #[derive(Serialize, Deserialize, PartialEq, Debug)]
9 struct Test<'a> {
10 #[serde(with = "serde_bytes")]
11 slice: &'a [u8],
12
13 #[serde(with = "serde_bytes")]
14 vec: Vec<u8>,
15
16 #[serde(with = "serde_bytes")]
17 bytes: &'a Bytes,
18
19 #[serde(with = "serde_bytes")]
20 byte_buf: ByteBuf,
21
22 #[serde(with = "serde_bytes")]
23 cow_slice: Cow<'a, [u8]>,
24
25 #[serde(with = "serde_bytes")]
26 cow_bytes: Cow<'a, Bytes>,
27
28 #[serde(with = "serde_bytes")]
29 boxed_slice: Box<[u8]>,
30
31 #[serde(with = "serde_bytes")]
32 boxed_bytes: Box<Bytes>,
33
34 #[serde(with = "serde_bytes")]
35 opt_slice: Option<&'a [u8]>,
36
37 #[serde(with = "serde_bytes")]
38 opt_vec: Option<Vec<u8>>,
39
40 #[serde(with = "serde_bytes")]
41 opt_cow_slice: Option<Cow<'a, [u8]>>,
42 }
43
44 #[derive(Serialize)]
45 struct Dst {
46 #[serde(with = "serde_bytes")]
47 bytes: [u8],
48 }
49
50 #[test]
51 fn test() {
52 let test = Test {
53 slice: b"...",
54 vec: b"...".to_vec(),
55 bytes: Bytes::new(b"..."),
56 byte_buf: ByteBuf::from(b"...".as_ref()),
57 cow_slice: Cow::Borrowed(b"..."),
58 cow_bytes: Cow::Borrowed(Bytes::new(b"...")),
59 boxed_slice: b"...".to_vec().into_boxed_slice(),
60 boxed_bytes: ByteBuf::from(b"...".as_ref()).into_boxed_bytes(),
61 opt_slice: Some(b"..."),
62 opt_vec: Some(b"...".to_vec()),
63 opt_cow_slice: Some(Cow::Borrowed(b"...")),
64 };
65
66 assert_tokens(
67 &test,
68 &[
69 Token::Struct {
70 name: "Test",
71 len: 11,
72 },
73 Token::Str("slice"),
74 Token::BorrowedBytes(b"..."),
75 Token::Str("vec"),
76 Token::Bytes(b"..."),
77 Token::Str("bytes"),
78 Token::BorrowedBytes(b"..."),
79 Token::Str("byte_buf"),
80 Token::Bytes(b"..."),
81 Token::Str("cow_slice"),
82 Token::BorrowedBytes(b"..."),
83 Token::Str("cow_bytes"),
84 Token::BorrowedBytes(b"..."),
85 Token::Str("boxed_slice"),
86 Token::Bytes(b"..."),
87 Token::Str("boxed_bytes"),
88 Token::Bytes(b"..."),
89 Token::Str("opt_slice"),
90 Token::Some,
91 Token::BorrowedBytes(b"..."),
92 Token::Str("opt_vec"),
93 Token::Some,
94 Token::Bytes(b"..."),
95 Token::Str("opt_cow_slice"),
96 Token::Some,
97 Token::BorrowedBytes(b"..."),
98 Token::StructEnd,
99 ],
100 );
101 }