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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
//! Encoders and decoders for [scalar] values.
//!
//! [scalar]: https://developers.google.com/protocol-buffers/docs/proto3#scalar
use bytecodec::bytes::{BytesEncoder as BytesEncoderInner, RemainingBytesDecoder, Utf8Decoder,
                       Utf8Encoder};
use bytecodec::fixnum::{F32leDecoder, F32leEncoder, F64leDecoder, F64leEncoder, I32leDecoder,
                        I32leEncoder, I64leDecoder, I64leEncoder, U32leDecoder, U32leEncoder,
                        U64leDecoder, U64leEncoder};
use bytecodec::{ByteCount, Decode, Encode, Eos, Result, SizedEncode};

use value::{MapKeyDecode, MapKeyEncode, NumericValueDecode, NumericValueEncode, ValueDecode,
            ValueEncode};
use wire::{LengthDelimitedDecoder, LengthDelimitedEncoder, VarintDecoder, VarintEncoder, WireType};

macro_rules! impl_newtype_decode {
    ($decoder:ty, $item:ty, $wire:ident) => {
        impl Decode for $decoder {
            type Item = $item;

            fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
                track!(self.0.decode(buf, eos))
            }

            fn finish_decoding(&mut self) -> Result<Self::Item> {
                track!(self.0.finish_decoding())
            }

            fn requiring_bytes(&self) -> ByteCount {
                self.0.requiring_bytes()
            }

            fn is_idle(&self) -> bool {
                self.0.is_idle()
            }
        }
        impl ValueDecode for $decoder {
            fn wire_type(&self) -> WireType {
                WireType::$wire
            }
        }
    };
}

macro_rules! impl_newtype_encode {
    ($encoder:ty, $item:ty, $wire:ident) => {
        impl Encode for $encoder {
            type Item = $item;

            fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
                track!(self.0.encode(buf, eos))
            }

            fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
                track!(self.0.start_encoding(item))
            }

            fn is_idle(&self) -> bool {
                self.0.is_idle()
            }

            fn requiring_bytes(&self) -> ByteCount {
                self.0.requiring_bytes()
            }
        }
        impl SizedEncode for $encoder {
            fn exact_requiring_bytes(&self) -> u64 {
                self.0.exact_requiring_bytes()
            }
        }
        impl ValueEncode for $encoder {
            fn wire_type(&self) -> WireType {
                WireType::$wire
            }
        }
    };
}

macro_rules! impl_varint_decode {
    ($decoder:ty, $item:ty) => {
        impl Decode for $decoder {
            type Item = $item;

            fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
                track!(self.0.decode(buf, eos))
            }

            fn finish_decoding(&mut self) -> Result<Self::Item> {
                track!(self.0.finish_decoding()).map(Self::value_from_varint)
            }

            fn requiring_bytes(&self) -> ByteCount {
                self.0.requiring_bytes()
            }

            fn is_idle(&self) -> bool {
                self.0.is_idle()
            }
        }
        impl ValueDecode for $decoder {
            fn wire_type(&self) -> WireType {
                WireType::Varint
            }
        }
    };
}

macro_rules! impl_varint_encode {
    ($encoder:ty, $item:ty) => {
        impl Encode for $encoder {
            type Item = $item;

            fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
                track!(self.0.encode(buf, eos))
            }

            fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
                track!(self.0.start_encoding(Self::value_to_varint(item)))
            }

            fn is_idle(&self) -> bool {
                self.0.is_idle()
            }

            fn requiring_bytes(&self) -> ByteCount {
                self.0.requiring_bytes()
            }
        }
        impl SizedEncode for $encoder {
            fn exact_requiring_bytes(&self) -> u64 {
                self.0.exact_requiring_bytes()
            }
        }
        impl ValueEncode for $encoder {
            fn wire_type(&self) -> WireType {
                WireType::Varint
            }
        }
    };
}

/// Decoder for `double` values.
#[derive(Debug, Default)]
pub struct DoubleDecoder(F64leDecoder);
impl DoubleDecoder {
    /// Makes a new `DoubleDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_decode!(DoubleDecoder, f64, Bit64);
impl NumericValueDecode for DoubleDecoder {}

/// Encoder for `double` values.
#[derive(Debug, Default)]
pub struct DoubleEncoder(F64leEncoder);
impl DoubleEncoder {
    /// Makes a new `DoubleEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_encode!(DoubleEncoder, f64, Bit64);
impl NumericValueEncode for DoubleEncoder {}

/// Decoder for `float` values.
#[derive(Debug, Default)]
pub struct FloatDecoder(F32leDecoder);
impl FloatDecoder {
    /// Makes a new `FloatDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_decode!(FloatDecoder, f32, Bit32);
impl NumericValueDecode for FloatDecoder {}

/// Encoder for `float` values.
#[derive(Debug, Default)]
pub struct FloatEncoder(F32leEncoder);
impl FloatEncoder {
    /// Makes a new `FloatEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_encode!(FloatEncoder, f32, Bit32);
impl NumericValueEncode for FloatEncoder {}

/// Decoder for `fixed32` values.
#[derive(Debug, Default)]
pub struct Fixed32Decoder(U32leDecoder);
impl Fixed32Decoder {
    /// Makes a new `Fixed32Decoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_decode!(Fixed32Decoder, u32, Bit32);
impl MapKeyDecode for Fixed32Decoder {}
impl NumericValueDecode for Fixed32Decoder {}

/// Encoder for `fixed32` values.
#[derive(Debug, Default)]
pub struct Fixed32Encoder(U32leEncoder);
impl Fixed32Encoder {
    /// Makes a new `Fixed32Encoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_encode!(Fixed32Encoder, u32, Bit32);
impl MapKeyEncode for Fixed32Encoder {}
impl NumericValueEncode for Fixed32Encoder {}

/// Decoder for `fixed64` values.
#[derive(Debug, Default)]
pub struct Fixed64Decoder(U64leDecoder);
impl Fixed64Decoder {
    /// Makes a new `Fixed64Decoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_decode!(Fixed64Decoder, u64, Bit64);
impl MapKeyDecode for Fixed64Decoder {}
impl NumericValueDecode for Fixed64Decoder {}

/// Encoder for `fixed64` values.
#[derive(Debug, Default)]
pub struct Fixed64Encoder(U64leEncoder);
impl Fixed64Encoder {
    /// Makes a new `Fixed64Encoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_encode!(Fixed64Encoder, u64, Bit64);
impl MapKeyEncode for Fixed64Encoder {}
impl NumericValueEncode for Fixed64Encoder {}

/// Decoder for `sfixed32` values.
#[derive(Debug, Default)]
pub struct Sfixed32Decoder(I32leDecoder);
impl Sfixed32Decoder {
    /// Makes a new `Sfixed32Decoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_decode!(Sfixed32Decoder, i32, Bit32);
impl MapKeyDecode for Sfixed32Decoder {}
impl NumericValueDecode for Sfixed32Decoder {}

/// Encoder for `sfixed32` values.
#[derive(Debug, Default)]
pub struct Sfixed32Encoder(I32leEncoder);
impl Sfixed32Encoder {
    /// Makes a new `Sfixed32Encoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_encode!(Sfixed32Encoder, i32, Bit32);
impl MapKeyEncode for Sfixed32Encoder {}
impl NumericValueEncode for Sfixed32Encoder {}

/// Decoder for `sfixed64` values.
#[derive(Debug, Default)]
pub struct Sfixed64Decoder(I64leDecoder);
impl Sfixed64Decoder {
    /// Makes a new `Sfixed64Decoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_decode!(Sfixed64Decoder, i64, Bit64);
impl MapKeyDecode for Sfixed64Decoder {}
impl NumericValueDecode for Sfixed64Decoder {}

/// Encoder for `sfixed64` values.
#[derive(Debug, Default)]
pub struct Sfixed64Encoder(I64leEncoder);
impl Sfixed64Encoder {
    /// Makes a new `Sfixed64Encoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_encode!(Sfixed64Encoder, i64, Bit64);
impl MapKeyEncode for Sfixed64Encoder {}
impl NumericValueEncode for Sfixed64Encoder {}

/// Decoder for `bool` values.
#[derive(Debug, Default)]
pub struct BoolDecoder(VarintDecoder);
impl BoolDecoder {
    /// Makes a new `BoolDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_from_varint(n: u64) -> bool {
        n != 0
    }
}
impl_varint_decode!(BoolDecoder, bool);
impl MapKeyDecode for BoolDecoder {}
impl NumericValueDecode for BoolDecoder {}

/// Encoder for `bool` values.
#[derive(Debug, Default)]
pub struct BoolEncoder(VarintEncoder);
impl BoolEncoder {
    /// Makes a new `BoolEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_to_varint(n: bool) -> u64 {
        n as u64
    }
}
impl_varint_encode!(BoolEncoder, bool);
impl MapKeyEncode for BoolEncoder {}
impl NumericValueEncode for BoolEncoder {}

/// Decoder for `int32` values.
#[derive(Debug, Default)]
pub struct Int32Decoder(VarintDecoder);
impl Int32Decoder {
    /// Makes a new `Int32Decoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_from_varint(n: u64) -> i32 {
        n as i32
    }
}
impl_varint_decode!(Int32Decoder, i32);
impl MapKeyDecode for Int32Decoder {}
impl NumericValueDecode for Int32Decoder {}

/// Encoder for `int32` values.
#[derive(Debug, Default)]
pub struct Int32Encoder(VarintEncoder);
impl Int32Encoder {
    /// Makes a new `Int32Encoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_to_varint(n: i32) -> u64 {
        n as u64
    }
}
impl_varint_encode!(Int32Encoder, i32);
impl MapKeyEncode for Int32Encoder {}
impl NumericValueEncode for Int32Encoder {}

/// Decoder for `int64` values.
#[derive(Debug, Default)]
pub struct Int64Decoder(VarintDecoder);
impl Int64Decoder {
    /// Makes a new `Int64Decoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_from_varint(n: u64) -> i64 {
        n as i64
    }
}
impl_varint_decode!(Int64Decoder, i64);
impl MapKeyDecode for Int64Decoder {}
impl NumericValueDecode for Int64Decoder {}

/// Encoder for `int64` values.
#[derive(Debug, Default)]
pub struct Int64Encoder(VarintEncoder);
impl Int64Encoder {
    /// Makes a new `Int64Encoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_to_varint(n: i64) -> u64 {
        n as u64
    }
}
impl_varint_encode!(Int64Encoder, i64);
impl MapKeyEncode for Int64Encoder {}
impl NumericValueEncode for Int64Encoder {}

/// Decoder for `uint32` values.
#[derive(Debug, Default)]
pub struct Uint32Decoder(VarintDecoder);
impl Uint32Decoder {
    /// Makes a new `Uint32Decoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_from_varint(n: u64) -> u32 {
        n as u32
    }
}
impl_varint_decode!(Uint32Decoder, u32);
impl MapKeyDecode for Uint32Decoder {}
impl NumericValueDecode for Uint32Decoder {}

/// Encoder for `uint32` values.
#[derive(Debug, Default)]
pub struct Uint32Encoder(VarintEncoder);
impl Uint32Encoder {
    /// Makes a new `Uint32Encoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_to_varint(n: u32) -> u64 {
        u64::from(n)
    }
}
impl_varint_encode!(Uint32Encoder, u32);
impl MapKeyEncode for Uint32Encoder {}
impl NumericValueEncode for Uint32Encoder {}

/// Decoder for `uint64` values.
#[derive(Debug, Default)]
pub struct Uint64Decoder(VarintDecoder);
impl Uint64Decoder {
    /// Makes a new `Uint64Decoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_from_varint(n: u64) -> u64 {
        n
    }
}
impl_varint_decode!(Uint64Decoder, u64);
impl MapKeyDecode for Uint64Decoder {}
impl NumericValueDecode for Uint64Decoder {}

/// Encoder for `uint64` values.
#[derive(Debug, Default)]
pub struct Uint64Encoder(VarintEncoder);
impl Uint64Encoder {
    /// Makes a new `Uint64Encoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_to_varint(n: u64) -> u64 {
        n
    }
}
impl_varint_encode!(Uint64Encoder, u64);
impl MapKeyEncode for Uint64Encoder {}
impl NumericValueEncode for Uint64Encoder {}

/// Decoder for `sint32` values.
#[derive(Debug, Default)]
pub struct Sint32Decoder(VarintDecoder);
impl Sint32Decoder {
    /// Makes a new `Sint32Decoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_from_varint(n: u64) -> i32 {
        let n = n as i32;
        (n >> 1) ^ ((n << 31) >> 31)
    }
}
impl_varint_decode!(Sint32Decoder, i32);
impl MapKeyDecode for Sint32Decoder {}
impl NumericValueDecode for Sint32Decoder {}

/// Encoder for `sint32` values.
#[derive(Debug, Default)]
pub struct Sint32Encoder(VarintEncoder);
impl Sint32Encoder {
    /// Makes a new `Sint32Encoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_to_varint(n: i32) -> u64 {
        ((n << 1) ^ (n >> 31)) as u64
    }
}
impl_varint_encode!(Sint32Encoder, i32);
impl MapKeyEncode for Sint32Encoder {}
impl NumericValueEncode for Sint32Encoder {}

/// Decoder for `sint64` values.
#[derive(Debug, Default)]
pub struct Sint64Decoder(VarintDecoder);
impl Sint64Decoder {
    /// Makes a new `Sint64Decoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_from_varint(n: u64) -> i64 {
        let n = n as i64;
        (n >> 1) ^ ((n << 63) >> 63)
    }
}
impl_varint_decode!(Sint64Decoder, i64);
impl MapKeyDecode for Sint64Decoder {}
impl NumericValueDecode for Sint64Decoder {}

/// Encoder for `sint64` values.
#[derive(Debug, Default)]
pub struct Sint64Encoder(VarintEncoder);
impl Sint64Encoder {
    /// Makes a new `Sint64Encoder` instance.
    pub fn new() -> Self {
        Self::default()
    }

    fn value_to_varint(n: i64) -> u64 {
        ((n << 1) ^ (n >> 63)) as u64
    }
}
impl_varint_encode!(Sint64Encoder, i64);
impl MapKeyEncode for Sint64Encoder {}
impl NumericValueEncode for Sint64Encoder {}

/// Decoder for `bytes` values.
#[derive(Debug, Default)]
pub struct BytesDecoder(LengthDelimitedDecoder<RemainingBytesDecoder>);
impl BytesDecoder {
    /// Makes a new `BytesDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_decode!(BytesDecoder, Vec<u8>, LengthDelimited);

/// Decoder for custom `bytes` values.
///
/// This is equivalent to `BytesDecoder` in the protobol buffers layer,
/// but it decodes the payload bytes by using `D` and
/// returns the decoded items to the application layer instead of raw bytes.
#[derive(Debug, Default)]
pub struct CustomBytesDecoder<D>(LengthDelimitedDecoder<D>);
impl<D: Decode> CustomBytesDecoder<D> {
    /// Makes a new `CustomBytesDecoder` instance.
    pub fn new(inner: D) -> Self {
        CustomBytesDecoder(LengthDelimitedDecoder::new(inner))
    }

    /// Returns a reference to the inner decoder.
    pub fn inner_ref(&self) -> &D {
        self.0.inner_ref()
    }

    /// Returns a mutable reference to the inner decoder.
    pub fn inner_mut(&mut self) -> &mut D {
        self.0.inner_mut()
    }

    /// Takes ownership of the instance and returns the inner decoder.
    pub fn into_inner(self) -> D {
        self.0.into_inner()
    }
}
impl<D: Decode> Decode for CustomBytesDecoder<D> {
    type Item = D::Item;

    fn decode(&mut self, buf: &[u8], eos: Eos) -> Result<usize> {
        track!(self.0.decode(buf, eos))
    }

    fn finish_decoding(&mut self) -> Result<Self::Item> {
        track!(self.0.finish_decoding())
    }

    fn requiring_bytes(&self) -> ByteCount {
        self.0.requiring_bytes()
    }

    fn is_idle(&self) -> bool {
        self.0.is_idle()
    }
}
impl<D: Decode> ValueDecode for CustomBytesDecoder<D> {
    fn wire_type(&self) -> WireType {
        WireType::LengthDelimited
    }
}

/// Encoder for `bytes` values.
#[derive(Debug)]
pub struct BytesEncoder<B = Vec<u8>>(LengthDelimitedEncoder<BytesEncoderInner<B>>);
impl<B> BytesEncoder<B> {
    /// Makes a new `BytesEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl<B> Default for BytesEncoder<B> {
    fn default() -> Self {
        BytesEncoder(Default::default())
    }
}
impl<B: AsRef<[u8]>> Encode for BytesEncoder<B> {
    type Item = B;

    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
        track!(self.0.encode(buf, eos))
    }

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        track!(self.0.start_encoding(item))
    }

    fn is_idle(&self) -> bool {
        self.0.is_idle()
    }

    fn requiring_bytes(&self) -> ByteCount {
        self.0.requiring_bytes()
    }
}
impl<B: AsRef<[u8]>> SizedEncode for BytesEncoder<B> {
    fn exact_requiring_bytes(&self) -> u64 {
        self.0.exact_requiring_bytes()
    }
}
impl<B: AsRef<[u8]>> ValueEncode for BytesEncoder<B> {
    fn wire_type(&self) -> WireType {
        WireType::LengthDelimited
    }
}

/// Encoder for custom `bytes` values.
///
/// This is equivalent to `BytesEncoder` in the protobol buffers layer,
/// but it uses the encoder `E` for producing bytes instead of passing raw bytes.
#[derive(Debug, Default)]
pub struct CustomBytesEncoder<E>(LengthDelimitedEncoder<E>);
impl<E: SizedEncode> CustomBytesEncoder<E> {
    /// Makes a new `CustomBytesEncoder` instance.
    pub fn new(inner: E) -> Self {
        CustomBytesEncoder(LengthDelimitedEncoder::new(inner))
    }

    /// Returns a reference to the inner encoder.
    pub fn inner_ref(&self) -> &E {
        self.0.inner_ref()
    }

    /// Returns a mutable reference to the inner encoder.
    pub fn inner_mut(&mut self) -> &mut E {
        self.0.inner_mut()
    }

    /// Takes ownership of the instance and returns the inner encoder.
    pub fn into_inner(self) -> E {
        self.0.into_inner()
    }
}
impl<E: SizedEncode> Encode for CustomBytesEncoder<E> {
    type Item = E::Item;

    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
        track!(self.0.encode(buf, eos))
    }

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        track!(self.0.start_encoding(item))
    }

    fn is_idle(&self) -> bool {
        self.0.is_idle()
    }

    fn requiring_bytes(&self) -> ByteCount {
        self.0.requiring_bytes()
    }
}
impl<E: SizedEncode> SizedEncode for CustomBytesEncoder<E> {
    fn exact_requiring_bytes(&self) -> u64 {
        self.0.exact_requiring_bytes()
    }
}
impl<E: SizedEncode> ValueEncode for CustomBytesEncoder<E> {
    fn wire_type(&self) -> WireType {
        WireType::LengthDelimited
    }
}

/// Decoder for `string` values.
#[derive(Debug, Default)]
pub struct StringDecoder(LengthDelimitedDecoder<Utf8Decoder>);
impl StringDecoder {
    /// Makes a new `StringDecoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl_newtype_decode!(StringDecoder, String, LengthDelimited);
impl MapKeyDecode for StringDecoder {}

/// Encoder for `string` values.
#[derive(Debug)]
pub struct StringEncoder<S = String>(LengthDelimitedEncoder<Utf8Encoder<S>>);
impl<S> StringEncoder<S> {
    /// Makes a new `StringEncoder` instance.
    pub fn new() -> Self {
        Self::default()
    }
}
impl<S> Default for StringEncoder<S> {
    fn default() -> Self {
        StringEncoder(Default::default())
    }
}
impl<S: AsRef<str>> Encode for StringEncoder<S> {
    type Item = S;

    fn encode(&mut self, buf: &mut [u8], eos: Eos) -> Result<usize> {
        track!(self.0.encode(buf, eos))
    }

    fn start_encoding(&mut self, item: Self::Item) -> Result<()> {
        track!(self.0.start_encoding(item))
    }

    fn is_idle(&self) -> bool {
        self.0.is_idle()
    }

    fn requiring_bytes(&self) -> ByteCount {
        self.0.requiring_bytes()
    }
}
impl<S: AsRef<str>> SizedEncode for StringEncoder<S> {
    fn exact_requiring_bytes(&self) -> u64 {
        self.0.exact_requiring_bytes()
    }
}
impl<S: AsRef<str>> ValueEncode for StringEncoder<S> {
    fn wire_type(&self) -> WireType {
        WireType::LengthDelimited
    }
}
impl<S: AsRef<str>> MapKeyEncode for StringEncoder<S> {}

#[cfg(test)]
mod test {
    use bytecodec::EncodeExt;
    use bytecodec::io::{IoDecodeExt, IoEncodeExt};

    use super::*;

    macro_rules! assert_decode {
        ($decoder:ident, $value:expr, $bytes:expr) => {
            let mut decoder = $decoder::new();
            let item = track_try_unwrap!(decoder.decode_exact($bytes.as_ref()));
            assert_eq!(item, $value);
        };
    }

    macro_rules! assert_encode {
        ($encoder:ident, $value:expr, $bytes:expr) => {
            let mut buf = Vec::new();
            let mut encoder = track_try_unwrap!($encoder::with_item($value));
            track_try_unwrap!(encoder.encode_all(&mut buf));
            assert_eq!(buf, $bytes);
        };
    }

    #[test]
    fn double_decoder_works() {
        assert_decode!(
            DoubleDecoder,
            1.23,
            [0xae, 0x47, 0xe1, 0x7a, 0x14, 0xae, 0xf3, 0x3f]
        );
    }

    #[test]
    fn double_encoder_works() {
        assert_encode!(
            DoubleEncoder,
            1.23,
            [0xae, 0x47, 0xe1, 0x7a, 0x14, 0xae, 0xf3, 0x3f]
        );
    }

    #[test]
    fn float_decoder_works() {
        assert_decode!(FloatDecoder, 3.25, [0x00, 0x00, 0x50, 0x40]);
    }

    #[test]
    fn float_encoder_works() {
        assert_encode!(FloatEncoder, 3.25, [0x00, 0x00, 0x50, 0x40]);
    }

    #[test]
    fn fixed32_decoder_works() {
        assert_decode!(Fixed32Decoder, 12345678, [0x4e, 0x61, 0xbc, 0x00]);
    }

    #[test]
    fn fixed32_encoder_works() {
        assert_encode!(Fixed32Encoder, 12345678, [0x4e, 0x61, 0xbc, 0x00]);
    }

    #[test]
    fn fixed64_decoder_works() {
        assert_decode!(
            Fixed64Decoder,
            1234567890987654321,
            [0xb1, 0x1c, 0x6c, 0xb1, 0xf4, 0x10, 0x22, 0x11]
        );
    }

    #[test]
    fn fixed64_encoder_works() {
        assert_encode!(
            Fixed64Encoder,
            1234567890987654321,
            [0xb1, 0x1c, 0x6c, 0xb1, 0xf4, 0x10, 0x22, 0x11]
        );
    }

    #[test]
    fn sfixed32_decoder_works() {
        assert_decode!(Sfixed32Decoder, -123456789, [0xeb, 0x32, 0xa4, 0xf8]);
    }

    #[test]
    fn sfixed32_encoder_works() {
        assert_encode!(Sfixed32Encoder, -123456789, [0xeb, 0x32, 0xa4, 0xf8]);
    }

    #[test]
    fn sfixed64_decoder_works() {
        assert_decode!(
            Sfixed64Decoder,
            -1234567890987654321,
            [0x4f, 0xe3, 0x93, 0x4e, 0x0b, 0xef, 0xdd, 0xee]
        );
    }

    #[test]
    fn sfixed64_encoder_works() {
        assert_encode!(
            Sfixed64Encoder,
            -1234567890987654321,
            [0x4f, 0xe3, 0x93, 0x4e, 0x0b, 0xef, 0xdd, 0xee]
        );
    }

    #[test]
    fn bool_decoder_works() {
        assert_decode!(BoolDecoder, false, [0x00]);
        assert_decode!(BoolDecoder, true, [0x01]);
        assert_decode!(BoolDecoder, true, [0xFF, 0xFF, 0x01]);
    }

    #[test]
    fn bool_encoder_works() {
        assert_encode!(BoolEncoder, false, [0x00]);
        assert_encode!(BoolEncoder, true, [0x01]);
    }

    #[test]
    fn int32_decoder_works() {
        assert_decode!(
            Int32Decoder,
            -12345678,
            [0xb2, 0xbd, 0x8e, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01]
        );
    }

    #[test]
    fn int32_encoder_works() {
        assert_encode!(
            Int32Encoder,
            -12345678,
            [0xb2, 0xbd, 0x8e, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01]
        );
    }

    #[test]
    fn int64_decoder_works() {
        assert_decode!(
            Int64Decoder,
            -12345678,
            [0xb2, 0xbd, 0x8e, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01]
        );
    }

    #[test]
    fn int64_encoder_works() {
        assert_encode!(
            Int64Encoder,
            -12345678,
            [0xb2, 0xbd, 0x8e, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01]
        );
    }

    #[test]
    fn uint32_decoder_works() {
        assert_decode!(Uint32Decoder, 12345678, [0xce, 0xc2, 0xf1, 0x05]);
    }

    #[test]
    fn uint32_encoder_works() {
        assert_encode!(Uint32Encoder, 12345678, [0xce, 0xc2, 0xf1, 0x05]);
    }

    #[test]
    fn uint64_decoder_works() {
        assert_decode!(Uint64Decoder, 12345678, [0xce, 0xc2, 0xf1, 0x05]);
    }

    #[test]
    fn uint64_encoder_works() {
        assert_encode!(Uint64Encoder, 12345678, [0xce, 0xc2, 0xf1, 0x05]);
    }

    #[test]
    fn sint32_decoder_works() {
        assert_decode!(Sint32Decoder, -1, [0x01]);
        assert_decode!(Sint32Decoder, -12345678, [0x9b, 0x85, 0xe3, 0x0b]);
        assert_decode!(Sint32Decoder, 12345678, [0x9c, 0x85, 0xe3, 0x0b]);
    }

    #[test]
    fn sint32_encoder_works() {
        assert_encode!(Sint32Encoder, -1, [0x01]);
        assert_encode!(Sint32Encoder, -12345678, [0x9b, 0x85, 0xe3, 0x0b]);
        assert_encode!(Sint32Encoder, 12345678, [0x9c, 0x85, 0xe3, 0x0b]);
    }

    #[test]
    fn sint64_decoder_works() {
        assert_decode!(Sint64Decoder, -1, [0x01]);
        assert_decode!(Sint64Decoder, -12345678, [0x9b, 0x85, 0xe3, 0x0b]);
        assert_decode!(Sint64Decoder, 12345678, [0x9c, 0x85, 0xe3, 0x0b]);
    }

    #[test]
    fn sint64_encoder_works() {
        assert_encode!(Sint64Encoder, -1, [0x01]);
        assert_encode!(Sint64Encoder, -12345678, [0x9b, 0x85, 0xe3, 0x0b]);
        assert_encode!(Sint64Encoder, 12345678, [0x9c, 0x85, 0xe3, 0x0b]);
    }

    #[test]
    fn bytes_decoder_works() {
        assert_decode!(BytesDecoder, [0, 1, 2, 3], [4, 0, 1, 2, 3]);
    }

    #[test]
    fn bytes_encoder_works() {
        assert_encode!(BytesEncoder, [0, 1, 2, 3], [4, 0, 1, 2, 3]);
    }

    #[test]
    fn string_decoder_works() {
        assert_decode!(StringDecoder, "foo", [3, 102, 111, 111]);
    }

    #[test]
    fn string_encoder_works() {
        assert_encode!(StringEncoder, "foo", [3, 102, 111, 111]);
    }
}