-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathbytes.rs
51 lines (39 loc) · 1.22 KB
/
bytes.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
47
48
49
50
51
// Take a look at the license at the top of the repository in the LICENSE file.
use serde::Deserializer;
use super::*;
use crate::Bytes;
serialize_impl!(Bytes, Bytes(b) => b);
deserialize_impl! {
Bytes,
"a sequence of bytes",
Deserializer::deserialize_seq => match impl {
Bytes(b) => Ok(Bytes::from_owned(b.to_owned())),
ByteBuf(buf) => Ok(Bytes::from_owned(buf)),
Seq(s) => {
let mut bytes = Vec::with_capacity(min(s.size_hint().unwrap_or(0), 4096));
while let Some(byte) = s.next_element()? {
bytes.push(byte)
}
Ok(Bytes::from_owned(bytes))
},
}
}
#[cfg(test)]
mod tests {
use crate::{gformat, Bytes};
#[test]
fn serialization() {
let json = match serde_json::to_value(Bytes::from_owned(
gformat!("Lorem ipsum dolor sit amet").into_bytes(),
)) {
Ok(v) => Some(v),
Err(_) => None,
};
assert_ne!(json, None);
}
#[test]
fn deserialization() {
let json_str = r#"[76,111,114,101,109,32,105,112,115,117,109,32,100,111,108,111,114,32,115,105,116,32,97,109,101]"#;
serde_json::from_str::<Bytes>(json_str).unwrap();
}
}