1
#![doc = include_str!("./.crate-docs.md")]
2
#![forbid(unsafe_code)]
3
#![warn(
4
    clippy::cargo,
5
    missing_docs,
6
    // clippy::missing_docs_in_private_items,
7
    clippy::pedantic,
8
    future_incompatible,
9
    rust_2018_idioms,
10
)]
11
#![allow(
12
    clippy::missing_errors_doc, // TODO clippy::missing_errors_doc
13
    clippy::option_if_let_else,
14
)]
15

            
16
use std::io::{Read, Write};
17

            
18
pub use bincode;
19
use bincode::{
20
    config::{
21
        AllowTrailing, BigEndian, Bounded, FixintEncoding, Infinite, LittleEndian, NativeEndian,
22
        RejectTrailing, VarintEncoding, WithOtherEndian, WithOtherIntEncoding, WithOtherLimit,
23
        WithOtherTrailing,
24
    },
25
    DefaultOptions, Options,
26
};
27
use serde::{de::DeserializeOwned, ser::Error, Deserialize, Serialize};
28
pub use transmog;
29
use transmog::{BorrowedDeserializer, Format, OwnedDeserializer};
30

            
31
/// Bincode implementor of [`Format`] with default options.
32
45
#[derive(Clone)]
33
#[must_use]
34
pub struct Bincode {
35
    limit: Option<u64>,
36
    endian: Endian,
37
    integer_encoding: IntegerEncoding,
38
    reject_trailing_bytes: bool,
39
}
40

            
41
45
#[derive(Clone, Copy)]
42
enum Endian {
43
    Little,
44
    Big,
45
    Native,
46
}
47

            
48
45
#[derive(Clone, Copy)]
49
enum IntegerEncoding {
50
    Fixed,
51
    Variable,
52
}
53

            
54
impl Default for Bincode {
55
    /// Returns a `Bincode` instance initialized using the equivalent of [`DefaultOptions`].
56
35
    fn default() -> Self {
57
35
        Self {
58
35
            limit: None,
59
35
            endian: Endian::Little,
60
35
            integer_encoding: IntegerEncoding::Variable,
61
35
            reject_trailing_bytes: true,
62
35
        }
63
35
    }
64
}
65

            
66
impl Bincode {
67
    /// Returns a `Bincode` instance initialized using the equivalent settings
68
    /// that [`bincode::serialize`], [`bincode::deserialize`],
69
    /// [`bincode::serialize_into`], and [`bincode::deserialize_from`] use. See
70
    /// [`bincode::config`](mod@bincode::config) for more information.
71
3
    pub fn legacy_default() -> Self {
72
3
        Self::default()
73
3
            .fixed_integer_encoding()
74
3
            .allow_trailing_bytes()
75
3
    }
76

            
77
    /// Configures no byte limit. See [`Infinite`] for more information.
78
12
    pub fn no_limit(mut self) -> Self {
79
12
        self.limit = None;
80
12
        self
81
12
    }
82

            
83
    /// Configures bincode to restrict encoding and decoding to `byte_limit`. See [`Bounded`] for more information.
84
12
    pub fn limit(mut self, byte_limit: u64) -> Self {
85
12
        self.limit = Some(byte_limit);
86
12
        self
87
12
    }
88

            
89
    /// Configures big-endian encoding. See [`BigEndian`] for more information.
90
8
    pub fn big_endian(mut self) -> Self {
91
8
        self.endian = Endian::Big;
92
8
        self
93
8
    }
94

            
95
    /// Configures little-endian encoding. See [`LittleEndian`] for more information.
96
8
    pub fn little_endian(mut self) -> Self {
97
8
        self.endian = Endian::Little;
98
8
        self
99
8
    }
100

            
101
    /// Configures native-endian encoding. See [`NativeEndian`] for more information.
102
8
    pub fn native_endian(mut self) -> Self {
103
8
        self.endian = Endian::Native;
104
8
        self
105
8
    }
106

            
107
    /// Configures variable length integer encoding. See [`VarintEncoding`] for more information.
108
12
    pub fn variable_integer_encoding(mut self) -> Self {
109
12
        self.integer_encoding = IntegerEncoding::Variable;
110
12
        self
111
12
    }
112

            
113
    /// Configures fixed length integer encoding. See [`FixintEncoding`] for more information.
114
15
    pub fn fixed_integer_encoding(mut self) -> Self {
115
15
        self.integer_encoding = IntegerEncoding::Fixed;
116
15
        self
117
15
    }
118

            
119
    /// Configures Bincode to allow trailing bytes when deserializing. See [`AllowTrailing`] for more information.
120
15
    pub fn allow_trailing_bytes(mut self) -> Self {
121
15
        self.reject_trailing_bytes = false;
122
15
        self
123
15
    }
124

            
125
    /// Configures Bincode to reject trailing bytes when deserializing. See [`RejectTrailing`] for more information.
126
12
    pub fn reject_trailing_bytes(mut self) -> Self {
127
12
        self.reject_trailing_bytes = true;
128
12
        self
129
12
    }
130
}
131

            
132
impl From<&Bincode> for BincodeOptions {
133
    #[allow(clippy::too_many_lines)]
134
    fn from(settings: &Bincode) -> Self {
135
        match (
136
983253
            settings.limit,
137
983253
            settings.endian,
138
983253
            settings.integer_encoding,
139
983253
            settings.reject_trailing_bytes,
140
        ) {
141
            (None, Endian::Little, IntegerEncoding::Fixed, true) => {
142
7
                Self::UnlimitedLittleFixintReject(
143
7
                    DefaultOptions::default()
144
7
                        .reject_trailing_bytes()
145
7
                        .with_fixint_encoding()
146
7
                        .with_little_endian()
147
7
                        .with_no_limit(),
148
7
                )
149
            }
150
            (None, Endian::Little, IntegerEncoding::Fixed, false) => {
151
16
                Self::UnlimitedLittleFixintAllow(
152
16
                    DefaultOptions::default()
153
16
                        .allow_trailing_bytes()
154
16
                        .with_fixint_encoding()
155
16
                        .with_little_endian()
156
16
                        .with_no_limit(),
157
16
                )
158
            }
159
            (None, Endian::Little, IntegerEncoding::Variable, true) => {
160
983083
                Self::UnlimitedLittleVarintReject(
161
983083
                    DefaultOptions::default()
162
983083
                        .reject_trailing_bytes()
163
983083
                        .with_varint_encoding()
164
983083
                        .with_little_endian()
165
983083
                        .with_no_limit(),
166
983083
                )
167
            }
168
            (None, Endian::Little, IntegerEncoding::Variable, false) => {
169
7
                Self::UnlimitedLittleVarintAllow(
170
7
                    DefaultOptions::default()
171
7
                        .allow_trailing_bytes()
172
7
                        .with_varint_encoding()
173
7
                        .with_little_endian()
174
7
                        .with_no_limit(),
175
7
                )
176
            }
177
7
            (None, Endian::Big, IntegerEncoding::Fixed, true) => Self::UnlimitedBigFixintReject(
178
7
                DefaultOptions::default()
179
7
                    .reject_trailing_bytes()
180
7
                    .with_fixint_encoding()
181
7
                    .with_big_endian()
182
7
                    .with_no_limit(),
183
7
            ),
184
7
            (None, Endian::Big, IntegerEncoding::Fixed, false) => Self::UnlimitedBigFixintAllow(
185
7
                DefaultOptions::default()
186
7
                    .allow_trailing_bytes()
187
7
                    .with_fixint_encoding()
188
7
                    .with_big_endian()
189
7
                    .with_no_limit(),
190
7
            ),
191
7
            (None, Endian::Big, IntegerEncoding::Variable, true) => Self::UnlimitedBigVarintReject(
192
7
                DefaultOptions::default()
193
7
                    .reject_trailing_bytes()
194
7
                    .with_varint_encoding()
195
7
                    .with_big_endian()
196
7
                    .with_no_limit(),
197
7
            ),
198
7
            (None, Endian::Big, IntegerEncoding::Variable, false) => Self::UnlimitedBigVarintAllow(
199
7
                DefaultOptions::default()
200
7
                    .allow_trailing_bytes()
201
7
                    .with_varint_encoding()
202
7
                    .with_big_endian()
203
7
                    .with_no_limit(),
204
7
            ),
205
            (None, Endian::Native, IntegerEncoding::Fixed, true) => {
206
7
                Self::UnlimitedNativeFixintReject(
207
7
                    DefaultOptions::default()
208
7
                        .reject_trailing_bytes()
209
7
                        .with_fixint_encoding()
210
7
                        .with_native_endian()
211
7
                        .with_no_limit(),
212
7
                )
213
            }
214
            (None, Endian::Native, IntegerEncoding::Fixed, false) => {
215
7
                Self::UnlimitedNativeFixintAllow(
216
7
                    DefaultOptions::default()
217
7
                        .allow_trailing_bytes()
218
7
                        .with_fixint_encoding()
219
7
                        .with_native_endian()
220
7
                        .with_no_limit(),
221
7
                )
222
            }
223
            (None, Endian::Native, IntegerEncoding::Variable, true) => {
224
7
                Self::UnlimitedNativeVarintReject(
225
7
                    DefaultOptions::default()
226
7
                        .reject_trailing_bytes()
227
7
                        .with_varint_encoding()
228
7
                        .with_native_endian()
229
7
                        .with_no_limit(),
230
7
                )
231
            }
232
            (None, Endian::Native, IntegerEncoding::Variable, false) => {
233
7
                Self::UnlimitedNativeVarintAllow(
234
7
                    DefaultOptions::default()
235
7
                        .allow_trailing_bytes()
236
7
                        .with_varint_encoding()
237
7
                        .with_native_endian()
238
7
                        .with_no_limit(),
239
7
                )
240
            }
241
7
            (Some(limit), Endian::Little, IntegerEncoding::Fixed, true) => {
242
7
                Self::LimitedLittleFixintReject(
243
7
                    DefaultOptions::default()
244
7
                        .reject_trailing_bytes()
245
7
                        .with_fixint_encoding()
246
7
                        .with_little_endian()
247
7
                        .with_limit(limit),
248
7
                )
249
            }
250
7
            (Some(limit), Endian::Little, IntegerEncoding::Fixed, false) => {
251
7
                Self::LimitedLittleFixintAllow(
252
7
                    DefaultOptions::default()
253
7
                        .allow_trailing_bytes()
254
7
                        .with_fixint_encoding()
255
7
                        .with_little_endian()
256
7
                        .with_limit(limit),
257
7
                )
258
            }
259
7
            (Some(limit), Endian::Little, IntegerEncoding::Variable, true) => {
260
7
                Self::LimitedLittleVarintReject(
261
7
                    DefaultOptions::default()
262
7
                        .reject_trailing_bytes()
263
7
                        .with_varint_encoding()
264
7
                        .with_little_endian()
265
7
                        .with_limit(limit),
266
7
                )
267
            }
268
7
            (Some(limit), Endian::Little, IntegerEncoding::Variable, false) => {
269
7
                Self::LimitedLittleVarintAllow(
270
7
                    DefaultOptions::default()
271
7
                        .allow_trailing_bytes()
272
7
                        .with_varint_encoding()
273
7
                        .with_little_endian()
274
7
                        .with_limit(limit),
275
7
                )
276
            }
277
7
            (Some(limit), Endian::Big, IntegerEncoding::Fixed, true) => {
278
7
                Self::LimitedBigFixintReject(
279
7
                    DefaultOptions::default()
280
7
                        .reject_trailing_bytes()
281
7
                        .with_fixint_encoding()
282
7
                        .with_big_endian()
283
7
                        .with_limit(limit),
284
7
                )
285
            }
286
7
            (Some(limit), Endian::Big, IntegerEncoding::Fixed, false) => {
287
7
                Self::LimitedBigFixintAllow(
288
7
                    DefaultOptions::default()
289
7
                        .allow_trailing_bytes()
290
7
                        .with_fixint_encoding()
291
7
                        .with_big_endian()
292
7
                        .with_limit(limit),
293
7
                )
294
            }
295
7
            (Some(limit), Endian::Big, IntegerEncoding::Variable, true) => {
296
7
                Self::LimitedBigVarintReject(
297
7
                    DefaultOptions::default()
298
7
                        .reject_trailing_bytes()
299
7
                        .with_varint_encoding()
300
7
                        .with_big_endian()
301
7
                        .with_limit(limit),
302
7
                )
303
            }
304
7
            (Some(limit), Endian::Big, IntegerEncoding::Variable, false) => {
305
7
                Self::LimitedBigVarintAllow(
306
7
                    DefaultOptions::default()
307
7
                        .allow_trailing_bytes()
308
7
                        .with_varint_encoding()
309
7
                        .with_big_endian()
310
7
                        .with_limit(limit),
311
7
                )
312
            }
313
7
            (Some(limit), Endian::Native, IntegerEncoding::Fixed, true) => {
314
7
                Self::LimitedNativeFixintReject(
315
7
                    DefaultOptions::default()
316
7
                        .reject_trailing_bytes()
317
7
                        .with_fixint_encoding()
318
7
                        .with_native_endian()
319
7
                        .with_limit(limit),
320
7
                )
321
            }
322
7
            (Some(limit), Endian::Native, IntegerEncoding::Fixed, false) => {
323
7
                Self::LimitedNativeFixintAllow(
324
7
                    DefaultOptions::default()
325
7
                        .allow_trailing_bytes()
326
7
                        .with_fixint_encoding()
327
7
                        .with_native_endian()
328
7
                        .with_limit(limit),
329
7
                )
330
            }
331
7
            (Some(limit), Endian::Native, IntegerEncoding::Variable, true) => {
332
7
                Self::LimitedNativeVarintReject(
333
7
                    DefaultOptions::default()
334
7
                        .reject_trailing_bytes()
335
7
                        .with_varint_encoding()
336
7
                        .with_native_endian()
337
7
                        .with_limit(limit),
338
7
                )
339
            }
340
7
            (Some(limit), Endian::Native, IntegerEncoding::Variable, false) => {
341
7
                Self::LimitedNativeVarintAllow(
342
7
                    DefaultOptions::default()
343
7
                        .allow_trailing_bytes()
344
7
                        .with_varint_encoding()
345
7
                        .with_native_endian()
346
7
                        .with_limit(limit),
347
7
                )
348
            }
349
        }
350
983253
    }
351
}
352

            
353
#[allow(clippy::type_complexity)]
354
enum BincodeOptions {
355
    UnlimitedLittleVarintReject(
356
        WithOtherLimit<
357
            WithOtherEndian<
358
                WithOtherIntEncoding<
359
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
360
                    VarintEncoding,
361
                >,
362
                LittleEndian,
363
            >,
364
            Infinite,
365
        >,
366
    ),
367
    UnlimitedLittleVarintAllow(
368
        WithOtherLimit<
369
            WithOtherEndian<
370
                WithOtherIntEncoding<
371
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
372
                    VarintEncoding,
373
                >,
374
                LittleEndian,
375
            >,
376
            Infinite,
377
        >,
378
    ),
379
    UnlimitedLittleFixintReject(
380
        WithOtherLimit<
381
            WithOtherEndian<
382
                WithOtherIntEncoding<
383
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
384
                    FixintEncoding,
385
                >,
386
                LittleEndian,
387
            >,
388
            Infinite,
389
        >,
390
    ),
391
    UnlimitedLittleFixintAllow(
392
        WithOtherLimit<
393
            WithOtherEndian<
394
                WithOtherIntEncoding<
395
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
396
                    FixintEncoding,
397
                >,
398
                LittleEndian,
399
            >,
400
            Infinite,
401
        >,
402
    ),
403
    UnlimitedBigVarintReject(
404
        WithOtherLimit<
405
            WithOtherEndian<
406
                WithOtherIntEncoding<
407
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
408
                    VarintEncoding,
409
                >,
410
                BigEndian,
411
            >,
412
            Infinite,
413
        >,
414
    ),
415
    UnlimitedBigVarintAllow(
416
        WithOtherLimit<
417
            WithOtherEndian<
418
                WithOtherIntEncoding<
419
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
420
                    VarintEncoding,
421
                >,
422
                BigEndian,
423
            >,
424
            Infinite,
425
        >,
426
    ),
427
    UnlimitedBigFixintReject(
428
        WithOtherLimit<
429
            WithOtherEndian<
430
                WithOtherIntEncoding<
431
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
432
                    FixintEncoding,
433
                >,
434
                BigEndian,
435
            >,
436
            Infinite,
437
        >,
438
    ),
439
    UnlimitedBigFixintAllow(
440
        WithOtherLimit<
441
            WithOtherEndian<
442
                WithOtherIntEncoding<
443
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
444
                    FixintEncoding,
445
                >,
446
                BigEndian,
447
            >,
448
            Infinite,
449
        >,
450
    ),
451
    UnlimitedNativeVarintReject(
452
        WithOtherLimit<
453
            WithOtherEndian<
454
                WithOtherIntEncoding<
455
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
456
                    VarintEncoding,
457
                >,
458
                NativeEndian,
459
            >,
460
            Infinite,
461
        >,
462
    ),
463
    UnlimitedNativeVarintAllow(
464
        WithOtherLimit<
465
            WithOtherEndian<
466
                WithOtherIntEncoding<
467
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
468
                    VarintEncoding,
469
                >,
470
                NativeEndian,
471
            >,
472
            Infinite,
473
        >,
474
    ),
475
    UnlimitedNativeFixintReject(
476
        WithOtherLimit<
477
            WithOtherEndian<
478
                WithOtherIntEncoding<
479
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
480
                    FixintEncoding,
481
                >,
482
                NativeEndian,
483
            >,
484
            Infinite,
485
        >,
486
    ),
487
    UnlimitedNativeFixintAllow(
488
        WithOtherLimit<
489
            WithOtherEndian<
490
                WithOtherIntEncoding<
491
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
492
                    FixintEncoding,
493
                >,
494
                NativeEndian,
495
            >,
496
            Infinite,
497
        >,
498
    ),
499
    LimitedLittleVarintReject(
500
        WithOtherLimit<
501
            WithOtherEndian<
502
                WithOtherIntEncoding<
503
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
504
                    VarintEncoding,
505
                >,
506
                LittleEndian,
507
            >,
508
            Bounded,
509
        >,
510
    ),
511
    LimitedLittleVarintAllow(
512
        WithOtherLimit<
513
            WithOtherEndian<
514
                WithOtherIntEncoding<
515
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
516
                    VarintEncoding,
517
                >,
518
                LittleEndian,
519
            >,
520
            Bounded,
521
        >,
522
    ),
523
    LimitedLittleFixintReject(
524
        WithOtherLimit<
525
            WithOtherEndian<
526
                WithOtherIntEncoding<
527
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
528
                    FixintEncoding,
529
                >,
530
                LittleEndian,
531
            >,
532
            Bounded,
533
        >,
534
    ),
535
    LimitedLittleFixintAllow(
536
        WithOtherLimit<
537
            WithOtherEndian<
538
                WithOtherIntEncoding<
539
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
540
                    FixintEncoding,
541
                >,
542
                LittleEndian,
543
            >,
544
            Bounded,
545
        >,
546
    ),
547
    LimitedBigVarintReject(
548
        WithOtherLimit<
549
            WithOtherEndian<
550
                WithOtherIntEncoding<
551
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
552
                    VarintEncoding,
553
                >,
554
                BigEndian,
555
            >,
556
            Bounded,
557
        >,
558
    ),
559
    LimitedBigVarintAllow(
560
        WithOtherLimit<
561
            WithOtherEndian<
562
                WithOtherIntEncoding<
563
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
564
                    VarintEncoding,
565
                >,
566
                BigEndian,
567
            >,
568
            Bounded,
569
        >,
570
    ),
571
    LimitedBigFixintReject(
572
        WithOtherLimit<
573
            WithOtherEndian<
574
                WithOtherIntEncoding<
575
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
576
                    FixintEncoding,
577
                >,
578
                BigEndian,
579
            >,
580
            Bounded,
581
        >,
582
    ),
583
    LimitedBigFixintAllow(
584
        WithOtherLimit<
585
            WithOtherEndian<
586
                WithOtherIntEncoding<
587
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
588
                    FixintEncoding,
589
                >,
590
                BigEndian,
591
            >,
592
            Bounded,
593
        >,
594
    ),
595
    LimitedNativeVarintReject(
596
        WithOtherLimit<
597
            WithOtherEndian<
598
                WithOtherIntEncoding<
599
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
600
                    VarintEncoding,
601
                >,
602
                NativeEndian,
603
            >,
604
            Bounded,
605
        >,
606
    ),
607
    LimitedNativeVarintAllow(
608
        WithOtherLimit<
609
            WithOtherEndian<
610
                WithOtherIntEncoding<
611
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
612
                    VarintEncoding,
613
                >,
614
                NativeEndian,
615
            >,
616
            Bounded,
617
        >,
618
    ),
619
    LimitedNativeFixintReject(
620
        WithOtherLimit<
621
            WithOtherEndian<
622
                WithOtherIntEncoding<
623
                    WithOtherTrailing<DefaultOptions, RejectTrailing>,
624
                    FixintEncoding,
625
                >,
626
                NativeEndian,
627
            >,
628
            Bounded,
629
        >,
630
    ),
631
    LimitedNativeFixintAllow(
632
        WithOtherLimit<
633
            WithOtherEndian<
634
                WithOtherIntEncoding<
635
                    WithOtherTrailing<DefaultOptions, AllowTrailing>,
636
                    FixintEncoding,
637
                >,
638
                NativeEndian,
639
            >,
640
            Bounded,
641
        >,
642
    ),
643
}
644

            
645
impl<'a, T> Format<'a, T> for Bincode
646
where
647
    T: Serialize,
648
{
649
    type Error = bincode::Error;
650

            
651
163871
    fn serialized_size(&self, value: &T) -> Result<Option<usize>, Self::Error> {
652
163871
        BincodeOptions::from(self).serialized_size(value)
653
163871
    }
654

            
655
50
    fn serialize(&self, value: &T) -> Result<Vec<u8>, Self::Error> {
656
50
        BincodeOptions::from(self).serialize(value)
657
50
    }
658

            
659
163871
    fn serialize_into<W: Write>(&self, value: &T, writer: W) -> Result<(), Self::Error> {
660
163871
        BincodeOptions::from(self).serialize_into(value, writer)
661
163871
    }
662
}
663

            
664
impl<'a, T> BorrowedDeserializer<'a, T> for Bincode
665
where
666
    T: Serialize + Deserialize<'a>,
667
{
668
    fn deserialize_borrowed(&self, data: &'a [u8]) -> Result<T, Self::Error> {
669
        BincodeOptions::from(self).deserialize_borrowed(data)
670
    }
671
}
672

            
673
impl<T> OwnedDeserializer<T> for Bincode
674
where
675
    T: Serialize + DeserializeOwned,
676
{
677
25
    fn deserialize_from<R: Read>(&self, reader: R) -> Result<T, Self::Error> {
678
25
        BincodeOptions::from(self).deserialize_from(reader)
679
25
    }
680

            
681
163897
    fn deserialize_owned(&self, data: &[u8]) -> Result<T, Self::Error> {
682
163897
        BincodeOptions::from(self).deserialize_owned(data)
683
163897
    }
684
}
685

            
686
impl<'a, T> Format<'a, T> for BincodeOptions
687
where
688
    T: Serialize,
689
{
690
    type Error = bincode::Error;
691

            
692
163871
    fn serialized_size(&self, value: &T) -> Result<Option<usize>, Self::Error> {
693
163871
        match self {
694
163847
            BincodeOptions::UnlimitedLittleVarintReject(options) => options.serialized_size(value),
695
1
            BincodeOptions::UnlimitedLittleVarintAllow(options) => options.serialized_size(value),
696
1
            BincodeOptions::UnlimitedLittleFixintReject(options) => options.serialized_size(value),
697
2
            BincodeOptions::UnlimitedLittleFixintAllow(options) => options.serialized_size(value),
698
1
            BincodeOptions::UnlimitedBigVarintReject(options) => options.serialized_size(value),
699
1
            BincodeOptions::UnlimitedBigVarintAllow(options) => options.serialized_size(value),
700
1
            BincodeOptions::UnlimitedBigFixintReject(options) => options.serialized_size(value),
701
1
            BincodeOptions::UnlimitedBigFixintAllow(options) => options.serialized_size(value),
702
1
            BincodeOptions::UnlimitedNativeVarintReject(options) => options.serialized_size(value),
703
1
            BincodeOptions::UnlimitedNativeVarintAllow(options) => options.serialized_size(value),
704
1
            BincodeOptions::UnlimitedNativeFixintReject(options) => options.serialized_size(value),
705
1
            BincodeOptions::UnlimitedNativeFixintAllow(options) => options.serialized_size(value),
706
1
            BincodeOptions::LimitedLittleVarintReject(options) => options.serialized_size(value),
707
1
            BincodeOptions::LimitedLittleVarintAllow(options) => options.serialized_size(value),
708
1
            BincodeOptions::LimitedLittleFixintReject(options) => options.serialized_size(value),
709
1
            BincodeOptions::LimitedLittleFixintAllow(options) => options.serialized_size(value),
710
1
            BincodeOptions::LimitedBigVarintReject(options) => options.serialized_size(value),
711
1
            BincodeOptions::LimitedBigVarintAllow(options) => options.serialized_size(value),
712
1
            BincodeOptions::LimitedBigFixintReject(options) => options.serialized_size(value),
713
1
            BincodeOptions::LimitedBigFixintAllow(options) => options.serialized_size(value),
714
1
            BincodeOptions::LimitedNativeVarintReject(options) => options.serialized_size(value),
715
1
            BincodeOptions::LimitedNativeVarintAllow(options) => options.serialized_size(value),
716
1
            BincodeOptions::LimitedNativeFixintReject(options) => options.serialized_size(value),
717
1
            BincodeOptions::LimitedNativeFixintAllow(options) => options.serialized_size(value),
718
        }
719
163871
        .and_then(|size| {
720
163871
            usize::try_from(size)
721
163871
                .map(Some)
722
163871
                .map_err(bincode::Error::custom)
723
163871
        })
724
163871
    }
725

            
726
50
    fn serialize(&self, value: &T) -> Result<Vec<u8>, Self::Error> {
727
50
        match self {
728
2
            BincodeOptions::UnlimitedLittleVarintReject(options) => options.serialize(value),
729
2
            BincodeOptions::UnlimitedLittleVarintAllow(options) => options.serialize(value),
730
2
            BincodeOptions::UnlimitedLittleFixintReject(options) => options.serialize(value),
731
4
            BincodeOptions::UnlimitedLittleFixintAllow(options) => options.serialize(value),
732
2
            BincodeOptions::UnlimitedBigVarintReject(options) => options.serialize(value),
733
2
            BincodeOptions::UnlimitedBigVarintAllow(options) => options.serialize(value),
734
2
            BincodeOptions::UnlimitedBigFixintReject(options) => options.serialize(value),
735
2
            BincodeOptions::UnlimitedBigFixintAllow(options) => options.serialize(value),
736
2
            BincodeOptions::UnlimitedNativeVarintReject(options) => options.serialize(value),
737
2
            BincodeOptions::UnlimitedNativeVarintAllow(options) => options.serialize(value),
738
2
            BincodeOptions::UnlimitedNativeFixintReject(options) => options.serialize(value),
739
2
            BincodeOptions::UnlimitedNativeFixintAllow(options) => options.serialize(value),
740
2
            BincodeOptions::LimitedLittleVarintReject(options) => options.serialize(value),
741
2
            BincodeOptions::LimitedLittleVarintAllow(options) => options.serialize(value),
742
2
            BincodeOptions::LimitedLittleFixintReject(options) => options.serialize(value),
743
2
            BincodeOptions::LimitedLittleFixintAllow(options) => options.serialize(value),
744
2
            BincodeOptions::LimitedBigVarintReject(options) => options.serialize(value),
745
2
            BincodeOptions::LimitedBigVarintAllow(options) => options.serialize(value),
746
2
            BincodeOptions::LimitedBigFixintReject(options) => options.serialize(value),
747
2
            BincodeOptions::LimitedBigFixintAllow(options) => options.serialize(value),
748
2
            BincodeOptions::LimitedNativeVarintReject(options) => options.serialize(value),
749
2
            BincodeOptions::LimitedNativeVarintAllow(options) => options.serialize(value),
750
2
            BincodeOptions::LimitedNativeFixintReject(options) => options.serialize(value),
751
2
            BincodeOptions::LimitedNativeFixintAllow(options) => options.serialize(value),
752
        }
753
50
    }
754

            
755
163871
    fn serialize_into<W: Write>(&self, value: &T, writer: W) -> Result<(), Self::Error> {
756
163871
        match self {
757
163847
            BincodeOptions::UnlimitedLittleVarintReject(options) => {
758
163847
                options.serialize_into(writer, value)
759
            }
760
1
            BincodeOptions::UnlimitedLittleVarintAllow(options) => {
761
1
                options.serialize_into(writer, value)
762
            }
763
1
            BincodeOptions::UnlimitedLittleFixintReject(options) => {
764
1
                options.serialize_into(writer, value)
765
            }
766
2
            BincodeOptions::UnlimitedLittleFixintAllow(options) => {
767
2
                options.serialize_into(writer, value)
768
            }
769
1
            BincodeOptions::UnlimitedBigVarintReject(options) => {
770
1
                options.serialize_into(writer, value)
771
            }
772
1
            BincodeOptions::UnlimitedBigVarintAllow(options) => {
773
1
                options.serialize_into(writer, value)
774
            }
775
1
            BincodeOptions::UnlimitedBigFixintReject(options) => {
776
1
                options.serialize_into(writer, value)
777
            }
778
1
            BincodeOptions::UnlimitedBigFixintAllow(options) => {
779
1
                options.serialize_into(writer, value)
780
            }
781
1
            BincodeOptions::UnlimitedNativeVarintReject(options) => {
782
1
                options.serialize_into(writer, value)
783
            }
784
1
            BincodeOptions::UnlimitedNativeVarintAllow(options) => {
785
1
                options.serialize_into(writer, value)
786
            }
787
1
            BincodeOptions::UnlimitedNativeFixintReject(options) => {
788
1
                options.serialize_into(writer, value)
789
            }
790
1
            BincodeOptions::UnlimitedNativeFixintAllow(options) => {
791
1
                options.serialize_into(writer, value)
792
            }
793
1
            BincodeOptions::LimitedLittleVarintReject(options) => {
794
1
                options.serialize_into(writer, value)
795
            }
796
1
            BincodeOptions::LimitedLittleVarintAllow(options) => {
797
1
                options.serialize_into(writer, value)
798
            }
799
1
            BincodeOptions::LimitedLittleFixintReject(options) => {
800
1
                options.serialize_into(writer, value)
801
            }
802
1
            BincodeOptions::LimitedLittleFixintAllow(options) => {
803
1
                options.serialize_into(writer, value)
804
            }
805
1
            BincodeOptions::LimitedBigVarintReject(options) => {
806
1
                options.serialize_into(writer, value)
807
            }
808
1
            BincodeOptions::LimitedBigVarintAllow(options) => options.serialize_into(writer, value),
809
1
            BincodeOptions::LimitedBigFixintReject(options) => {
810
1
                options.serialize_into(writer, value)
811
            }
812
1
            BincodeOptions::LimitedBigFixintAllow(options) => options.serialize_into(writer, value),
813
1
            BincodeOptions::LimitedNativeVarintReject(options) => {
814
1
                options.serialize_into(writer, value)
815
            }
816
1
            BincodeOptions::LimitedNativeVarintAllow(options) => {
817
1
                options.serialize_into(writer, value)
818
            }
819
1
            BincodeOptions::LimitedNativeFixintReject(options) => {
820
1
                options.serialize_into(writer, value)
821
            }
822
1
            BincodeOptions::LimitedNativeFixintAllow(options) => {
823
1
                options.serialize_into(writer, value)
824
            }
825
        }
826
163871
    }
827
}
828

            
829
impl<'a, T> BorrowedDeserializer<'a, T> for BincodeOptions
830
where
831
    T: Serialize + Deserialize<'a>,
832
{
833
    fn deserialize_borrowed(&self, data: &'a [u8]) -> Result<T, Self::Error> {
834
        match self {
835
            BincodeOptions::UnlimitedLittleVarintReject(options) => options.deserialize(data),
836
            BincodeOptions::UnlimitedLittleVarintAllow(options) => options.deserialize(data),
837
            BincodeOptions::UnlimitedLittleFixintReject(options) => options.deserialize(data),
838
            BincodeOptions::UnlimitedLittleFixintAllow(options) => options.deserialize(data),
839
            BincodeOptions::UnlimitedBigVarintReject(options) => options.deserialize(data),
840
            BincodeOptions::UnlimitedBigVarintAllow(options) => options.deserialize(data),
841
            BincodeOptions::UnlimitedBigFixintReject(options) => options.deserialize(data),
842
            BincodeOptions::UnlimitedBigFixintAllow(options) => options.deserialize(data),
843
            BincodeOptions::UnlimitedNativeVarintReject(options) => options.deserialize(data),
844
            BincodeOptions::UnlimitedNativeVarintAllow(options) => options.deserialize(data),
845
            BincodeOptions::UnlimitedNativeFixintReject(options) => options.deserialize(data),
846
            BincodeOptions::UnlimitedNativeFixintAllow(options) => options.deserialize(data),
847
            BincodeOptions::LimitedLittleVarintReject(options) => options.deserialize(data),
848
            BincodeOptions::LimitedLittleVarintAllow(options) => options.deserialize(data),
849
            BincodeOptions::LimitedLittleFixintReject(options) => options.deserialize(data),
850
            BincodeOptions::LimitedLittleFixintAllow(options) => options.deserialize(data),
851
            BincodeOptions::LimitedBigVarintReject(options) => options.deserialize(data),
852
            BincodeOptions::LimitedBigVarintAllow(options) => options.deserialize(data),
853
            BincodeOptions::LimitedBigFixintReject(options) => options.deserialize(data),
854
            BincodeOptions::LimitedBigFixintAllow(options) => options.deserialize(data),
855
            BincodeOptions::LimitedNativeVarintReject(options) => options.deserialize(data),
856
            BincodeOptions::LimitedNativeVarintAllow(options) => options.deserialize(data),
857
            BincodeOptions::LimitedNativeFixintReject(options) => options.deserialize(data),
858
            BincodeOptions::LimitedNativeFixintAllow(options) => options.deserialize(data),
859
        }
860
    }
861
}
862

            
863
impl<T> OwnedDeserializer<T> for BincodeOptions
864
where
865
    T: Serialize + DeserializeOwned,
866
{
867
163897
    fn deserialize_owned(&self, data: &[u8]) -> Result<T, Self::Error> {
868
163897
        match self {
869
163848
            BincodeOptions::UnlimitedLittleVarintReject(options) => options.deserialize(data),
870
2
            BincodeOptions::UnlimitedLittleVarintAllow(options) => options.deserialize(data),
871
2
            BincodeOptions::UnlimitedLittleFixintReject(options) => options.deserialize(data),
872
5
            BincodeOptions::UnlimitedLittleFixintAllow(options) => options.deserialize(data),
873
2
            BincodeOptions::UnlimitedBigVarintReject(options) => options.deserialize(data),
874
2
            BincodeOptions::UnlimitedBigVarintAllow(options) => options.deserialize(data),
875
2
            BincodeOptions::UnlimitedBigFixintReject(options) => options.deserialize(data),
876
2
            BincodeOptions::UnlimitedBigFixintAllow(options) => options.deserialize(data),
877
2
            BincodeOptions::UnlimitedNativeVarintReject(options) => options.deserialize(data),
878
2
            BincodeOptions::UnlimitedNativeVarintAllow(options) => options.deserialize(data),
879
2
            BincodeOptions::UnlimitedNativeFixintReject(options) => options.deserialize(data),
880
2
            BincodeOptions::UnlimitedNativeFixintAllow(options) => options.deserialize(data),
881
2
            BincodeOptions::LimitedLittleVarintReject(options) => options.deserialize(data),
882
2
            BincodeOptions::LimitedLittleVarintAllow(options) => options.deserialize(data),
883
2
            BincodeOptions::LimitedLittleFixintReject(options) => options.deserialize(data),
884
2
            BincodeOptions::LimitedLittleFixintAllow(options) => options.deserialize(data),
885
2
            BincodeOptions::LimitedBigVarintReject(options) => options.deserialize(data),
886
2
            BincodeOptions::LimitedBigVarintAllow(options) => options.deserialize(data),
887
2
            BincodeOptions::LimitedBigFixintReject(options) => options.deserialize(data),
888
2
            BincodeOptions::LimitedBigFixintAllow(options) => options.deserialize(data),
889
2
            BincodeOptions::LimitedNativeVarintReject(options) => options.deserialize(data),
890
2
            BincodeOptions::LimitedNativeVarintAllow(options) => options.deserialize(data),
891
2
            BincodeOptions::LimitedNativeFixintReject(options) => options.deserialize(data),
892
2
            BincodeOptions::LimitedNativeFixintAllow(options) => options.deserialize(data),
893
        }
894
163897
    }
895

            
896
25
    fn deserialize_from<R: Read>(&self, reader: R) -> Result<T, Self::Error> {
897
25
        match self {
898
1
            BincodeOptions::UnlimitedLittleVarintReject(options) => {
899
1
                options.deserialize_from(reader)
900
            }
901
1
            BincodeOptions::UnlimitedLittleVarintAllow(options) => options.deserialize_from(reader),
902
1
            BincodeOptions::UnlimitedLittleFixintReject(options) => {
903
1
                options.deserialize_from(reader)
904
            }
905
2
            BincodeOptions::UnlimitedLittleFixintAllow(options) => options.deserialize_from(reader),
906
1
            BincodeOptions::UnlimitedBigVarintReject(options) => options.deserialize_from(reader),
907
1
            BincodeOptions::UnlimitedBigVarintAllow(options) => options.deserialize_from(reader),
908
1
            BincodeOptions::UnlimitedBigFixintReject(options) => options.deserialize_from(reader),
909
1
            BincodeOptions::UnlimitedBigFixintAllow(options) => options.deserialize_from(reader),
910
1
            BincodeOptions::UnlimitedNativeVarintReject(options) => {
911
1
                options.deserialize_from(reader)
912
            }
913
1
            BincodeOptions::UnlimitedNativeVarintAllow(options) => options.deserialize_from(reader),
914
1
            BincodeOptions::UnlimitedNativeFixintReject(options) => {
915
1
                options.deserialize_from(reader)
916
            }
917
1
            BincodeOptions::UnlimitedNativeFixintAllow(options) => options.deserialize_from(reader),
918
1
            BincodeOptions::LimitedLittleVarintReject(options) => options.deserialize_from(reader),
919
1
            BincodeOptions::LimitedLittleVarintAllow(options) => options.deserialize_from(reader),
920
1
            BincodeOptions::LimitedLittleFixintReject(options) => options.deserialize_from(reader),
921
1
            BincodeOptions::LimitedLittleFixintAllow(options) => options.deserialize_from(reader),
922
1
            BincodeOptions::LimitedBigVarintReject(options) => options.deserialize_from(reader),
923
1
            BincodeOptions::LimitedBigVarintAllow(options) => options.deserialize_from(reader),
924
1
            BincodeOptions::LimitedBigFixintReject(options) => options.deserialize_from(reader),
925
1
            BincodeOptions::LimitedBigFixintAllow(options) => options.deserialize_from(reader),
926
1
            BincodeOptions::LimitedNativeVarintReject(options) => options.deserialize_from(reader),
927
1
            BincodeOptions::LimitedNativeVarintAllow(options) => options.deserialize_from(reader),
928
1
            BincodeOptions::LimitedNativeFixintReject(options) => options.deserialize_from(reader),
929
1
            BincodeOptions::LimitedNativeFixintAllow(options) => options.deserialize_from(reader),
930
        }
931
25
    }
932
}
933

            
934
1
#[test]
935
#[allow(clippy::too_many_lines)]
936
1
fn format_tests() {
937
1
    transmog::test_util::test_format(&Bincode::legacy_default());
938
1
    transmog::test_util::test_format(
939
1
        &Bincode::default()
940
1
            .no_limit()
941
1
            .little_endian()
942
1
            .variable_integer_encoding()
943
1
            .reject_trailing_bytes(),
944
1
    );
945
1
    transmog::test_util::test_format(
946
1
        &Bincode::default()
947
1
            .no_limit()
948
1
            .little_endian()
949
1
            .variable_integer_encoding()
950
1
            .allow_trailing_bytes(),
951
1
    );
952
1
    transmog::test_util::test_format(
953
1
        &Bincode::default()
954
1
            .no_limit()
955
1
            .little_endian()
956
1
            .fixed_integer_encoding()
957
1
            .reject_trailing_bytes(),
958
1
    );
959
1
    transmog::test_util::test_format(
960
1
        &Bincode::default()
961
1
            .no_limit()
962
1
            .little_endian()
963
1
            .fixed_integer_encoding()
964
1
            .allow_trailing_bytes(),
965
1
    );
966
1
    transmog::test_util::test_format(
967
1
        &Bincode::default()
968
1
            .no_limit()
969
1
            .big_endian()
970
1
            .variable_integer_encoding()
971
1
            .reject_trailing_bytes(),
972
1
    );
973
1
    transmog::test_util::test_format(
974
1
        &Bincode::default()
975
1
            .no_limit()
976
1
            .big_endian()
977
1
            .variable_integer_encoding()
978
1
            .allow_trailing_bytes(),
979
1
    );
980
1
    transmog::test_util::test_format(
981
1
        &Bincode::default()
982
1
            .no_limit()
983
1
            .big_endian()
984
1
            .fixed_integer_encoding()
985
1
            .reject_trailing_bytes(),
986
1
    );
987
1
    transmog::test_util::test_format(
988
1
        &Bincode::default()
989
1
            .no_limit()
990
1
            .big_endian()
991
1
            .fixed_integer_encoding()
992
1
            .allow_trailing_bytes(),
993
1
    );
994
1
    transmog::test_util::test_format(
995
1
        &Bincode::default()
996
1
            .no_limit()
997
1
            .native_endian()
998
1
            .variable_integer_encoding()
999
1
            .reject_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .no_limit()
1
            .native_endian()
1
            .variable_integer_encoding()
1
            .allow_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .no_limit()
1
            .native_endian()
1
            .fixed_integer_encoding()
1
            .reject_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .no_limit()
1
            .native_endian()
1
            .fixed_integer_encoding()
1
            .allow_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .little_endian()
1
            .variable_integer_encoding()
1
            .reject_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .little_endian()
1
            .variable_integer_encoding()
1
            .allow_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .little_endian()
1
            .fixed_integer_encoding()
1
            .reject_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .little_endian()
1
            .fixed_integer_encoding()
1
            .allow_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .big_endian()
1
            .variable_integer_encoding()
1
            .reject_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .big_endian()
1
            .variable_integer_encoding()
1
            .allow_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .big_endian()
1
            .fixed_integer_encoding()
1
            .reject_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .big_endian()
1
            .fixed_integer_encoding()
1
            .allow_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .native_endian()
1
            .variable_integer_encoding()
1
            .reject_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .native_endian()
1
            .variable_integer_encoding()
1
            .allow_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .native_endian()
1
            .fixed_integer_encoding()
1
            .reject_trailing_bytes(),
1
    );
1
    transmog::test_util::test_format(
1
        &Bincode::default()
1
            .limit(64)
1
            .native_endian()
1
            .fixed_integer_encoding()
1
            .allow_trailing_bytes(),
1
    );
1
}