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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use std::io::{Write, Read};
use ::SizeLimit;
pub use self::reader::{
Deserializer,
DeserializeResult,
DeserializeError,
};
pub use self::writer::{
Serializer,
SerializeResult,
SerializeError,
};
use self::writer::SizeChecker;
use serde_crate as serde;
mod reader;
mod writer;
pub fn serialize_into<W, T>(writer: &mut W, value: &T, size_limit: SizeLimit) -> SerializeResult<()>
where W: Write, T: serde::Serialize,
{
match size_limit {
SizeLimit::Infinite => { }
SizeLimit::Bounded(x) => {
let mut size_checker = SizeChecker::new(x);
try!(value.serialize(&mut size_checker))
}
}
let mut serializer = Serializer::new(writer);
serde::Serialize::serialize(value, &mut serializer)
}
pub fn serialize<T>(value: &T, size_limit: SizeLimit) -> SerializeResult<Vec<u8>>
where T: serde::Serialize,
{
let mut writer = match size_limit {
SizeLimit::Bounded(size_limit) => {
let actual_size = match serialized_size_bounded(value, size_limit) {
Some(actual_size) => actual_size,
None => { return Err(SerializeError::SizeLimit); }
};
Vec::with_capacity(actual_size as usize)
}
SizeLimit::Infinite => Vec::new()
};
try!(serialize_into(&mut writer, value, SizeLimit::Infinite));
Ok(writer)
}
pub fn serialized_size<T: serde::Serialize>(value: &T) -> u64 {
use std::u64::MAX;
let mut size_checker = SizeChecker::new(MAX);
value.serialize(&mut size_checker).ok();
size_checker.written
}
pub fn serialized_size_bounded<T: serde::Serialize>(value: &T, max: u64) -> Option<u64> {
let mut size_checker = SizeChecker::new(max);
value.serialize(&mut size_checker).ok().map(|_| size_checker.written)
}
pub fn deserialize_from<R, T>(reader: &mut R, size_limit: SizeLimit) -> DeserializeResult<T>
where R: Read,
T: serde::Deserialize,
{
let mut deserializer = Deserializer::new(reader, size_limit);
serde::Deserialize::deserialize(&mut deserializer)
}
pub fn deserialize<T>(bytes: &[u8]) -> DeserializeResult<T>
where T: serde::Deserialize,
{
let mut reader = bytes;
deserialize_from(&mut reader, SizeLimit::Infinite)
}