1use de::read::BincodeRead;
31use error::Result;
32use serde;
33use std::io::{Read, Write};
34use std::marker::PhantomData;
35
36pub(crate) use self::endian::BincodeByteOrder;
37pub(crate) use self::int::IntEncoding;
38pub(crate) use self::internal::*;
39pub(crate) use self::limit::SizeLimit;
40pub(crate) use self::trailing::TrailingBytes;
41
42pub use self::endian::{BigEndian, LittleEndian, NativeEndian};
43pub use self::int::{FixintEncoding, VarintEncoding};
44pub use self::legacy::*;
45pub use self::limit::{Bounded, Infinite};
46pub use self::trailing::{AllowTrailing, RejectTrailing};
47
48mod endian;
49mod int;
50mod legacy;
51mod limit;
52mod trailing;
53
54#[derive(Copy, Clone)]
77pub struct DefaultOptions(Infinite);
78
79impl DefaultOptions {
80 pub fn new() -> DefaultOptions {
88 DefaultOptions(Infinite)
89 }
90}
91
92impl Default for DefaultOptions {
93 fn default() -> Self {
94 Self::new()
95 }
96}
97
98impl InternalOptions for DefaultOptions {
99 type Limit = Infinite;
100 type Endian = LittleEndian;
101 type IntEncoding = VarintEncoding;
102 type Trailing = RejectTrailing;
103
104 #[inline(always)]
105 fn limit(&mut self) -> &mut Infinite {
106 &mut self.0
107 }
108}
109
110pub trait Options: InternalOptions + Sized {
129 fn with_no_limit(self) -> WithOtherLimit<Self, Infinite> {
132 WithOtherLimit::new(self, Infinite)
133 }
134
135 fn with_limit(self, limit: u64) -> WithOtherLimit<Self, Bounded> {
137 WithOtherLimit::new(self, Bounded(limit))
138 }
139
140 fn with_little_endian(self) -> WithOtherEndian<Self, LittleEndian> {
143 WithOtherEndian::new(self)
144 }
145
146 fn with_big_endian(self) -> WithOtherEndian<Self, BigEndian> {
148 WithOtherEndian::new(self)
149 }
150
151 fn with_native_endian(self) -> WithOtherEndian<Self, NativeEndian> {
153 WithOtherEndian::new(self)
154 }
155
156 fn with_varint_encoding(self) -> WithOtherIntEncoding<Self, VarintEncoding> {
158 WithOtherIntEncoding::new(self)
159 }
160
161 fn with_fixint_encoding(self) -> WithOtherIntEncoding<Self, FixintEncoding> {
163 WithOtherIntEncoding::new(self)
164 }
165
166 fn reject_trailing_bytes(self) -> WithOtherTrailing<Self, RejectTrailing> {
168 WithOtherTrailing::new(self)
169 }
170
171 fn allow_trailing_bytes(self) -> WithOtherTrailing<Self, AllowTrailing> {
173 WithOtherTrailing::new(self)
174 }
175
176 #[inline(always)]
178 fn serialize<S: ?Sized + serde::Serialize>(self, t: &S) -> Result<Vec<u8>> {
179 ::internal::serialize(t, self)
180 }
181
182 #[inline(always)]
184 fn serialized_size<T: ?Sized + serde::Serialize>(self, t: &T) -> Result<u64> {
185 ::internal::serialized_size(t, self)
186 }
187
188 #[inline(always)]
193 fn serialize_into<W: Write, T: ?Sized + serde::Serialize>(self, w: W, t: &T) -> Result<()> {
194 ::internal::serialize_into(w, t, self)
195 }
196
197 #[inline(always)]
199 fn deserialize<'a, T: serde::Deserialize<'a>>(self, bytes: &'a [u8]) -> Result<T> {
200 ::internal::deserialize(bytes, self)
201 }
202
203 #[doc(hidden)]
205 #[inline(always)]
206 fn deserialize_in_place<'a, R, T>(self, reader: R, place: &mut T) -> Result<()>
207 where
208 R: BincodeRead<'a>,
209 T: serde::de::Deserialize<'a>,
210 {
211 ::internal::deserialize_in_place(reader, self, place)
212 }
213
214 #[inline(always)]
216 fn deserialize_seed<'a, T: serde::de::DeserializeSeed<'a>>(
217 self,
218 seed: T,
219 bytes: &'a [u8],
220 ) -> Result<T::Value> {
221 ::internal::deserialize_seed(seed, bytes, self)
222 }
223
224 #[inline(always)]
228 fn deserialize_from<R: Read, T: serde::de::DeserializeOwned>(self, reader: R) -> Result<T> {
229 ::internal::deserialize_from(reader, self)
230 }
231
232 #[inline(always)]
236 fn deserialize_from_seed<'a, R: Read, T: serde::de::DeserializeSeed<'a>>(
237 self,
238 seed: T,
239 reader: R,
240 ) -> Result<T::Value> {
241 ::internal::deserialize_from_seed(seed, reader, self)
242 }
243
244 #[inline(always)]
250 fn deserialize_from_custom<'a, R: BincodeRead<'a>, T: serde::de::DeserializeOwned>(
251 self,
252 reader: R,
253 ) -> Result<T> {
254 ::internal::deserialize_from_custom(reader, self)
255 }
256
257 #[inline(always)]
263 fn deserialize_from_custom_seed<'a, R: BincodeRead<'a>, T: serde::de::DeserializeSeed<'a>>(
264 self,
265 seed: T,
266 reader: R,
267 ) -> Result<T::Value> {
268 ::internal::deserialize_from_custom_seed(seed, reader, self)
269 }
270}
271
272impl<T: InternalOptions> Options for T {}
273
274#[derive(Clone, Copy)]
276pub struct WithOtherLimit<O: Options, L: SizeLimit> {
277 _options: O,
278 pub(crate) new_limit: L,
279}
280
281#[derive(Clone, Copy)]
283pub struct WithOtherEndian<O: Options, E: BincodeByteOrder> {
284 options: O,
285 _endian: PhantomData<E>,
286}
287
288#[derive(Clone, Copy)]
290pub struct WithOtherIntEncoding<O: Options, I: IntEncoding> {
291 options: O,
292 _length: PhantomData<I>,
293}
294
295#[derive(Clone, Copy)]
297pub struct WithOtherTrailing<O: Options, T: TrailingBytes> {
298 options: O,
299 _trailing: PhantomData<T>,
300}
301
302impl<O: Options, L: SizeLimit> WithOtherLimit<O, L> {
303 #[inline(always)]
304 pub(crate) fn new(options: O, limit: L) -> WithOtherLimit<O, L> {
305 WithOtherLimit {
306 _options: options,
307 new_limit: limit,
308 }
309 }
310}
311
312impl<O: Options, E: BincodeByteOrder> WithOtherEndian<O, E> {
313 #[inline(always)]
314 pub(crate) fn new(options: O) -> WithOtherEndian<O, E> {
315 WithOtherEndian {
316 options,
317 _endian: PhantomData,
318 }
319 }
320}
321
322impl<O: Options, I: IntEncoding> WithOtherIntEncoding<O, I> {
323 #[inline(always)]
324 pub(crate) fn new(options: O) -> WithOtherIntEncoding<O, I> {
325 WithOtherIntEncoding {
326 options,
327 _length: PhantomData,
328 }
329 }
330}
331
332impl<O: Options, T: TrailingBytes> WithOtherTrailing<O, T> {
333 #[inline(always)]
334 pub(crate) fn new(options: O) -> WithOtherTrailing<O, T> {
335 WithOtherTrailing {
336 options,
337 _trailing: PhantomData,
338 }
339 }
340}
341
342impl<O: Options, E: BincodeByteOrder + 'static> InternalOptions for WithOtherEndian<O, E> {
343 type Limit = O::Limit;
344 type Endian = E;
345 type IntEncoding = O::IntEncoding;
346 type Trailing = O::Trailing;
347 #[inline(always)]
348 fn limit(&mut self) -> &mut O::Limit {
349 self.options.limit()
350 }
351}
352
353impl<O: Options, L: SizeLimit + 'static> InternalOptions for WithOtherLimit<O, L> {
354 type Limit = L;
355 type Endian = O::Endian;
356 type IntEncoding = O::IntEncoding;
357 type Trailing = O::Trailing;
358 fn limit(&mut self) -> &mut L {
359 &mut self.new_limit
360 }
361}
362
363impl<O: Options, I: IntEncoding + 'static> InternalOptions for WithOtherIntEncoding<O, I> {
364 type Limit = O::Limit;
365 type Endian = O::Endian;
366 type IntEncoding = I;
367 type Trailing = O::Trailing;
368
369 fn limit(&mut self) -> &mut O::Limit {
370 self.options.limit()
371 }
372}
373
374impl<O: Options, T: TrailingBytes + 'static> InternalOptions for WithOtherTrailing<O, T> {
375 type Limit = O::Limit;
376 type Endian = O::Endian;
377 type IntEncoding = O::IntEncoding;
378 type Trailing = T;
379
380 fn limit(&mut self) -> &mut O::Limit {
381 self.options.limit()
382 }
383}
384
385mod internal {
386 use super::*;
387
388 pub trait InternalOptions {
389 type Limit: SizeLimit + 'static;
390 type Endian: BincodeByteOrder + 'static;
391 type IntEncoding: IntEncoding + 'static;
392 type Trailing: TrailingBytes + 'static;
393
394 fn limit(&mut self) -> &mut Self::Limit;
395 }
396
397 impl<'a, O: InternalOptions> InternalOptions for &'a mut O {
398 type Limit = O::Limit;
399 type Endian = O::Endian;
400 type IntEncoding = O::IntEncoding;
401 type Trailing = O::Trailing;
402
403 #[inline(always)]
404 fn limit(&mut self) -> &mut Self::Limit {
405 (*self).limit()
406 }
407 }
408}