/rust/registry/src/index.crates.io-6f17d22bba15001f/bincode-1.3.3/src/config/legacy.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use std::io::{Read, Write}; |
2 | | |
3 | | use self::EndianOption::*; |
4 | | use self::LimitOption::*; |
5 | | use super::{DefaultOptions, Options}; |
6 | | use de::read::BincodeRead; |
7 | | use error::Result; |
8 | | use serde; |
9 | | |
10 | | /// A configuration builder whose options Bincode will use |
11 | | /// while serializing and deserializing. |
12 | | /// |
13 | | /// ### Options |
14 | | /// Endianness: The endianness with which multi-byte integers will be read/written. *default: little endian* |
15 | | /// Limit: The maximum number of bytes that will be read/written in a bincode serialize/deserialize. *default: unlimited* |
16 | | /// |
17 | | /// ### Byte Limit Details |
18 | | /// The purpose of byte-limiting is to prevent Denial-Of-Service attacks whereby malicious attackers get bincode |
19 | | /// deserialization to crash your process by allocating too much memory or keeping a connection open for too long. |
20 | | /// |
21 | | /// When a byte limit is set, bincode will return `Err` on any deserialization that goes over the limit, or any |
22 | | /// serialization that goes over the limit. |
23 | | #[derive(Clone, Debug)] |
24 | | #[deprecated( |
25 | | since = "1.3.0", |
26 | | note = "please use the `DefaultOptions`/`Options` system instead" |
27 | | )] |
28 | | pub struct Config { |
29 | | limit: LimitOption, |
30 | | endian: EndianOption, |
31 | | } |
32 | | |
33 | | #[derive(Clone, Copy, Debug)] |
34 | | enum LimitOption { |
35 | | Unlimited, |
36 | | Limited(u64), |
37 | | } |
38 | | |
39 | | #[derive(Clone, Copy, Debug)] |
40 | | enum EndianOption { |
41 | | Big, |
42 | | Little, |
43 | | Native, |
44 | | } |
45 | | |
46 | | macro_rules! config_map { |
47 | | ($self:expr, $opts:ident => $call:expr) => { |
48 | | match ($self.limit, $self.endian) { |
49 | | (Unlimited, Little) => { |
50 | | let $opts = DefaultOptions::new() |
51 | | .with_fixint_encoding() |
52 | | .allow_trailing_bytes() |
53 | | .with_no_limit() |
54 | | .with_little_endian(); |
55 | | $call |
56 | | } |
57 | | (Unlimited, Big) => { |
58 | | let $opts = DefaultOptions::new() |
59 | | .with_fixint_encoding() |
60 | | .allow_trailing_bytes() |
61 | | .with_no_limit() |
62 | | .with_big_endian(); |
63 | | $call |
64 | | } |
65 | | (Unlimited, Native) => { |
66 | | let $opts = DefaultOptions::new() |
67 | | .with_fixint_encoding() |
68 | | .allow_trailing_bytes() |
69 | | .with_no_limit() |
70 | | .with_native_endian(); |
71 | | $call |
72 | | } |
73 | | |
74 | | (Limited(l), Little) => { |
75 | | let $opts = DefaultOptions::new() |
76 | | .with_fixint_encoding() |
77 | | .allow_trailing_bytes() |
78 | | .with_limit(l) |
79 | | .with_little_endian(); |
80 | | $call |
81 | | } |
82 | | (Limited(l), Big) => { |
83 | | let $opts = DefaultOptions::new() |
84 | | .with_fixint_encoding() |
85 | | .allow_trailing_bytes() |
86 | | .with_limit(l) |
87 | | .with_big_endian(); |
88 | | $call |
89 | | } |
90 | | (Limited(l), Native) => { |
91 | | let $opts = DefaultOptions::new() |
92 | | .with_fixint_encoding() |
93 | | .allow_trailing_bytes() |
94 | | .with_limit(l) |
95 | | .with_native_endian(); |
96 | | $call |
97 | | } |
98 | | } |
99 | | }; |
100 | | } |
101 | | |
102 | | impl Config { |
103 | | #[inline(always)] |
104 | 5.15k | pub(crate) fn new() -> Config { |
105 | 5.15k | Config { |
106 | 5.15k | limit: LimitOption::Unlimited, |
107 | 5.15k | endian: EndianOption::Little, |
108 | 5.15k | } |
109 | 5.15k | } |
110 | | |
111 | | /// Sets the byte limit to be unlimited. |
112 | | /// This is the default. |
113 | | #[inline(always)] |
114 | 0 | pub fn no_limit(&mut self) -> &mut Self { |
115 | 0 | self.limit = LimitOption::Unlimited; |
116 | 0 | self |
117 | 0 | } |
118 | | |
119 | | /// Sets the byte limit to `limit`. |
120 | | #[inline(always)] |
121 | 5.15k | pub fn limit(&mut self, limit: u64) -> &mut Self { |
122 | 5.15k | self.limit = LimitOption::Limited(limit); |
123 | 5.15k | self |
124 | 5.15k | } |
125 | | |
126 | | /// Sets the endianness to little-endian |
127 | | /// This is the default. |
128 | | #[inline(always)] |
129 | 0 | pub fn little_endian(&mut self) -> &mut Self { |
130 | 0 | self.endian = EndianOption::Little; |
131 | 0 | self |
132 | 0 | } |
133 | | |
134 | | /// Sets the endianness to big-endian |
135 | | #[inline(always)] |
136 | 0 | pub fn big_endian(&mut self) -> &mut Self { |
137 | 0 | self.endian = EndianOption::Big; |
138 | 0 | self |
139 | 0 | } |
140 | | |
141 | | /// Sets the endianness to the the machine-native endianness |
142 | | #[inline(always)] |
143 | 0 | pub fn native_endian(&mut self) -> &mut Self { |
144 | 0 | self.endian = EndianOption::Native; |
145 | 0 | self |
146 | 0 | } |
147 | | |
148 | | /// Serializes a serializable object into a `Vec` of bytes using this configuration |
149 | | #[inline(always)] |
150 | 0 | pub fn serialize<T: ?Sized + serde::Serialize>(&self, t: &T) -> Result<Vec<u8>> { |
151 | 0 | config_map!(self, opts => ::internal::serialize(t, opts)) |
152 | 0 | } |
153 | | |
154 | | /// Returns the size that an object would be if serialized using Bincode with this configuration |
155 | | #[inline(always)] |
156 | 0 | pub fn serialized_size<T: ?Sized + serde::Serialize>(&self, t: &T) -> Result<u64> { |
157 | 0 | config_map!(self, opts => ::internal::serialized_size(t, opts)) |
158 | 0 | } |
159 | | |
160 | | /// Serializes an object directly into a `Writer` using this configuration |
161 | | /// |
162 | | /// If the serialization would take more bytes than allowed by the size limit, an error |
163 | | /// is returned and *no bytes* will be written into the `Writer` |
164 | | #[inline(always)] |
165 | 0 | pub fn serialize_into<W: Write, T: ?Sized + serde::Serialize>( |
166 | 0 | &self, |
167 | 0 | w: W, |
168 | 0 | t: &T, |
169 | 0 | ) -> Result<()> { |
170 | 0 | config_map!(self, opts => ::internal::serialize_into(w, t, opts)) |
171 | 0 | } |
172 | | |
173 | | /// Deserializes a slice of bytes into an instance of `T` using this configuration |
174 | | #[inline(always)] |
175 | 0 | pub fn deserialize<'a, T: serde::Deserialize<'a>>(&self, bytes: &'a [u8]) -> Result<T> { |
176 | 0 | config_map!(self, opts => ::internal::deserialize(bytes, opts)) |
177 | 0 | } |
178 | | |
179 | | /// TODO: document |
180 | | #[doc(hidden)] |
181 | | #[inline(always)] |
182 | 0 | pub fn deserialize_in_place<'a, R, T>(&self, reader: R, place: &mut T) -> Result<()> |
183 | 0 | where |
184 | 0 | R: BincodeRead<'a>, |
185 | 0 | T: serde::de::Deserialize<'a>, |
186 | 0 | { |
187 | 0 | config_map!(self, opts => ::internal::deserialize_in_place(reader, opts, place)) |
188 | 0 | } |
189 | | |
190 | | /// Deserializes a slice of bytes with state `seed` using this configuration. |
191 | | #[inline(always)] |
192 | 0 | pub fn deserialize_seed<'a, T: serde::de::DeserializeSeed<'a>>( |
193 | 0 | &self, |
194 | 0 | seed: T, |
195 | 0 | bytes: &'a [u8], |
196 | 0 | ) -> Result<T::Value> { |
197 | 0 | config_map!(self, opts => ::internal::deserialize_seed(seed, bytes, opts)) |
198 | 0 | } |
199 | | |
200 | | /// Deserializes an object directly from a `Read`er using this configuration |
201 | | /// |
202 | | /// If this returns an `Error`, `reader` may be in an invalid state. |
203 | | #[inline(always)] |
204 | 5.15k | pub fn deserialize_from<R: Read, T: serde::de::DeserializeOwned>( |
205 | 5.15k | &self, |
206 | 5.15k | reader: R, |
207 | 5.15k | ) -> Result<T> { |
208 | 5.15k | config_map!(self, opts => ::internal::deserialize_from(reader, opts)) |
209 | 5.15k | } <bincode::config::legacy::Config>::deserialize_from::<&[u8], compat::AllTypes> Line | Count | Source | 204 | 5.15k | pub fn deserialize_from<R: Read, T: serde::de::DeserializeOwned>( | 205 | 5.15k | &self, | 206 | 5.15k | reader: R, | 207 | 5.15k | ) -> Result<T> { | 208 | 5.15k | config_map!(self, opts => ::internal::deserialize_from(reader, opts)) | 209 | 5.15k | } |
Unexecuted instantiation: <bincode::config::legacy::Config>::deserialize_from::<_, _> |
210 | | |
211 | | /// Deserializes an object directly from a `Read`er with state `seed` using this configuration |
212 | | /// |
213 | | /// If this returns an `Error`, `reader` may be in an invalid state. |
214 | | #[inline(always)] |
215 | 0 | pub fn deserialize_from_seed<'a, R: Read, T: serde::de::DeserializeSeed<'a>>( |
216 | 0 | &self, |
217 | 0 | seed: T, |
218 | 0 | reader: R, |
219 | 0 | ) -> Result<T::Value> { |
220 | 0 | config_map!(self, opts => ::internal::deserialize_from_seed(seed, reader, opts)) |
221 | 0 | } |
222 | | |
223 | | /// Deserializes an object from a custom `BincodeRead`er using the default configuration. |
224 | | /// It is highly recommended to use `deserialize_from` unless you need to implement |
225 | | /// `BincodeRead` for performance reasons. |
226 | | /// |
227 | | /// If this returns an `Error`, `reader` may be in an invalid state. |
228 | | #[inline(always)] |
229 | 0 | pub fn deserialize_from_custom<'a, R: BincodeRead<'a>, T: serde::de::DeserializeOwned>( |
230 | 0 | &self, |
231 | 0 | reader: R, |
232 | 0 | ) -> Result<T> { |
233 | 0 | config_map!(self, opts => ::internal::deserialize_from_custom(reader, opts)) |
234 | 0 | } |
235 | | |
236 | | /// Deserializes an object from a custom `BincodeRead`er with state `seed` using the default |
237 | | /// configuration. It is highly recommended to use `deserialize_from` unless you need to |
238 | | /// implement `BincodeRead` for performance reasons. |
239 | | /// |
240 | | /// If this returns an `Error`, `reader` may be in an invalid state. |
241 | | #[inline(always)] |
242 | 0 | pub fn deserialize_from_custom_seed< |
243 | 0 | 'a, |
244 | 0 | R: BincodeRead<'a>, |
245 | 0 | T: serde::de::DeserializeSeed<'a>, |
246 | 0 | >( |
247 | 0 | &self, |
248 | 0 | seed: T, |
249 | 0 | reader: R, |
250 | 0 | ) -> Result<T::Value> { |
251 | 0 | config_map!(self, opts => ::internal::deserialize_from_custom_seed(seed, reader, opts)) |
252 | 0 | } |
253 | | } |