httlib_protos/encoder/
mod.rs

1//! Provides an implementation of the `proto3` encoder.
2//! 
3//! The encoder performs the task of transforming data fields into shrunken
4//! binary format. This sequence of bytes is much smaller than the original
5//! message which allows for much faster data transmission over the wire.
6//! 
7//! Property names are represented in the [Protocol Buffers] by unique numbers
8//! rather than strings. Compared to the raw JSON format, this already has a
9//! significant impact on the final size of the message that is then sent over
10//! the wire.
11//! 
12//! ```txt
13//! +-------------------+------------------+-------------------+
14//! +      1. JSON      +   2. Transform   +     3. Encode     + ENCODER
15//! +-------------------+------------------+-------------------+
16//! + {                 +                  +                   +
17//! +   "name": "John", + 1, John          + 0a 04 4a 6f 68 6e +
18//! +   "age": 35       + 2, 35            + 10 23             +
19//! + }                 +                  +                   +
20//! +-------------------+------------------+-------------------+
21//! +      6. JSON      +    5. Rebuild    +     4. Decode     + DECODER
22//! +-------------------+------------------+-------------------+
23//! ```
24//! 
25//! The encoder encodes a message into a binary format. The message is then
26//! represented on the wire as a kind of flattened sequence of encoded key-value
27//! properties. The key and the value are encoded separately. Each wire type has
28//! its own rules and therefore its own way of encoding.
29//! 
30//! ```txt
31//! [key1][value1][key2][value2] ... [keyN][valueN]
32//! ```
33
34mod error;
35mod lit;
36mod primitives;
37
38use std::io;
39use crate::Typ;
40pub use error::*;
41pub use lit::*;
42use primitives::*;
43
44/// Provides the encoding engine for Protocol Buffers.
45pub struct Encoder;
46
47impl Encoder {
48    /// A constant holding the minimum `tag` number of a field.
49    pub const TAG_MIN: u32 = 1;
50
51    /// A constant holding the maximum `tag` number of a field.
52    pub const TAG_MAX: u32 = (1 << 29) - 1;
53
54    /// Transforms a `field` into `proto3` binary format and writes the result
55    /// into `dst`.
56    /// 
57    /// By default, the encoder uses mostly "standard" variant formats for
58    /// numbers. A developer can choose a specific format by passing the 
59    /// `EncoderLit`.
60    /// 
61    /// ```rust
62    /// use httlib_protos::{Encoder, EncoderLit};
63    /// 
64    /// let encoder = Encoder::default();
65    /// 
66    /// let mut dst = Vec::new();
67    /// encoder.encode((&1, &150i32), &mut dst).unwrap();
68    /// encoder.encode((&2, EncoderLit::SInt32(&-150i32)), &mut dst).unwrap();
69    /// ```
70    /// 
71    /// On success the number of written bytes is returned otherwise an error is 
72    /// thrown.
73    pub fn encode<'a, W, F>(
74        &self,
75        field: (&u32, F),
76        dst: &mut W,
77    ) -> Result<usize, EncoderError>
78    where
79        F: Into<EncoderLit<'a>>,
80        W: ?Sized + io::Write,
81    {
82        match field.1.into() {
83            EncoderLit::Bool(val) => self.encode_bool(field.0, val, dst),
84            EncoderLit::BoolVec(val) => self.encode_bool_vec(field.0, val, dst),
85            EncoderLit::Int32(val) => self.encode_int32(field.0, val, dst),
86            EncoderLit::Int32Vec(val) => self.encode_int32_vec(field.0, val, dst),
87            EncoderLit::Int64(val) => self.encode_int64(field.0, val, dst),
88            EncoderLit::Int64Vec(val) => self.encode_int64_vec(field.0, val, dst),
89            EncoderLit::UInt32(val) => self.encode_uint32(field.0, val, dst),
90            EncoderLit::UInt32Vec(val) => self.encode_uint32_vec(field.0, val, dst),
91            EncoderLit::UInt64(val) => self.encode_uint64(field.0, val, dst),
92            EncoderLit::UInt64Vec(val) => self.encode_uint64_vec(field.0, val, dst),
93            EncoderLit::Float(val) => self.encode_float(field.0, val, dst),
94            EncoderLit::FloatVec(val) => self.encode_float_vec(field.0, val, dst),
95            EncoderLit::Double(val) => self.encode_double(field.0, val, dst),
96            EncoderLit::DoubleVec(val) => self.encode_double_vec(field.0, val, dst),
97            EncoderLit::SInt32(val) => self.encode_sint32(field.0, val, dst),
98            EncoderLit::SInt32Vec(val) => self.encode_sint32_vec(field.0, val, dst),
99            EncoderLit::SInt64(val) => self.encode_sint64(field.0, val, dst),
100            EncoderLit::SInt64Vec(val) => self.encode_sint64_vec(field.0, val, dst),
101            EncoderLit::Fixed32(val) => self.encode_fixed32(field.0, val, dst),
102            EncoderLit::Fixed32Vec(val) => self.encode_fixed32_vec(field.0, val, dst),
103            EncoderLit::Fixed64(val) => self.encode_fixed64(field.0, val, dst),
104            EncoderLit::Fixed64Vec(val) => self.encode_fixed64_vec(field.0, val, dst),
105            EncoderLit::SFixed32(val) => self.encode_sfixed32(field.0, val, dst),
106            EncoderLit::SFixed32Vec(val) => self.encode_sfixed32_vec(field.0, val, dst),
107            EncoderLit::SFixed64(val) => self.encode_sfixed64(field.0, val, dst),
108            EncoderLit::SFixed64Vec(val) => self.encode_sfixed64_vec(field.0, val, dst),
109            EncoderLit::Bytes(val) => self.encode_bytes(field.0, val, dst),
110        }
111    }
112
113    /// Encodes the provided `val` into `bool` field with a specific `tag`
114    /// number and writes the resulting bytes into `dst`.
115    /// 
116    /// On success the number of written bytes is returned otherwise an error is 
117    /// thrown.
118    pub fn encode_bool<W>(
119        &self,
120        tag: &u32,
121        val: &bool,
122        dst: &mut W,
123    ) -> Result<usize, EncoderError>
124    where
125        W: ?Sized + io::Write,
126    {
127        let mut size = 0;
128        size += encode_key(*tag, Typ::Varint, dst)?;
129        size += encode_bool(*val, dst)?;
130        Ok(size)
131    }
132
133    /// Encodes the provided `vals` into `bool` repeated field with a specific
134    /// `tag` number and writes the resulting bytes into `dst`.
135    /// 
136    /// On success the number of written bytes is returned otherwise an error is 
137    /// thrown.
138    pub fn encode_bool_vec<W>(
139        &self,
140        tag: &u32,
141        vals: &Vec<bool>,
142        dst: &mut W,
143    ) -> Result<usize, EncoderError>
144    where
145        W: ?Sized + io::Write,
146    {
147        let mut data = vec![];
148        for val in vals {
149            encode_bool(*val, &mut data)?;
150        }
151        let mut size = 0;
152        size += encode_key(*tag, Typ::LengthDelimited, dst)?;
153        size += encode_bytes(data, dst)?;
154        Ok(size)
155    }
156
157    /// Encodes the provided `val` into `int32` field with a specific `tag`
158    /// number and writes the resulting bytes into `dst`.
159    /// 
160    /// On success the number of written bytes is returned otherwise an error is 
161    /// thrown.
162    pub fn encode_int32<W>(
163        &self,
164        tag: &u32,
165        val: &i32,
166        dst: &mut W,
167    ) -> Result<usize, EncoderError>
168    where
169        W: ?Sized + io::Write,
170    {
171        let mut size = 0;
172        size += encode_key(*tag, Typ::Varint, dst)?;
173        size += encode_int32(*val, dst)?;
174        Ok(size)
175    }
176
177    /// Encodes the provided `vals` into `int32` repeated field with a specific
178    /// `tag` number and writes the resulting bytes into `dst`.
179    /// 
180    /// On success the number of written bytes is returned otherwise an error is 
181    /// thrown.
182    pub fn encode_int32_vec<W>(
183        &self,
184        tag: &u32,
185        vals: &Vec<i32>,
186        dst: &mut W,
187    ) -> Result<usize, EncoderError>
188    where
189        W: ?Sized + io::Write,
190    {
191        let mut data = vec![];
192        for val in vals {
193            encode_int32(*val, &mut data)?;
194        }
195        self.encode_bytes(tag, &data, dst)
196    }
197
198    /// Encodes the provided `val` into `int64` field with a specific `tag`
199    /// number and writes the resulting bytes into `dst`.
200    /// 
201    /// On success the number of written bytes is returned otherwise an error is 
202    /// thrown.
203    pub fn encode_int64<W>(
204        &self,
205        tag: &u32,
206        val: &i64,
207        dst: &mut W,
208    ) -> Result<usize, EncoderError>
209    where
210        W: ?Sized + io::Write,
211    {
212        let mut size = 0;
213        size += encode_key(*tag, Typ::Varint, dst)?;
214        size += encode_int64(*val, dst)?;
215        Ok(size)
216    }
217
218    /// Encodes the provided `vals` into `int64` repeated field with a specific
219    /// `tag` number and writes the resulting bytes into `dst`.
220    /// 
221    /// On success the number of written bytes is returned otherwise an error is 
222    /// thrown.
223    pub fn encode_int64_vec<W>(
224        &self,
225        tag: &u32,
226        vals: &Vec<i64>,
227        dst: &mut W,
228    ) -> Result<usize, EncoderError>
229    where
230        W: ?Sized + io::Write,
231    {
232        let mut data = vec![];
233        for val in vals {
234            encode_int64(*val, &mut data)?;
235        }
236        self.encode_bytes(tag, &data, dst)
237    }
238
239    /// Encodes the provided `val` into `uint32` field with a specific `tag`
240    /// number and writes the resulting bytes into `dst`.
241    /// 
242    /// On success the number of written bytes is returned otherwise an error is 
243    /// thrown.
244    pub fn encode_uint32<W>(
245        &self,
246        tag: &u32,
247        val: &u32,
248        dst: &mut W,
249    ) -> Result<usize, EncoderError>
250    where
251        W: ?Sized + io::Write,
252    {
253        let mut size = 0;
254        size += encode_key(*tag, Typ::Varint, dst)?;
255        size += encode_uint32(*val, dst)?;
256        Ok(size)
257    }
258
259    /// Encodes the provided `vals` into `uint32` repeated field with a
260    /// specific `tag` number and writes the resulting bytes into `dst`.
261    /// 
262    /// On success the number of written bytes is returned otherwise an error is 
263    /// thrown.
264    pub fn encode_uint32_vec<W>(
265        &self,
266        tag: &u32,
267        vals: &Vec<u32>,
268        dst: &mut W,
269    ) -> Result<usize, EncoderError>
270    where
271        W: ?Sized + io::Write,
272    {
273        let mut data = vec![];
274        for val in vals {
275            encode_uint32(*val, &mut data)?;
276        }
277        self.encode_bytes(tag, &data, dst)
278    }
279
280    /// Encodes the provided `val` into `uint64` field with a specific `tag`
281    /// number and writes the resulting bytes into `dst`.
282    /// 
283    /// On success the number of written bytes is returned otherwise an error is 
284    /// thrown.
285    pub fn encode_uint64<W>(
286        &self,
287        tag: &u32,
288        val: &u64,
289        dst: &mut W,
290    ) -> Result<usize, EncoderError>
291    where
292        W: ?Sized + io::Write,
293    {
294        let mut size = 0;
295        size += encode_key(*tag, Typ::Varint, dst)?;
296        size += encode_uint64(*val, dst)?;
297        Ok(size)
298    }
299
300    /// Encodes the provided `vals` into `uint64` repeated field with a
301    /// specific `tag` number and writes the resulting bytes into `dst`.
302    /// 
303    /// On success the number of written bytes is returned otherwise an error is 
304    /// thrown.
305    pub fn encode_uint64_vec<W>(
306        &self,
307        tag: &u32,
308        vals: &Vec<u64>,
309        dst: &mut W,
310    ) -> Result<usize, EncoderError>
311    where
312        W: ?Sized + io::Write,
313    {
314        let mut data = vec![];
315        for val in vals {
316            encode_uint64(*val, &mut data)?;
317        }
318        self.encode_bytes(tag, &data, dst)
319    }
320
321    /// Encodes the provided `val` into `float` field with a specific `tag`
322    /// number and writes the resulting bytes into `dst`.
323    /// 
324    /// On success the number of written bytes is returned otherwise an error is 
325    /// thrown.
326    pub fn encode_float<W>(
327        &self,
328        tag: &u32,
329        val: &f32,
330        dst: &mut W,
331    ) -> Result<usize, EncoderError>
332    where
333        W: ?Sized + io::Write,
334    {
335        let mut size = 0;
336        size += encode_key(*tag, Typ::Bit32, dst)?;
337        size += encode_float(*val, dst)?;
338        Ok(size)
339    }
340
341    /// Encodes the provided `vals` into `float` repeated field with a specific
342    /// `tag` number and writes the resulting bytes into `dst`.
343    /// 
344    /// On success the number of written bytes is returned otherwise an error is 
345    /// thrown.
346    pub fn encode_float_vec<W>(
347        &self,
348        tag: &u32,
349        vals: &Vec<f32>,
350        dst: &mut W,
351    ) -> Result<usize, EncoderError>
352    where
353        W: ?Sized + io::Write,
354    {
355        let mut data = vec![];
356        for val in vals {
357            encode_float(*val, &mut data)?;
358        }
359        self.encode_bytes(tag, &data, dst)
360    }
361
362    /// Encodes the provided `val` into `double` field with a specific `tag`
363    /// number and writes the resulting bytes into `dst`.
364    /// 
365    /// On success the number of written bytes is returned otherwise an error is 
366    /// thrown.
367    pub fn encode_double<W>(
368        &self,
369        tag: &u32,
370        val: &f64,
371        dst: &mut W,
372    ) -> Result<usize, EncoderError>
373    where
374        W: ?Sized + io::Write,
375    {
376        let mut size = 0;
377        size += encode_key(*tag, Typ::Bit64, dst)?;
378        size += encode_double(*val, dst)?;
379        Ok(size)
380    }
381
382    /// Encodes the provided `vals` into `double` repeated field with a
383    /// specific `tag` number and writes the resulting bytes into `dst`.
384    /// 
385    /// On success the number of written bytes is returned otherwise an error is 
386    /// thrown.
387    pub fn encode_double_vec<W>(
388        &self,
389        tag: &u32,
390        vals: &Vec<f64>,
391        dst: &mut W,
392    ) -> Result<usize, EncoderError>
393    where
394        W: ?Sized + io::Write,
395    {
396        let mut data = vec![];
397        for val in vals {
398            encode_double(*val, &mut data)?;
399        }
400        self.encode_bytes(tag, &data, dst)
401    }
402
403    /// Encodes the provided `val` into `bytes` field with a specific `tag`
404    /// number and writes the resulting bytes into `dst`.
405    /// 
406    /// On success the number of written bytes is returned otherwise an error is 
407    /// thrown.
408    pub fn encode_bytes<W>(
409        &self,
410        tag: &u32,
411        val: &Vec<u8>,
412        dst: &mut W,
413    ) -> Result<usize, EncoderError>
414    where
415        W: ?Sized + io::Write,
416    {
417        let mut size = 0;
418        size += encode_key(*tag, Typ::LengthDelimited, dst)?;
419        size += encode_bytes(val.clone(), dst)?;
420        Ok(size)
421    }
422
423    /// Encodes the provided `val` into `sint32` field with a specific `tag`
424    /// number and writes the resulting bytes into `dst`.
425    /// 
426    /// On success the number of written bytes is returned otherwise an error is 
427    /// thrown.
428    pub fn encode_sint32<W>(
429        &self,
430        tag: &u32,
431        val: &i32,
432        dst: &mut W,
433    ) -> Result<usize, EncoderError>
434    where
435        W: ?Sized + io::Write,
436    {
437        let mut size = 0;
438        size += encode_key(*tag, Typ::Varint, dst)?;
439        size += encode_sint32(*val, dst)?;
440        Ok(size)
441    }
442
443    /// Encodes the provided `vals` into `sin32` repeated field with a
444    /// specific `tag` number and writes the resulting bytes into `dst`.
445    /// 
446    /// On success the number of written bytes is returned otherwise an error is 
447    /// thrown.
448    pub fn encode_sint32_vec<W>(
449        &self,
450        tag: &u32,
451        vals: &Vec<i32>,
452        dst: &mut W,
453    ) -> Result<usize, EncoderError>
454    where
455        W: ?Sized + io::Write,
456    {
457        let mut data = vec![];
458        for val in vals {
459            encode_sint32(*val, &mut data)?;
460        }
461        self.encode_bytes(tag, &data, dst)
462    }
463
464    /// Encodes the provided `val` into `sint64` field with a specific `tag`
465    /// number and writes the resulting bytes into `dst`.
466    /// 
467    /// On success the number of written bytes is returned otherwise an error is 
468    /// thrown.
469    pub fn encode_sint64<W>(
470        &self,
471        tag: &u32,
472        val: &i64,
473        dst: &mut W,
474    ) -> Result<usize, EncoderError>
475    where
476        W: ?Sized + io::Write,
477    {
478        let mut size = 0;
479        size += encode_key(*tag, Typ::Varint, dst)?;
480        size += encode_sint64(*val, dst)?;
481        Ok(size)
482    }
483
484    /// Encodes the provided `vals` into `sin64` repeated field with a
485    /// specific `tag` number and writes the resulting bytes into `dst`.
486    /// 
487    /// On success the number of written bytes is returned otherwise an error is 
488    /// thrown.
489    pub fn encode_sint64_vec<W>(
490        &self,
491        tag: &u32,
492        vals: &Vec<i64>,
493        dst: &mut W,
494    ) -> Result<usize, EncoderError>
495    where
496        W: ?Sized + io::Write,
497    {
498        let mut data = vec![];
499        for val in vals {
500            encode_sint64(*val, &mut data)?;
501        }
502        self.encode_bytes(tag, &data, dst)
503    }
504    
505    /// Encodes the provided `val` into `fixed32` field with a specific `tag`
506    /// number and writes the resulting bytes into `dst`.
507    /// 
508    /// On success the number of written bytes is returned otherwise an error is 
509    /// thrown.
510    pub fn encode_fixed32<W>(
511        &self,
512        tag: &u32,
513        val: &u32,
514        dst: &mut W,
515    ) -> Result<usize, EncoderError>
516    where
517        W: ?Sized + io::Write,
518    {
519        let mut size = 0;
520        size += encode_key(*tag, Typ::Bit32, dst)?;
521        size += encode_fixed32(*val, dst)?;
522        Ok(size)
523    }
524
525    /// Encodes the provided `vals` into `fixed32` repeated field with a
526    /// specific `tag` number and writes the resulting bytes into `dst`.
527    /// 
528    /// On success the number of written bytes is returned otherwise an error is 
529    /// thrown.
530    pub fn encode_fixed32_vec<W>(
531        &self,
532        tag: &u32,
533        vals: &Vec<u32>,
534        dst: &mut W,
535    ) -> Result<usize, EncoderError>
536    where
537        W: ?Sized + io::Write,
538    {
539        let mut data = vec![];
540        for val in vals {
541            encode_fixed32(*val, &mut data)?;
542        }
543        self.encode_bytes(tag, &data, dst)
544    }
545
546    /// Encodes the provided `val` into `fixed64` field with a specific `tag`
547    /// number and writes the resulting bytes into `dst`.
548    /// 
549    /// On success the number of written bytes is returned otherwise an error is 
550    /// thrown.
551    pub fn encode_fixed64<W>(
552        &self,
553        tag: &u32,
554        val: &u64,
555        dst: &mut W,
556    ) -> Result<usize, EncoderError>
557    where
558        W: ?Sized + io::Write,
559    {
560        let mut size = 0;
561        size += encode_key(*tag, Typ::Bit64, dst)?;
562        size += encode_fixed64(*val, dst)?;
563        Ok(size)
564    }
565
566    /// Encodes the provided `vals` into `fixed64` repeated field with a
567    /// specific `tag` number and writes the resulting bytes into `dst`.
568    /// 
569    /// On success the number of written bytes is returned otherwise an error is 
570    /// thrown.
571    pub fn encode_fixed64_vec<W>(
572        &self,
573        tag: &u32,
574        vals: &Vec<u64>,
575        dst: &mut W,
576    ) -> Result<usize, EncoderError>
577    where
578        W: ?Sized + io::Write,
579    {
580        let mut data = vec![];
581        for val in vals {
582            encode_fixed64(*val, &mut data)?;
583        }
584        self.encode_bytes(tag, &data, dst)
585    }
586
587    /// Encodes the provided `val` into `sfixed32` field with a specific `tag`
588    /// number and writes the resulting bytes into `dst`.
589    /// 
590    /// On success the number of written bytes is returned otherwise an error is 
591    /// thrown.
592    pub fn encode_sfixed32<W>(
593        &self,
594        tag: &u32,
595        val: &i32,
596        dst: &mut W,
597    ) -> Result<usize, EncoderError>
598    where
599        W: ?Sized + io::Write,
600    {
601        let mut size = 0;
602        size += encode_key(*tag, Typ::Bit32, dst)?;
603        size += encode_sfixed32(*val, dst)?;
604        Ok(size)
605    }
606
607    /// Encodes the provided `vals` into `sfixed32` repeated field with a
608    /// specific `tag` number and writes the resulting bytes into `dst`.
609    /// 
610    /// On success the number of written bytes is returned otherwise an error is 
611    /// thrown.
612    pub fn encode_sfixed32_vec<W>(
613        &self,
614        tag: &u32,
615        vals: &Vec<i32>,
616        dst: &mut W,
617    ) -> Result<usize, EncoderError>
618    where
619        W: ?Sized + io::Write,
620    {
621        let mut data = vec![];
622        for val in vals {
623            encode_sfixed32(*val, &mut data)?;
624        }
625        self.encode_bytes(tag, &data, dst)
626    }
627
628    /// Encodes the provided `val` into `sfixed64` field with a specific `tag`
629    /// number and writes the resulting bytes into `dst`.
630    /// 
631    /// On success the number of written bytes is returned otherwise an error is 
632    /// thrown.
633    pub fn encode_sfixed64<W>(
634        &self,
635        tag: &u32,
636        val: &i64,
637        dst: &mut W,
638    ) -> Result<usize, EncoderError>
639    where
640        W: ?Sized + io::Write,
641    {
642        let mut size = 0;
643        size += encode_key(*tag, Typ::Bit64, dst)?;
644        size += encode_sfixed64(*val, dst)?;
645        Ok(size)
646    }
647
648    /// Encodes the provided `vals` into `sfixed64` repeated field with a
649    /// specific `tag` number and writes the resulting bytes into `dst`.
650    /// 
651    /// On success the number of written bytes is returned otherwise an error is 
652    /// thrown.
653    pub fn encode_sfixed64_vec<W>(
654        &self,
655        tag: &u32,
656        vals: &Vec<i64>,
657        dst: &mut W,
658    ) -> Result<usize, EncoderError>
659    where
660        W: ?Sized + io::Write,
661    {
662        let mut data = vec![];
663        for val in vals {
664            encode_sfixed64(*val, &mut data)?;
665        }
666        self.encode_bytes(tag, &data, dst)
667    }
668}
669
670impl Default for Encoder {
671    fn default() -> Self {
672        Self {}
673    }
674}
675
676#[cfg(test)]
677mod test {
678    use super::*;
679
680    /// Should encode supported data types into Protocol Buffers buffer.
681    #[test]
682    fn encodes_supported() {
683        let encoder = Encoder::default();
684        let mut dst = vec![];
685        let mut size = 0;
686        size += encoder.encode((&1, &b"foo".to_vec()), &mut dst).unwrap();
687        size += encoder.encode((&2, &true), &mut dst).unwrap();
688        size += encoder.encode((&3, &vec![false, true]), &mut dst).unwrap();
689        size += encoder.encode((&4, &1i32), &mut dst).unwrap();
690        size += encoder.encode((&5, &vec![-100i32, 100i32]), &mut dst).unwrap();
691        size += encoder.encode((&6, &1i64), &mut dst).unwrap();
692        size += encoder.encode((&7, &vec![-100i64, 100i64]), &mut dst).unwrap();
693        size += encoder.encode((&8, &1u32), &mut dst).unwrap();
694        size += encoder.encode((&9, &vec![1u32, 2u32]), &mut dst).unwrap();
695        size += encoder.encode((&10, &1u64), &mut dst).unwrap();
696        size += encoder.encode((&11, &vec![1u64, 2u64]), &mut dst).unwrap();
697        size += encoder.encode((&12, &1.0f32), &mut dst).unwrap();
698        size += encoder.encode((&13, &vec![1.0f32, 2.0f32]), &mut dst).unwrap();
699        size += encoder.encode((&14, &1.0f64), &mut dst).unwrap();
700        size += encoder.encode((&15, &vec![1.0f64, 2.0f64]), &mut dst).unwrap();
701        size += encoder.encode((&16, &b"foo".to_vec()), &mut dst).unwrap();
702        size += encoder.encode((&17, EncoderLit::SInt32(&-10)), &mut dst).unwrap();
703        size += encoder.encode((&18, EncoderLit::SInt32Vec(&vec![-10i32, 10i32])), &mut dst).unwrap();
704        size += encoder.encode((&19, EncoderLit::SInt64(&-10)), &mut dst).unwrap();
705        size += encoder.encode((&20, EncoderLit::SInt64Vec(&vec![-10i64, 10i64])), &mut dst).unwrap();
706        size += encoder.encode((&21, EncoderLit::Fixed32(&10)), &mut dst).unwrap();
707        size += encoder.encode((&22, EncoderLit::Fixed32Vec(&vec![1u32, 2u32])), &mut dst).unwrap();
708        size += encoder.encode((&23, EncoderLit::Fixed64(&10)), &mut dst).unwrap();
709        size += encoder.encode((&24, EncoderLit::Fixed64Vec(&vec![1u64, 2u64])), &mut dst).unwrap();
710        size += encoder.encode((&25, EncoderLit::SFixed32(&-10)), &mut dst).unwrap();
711        size += encoder.encode((&26, EncoderLit::SFixed32Vec(&vec![-10i32, 10i32])), &mut dst).unwrap();
712        size += encoder.encode((&27, EncoderLit::SFixed64(&-10)), &mut dst).unwrap();
713        size += encoder.encode((&28, EncoderLit::SFixed64Vec(&vec![-10i64, 10i64])), &mut dst).unwrap();
714        assert_eq!(dst, vec![
715            10, 3, 102, 111, 111,
716            16, 1,
717            26, 2, 0, 1,
718            32, 1,
719            42, 11, 156, 255, 255, 255, 255, 255, 255, 255, 255, 1, 100,
720            48, 1,
721            58, 11, 156, 255, 255, 255, 255, 255, 255, 255, 255, 1, 100,
722            64, 1,
723            74, 2, 1, 2,
724            80, 1,
725            90, 2, 1, 2,
726            101, 0, 0, 128, 63,
727            106, 8, 0, 0, 128, 63, 0, 0, 0, 64,
728            113, 0, 0, 0, 0, 0, 0, 240, 63,
729            122, 16, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 64,
730            130, 1, 3, 102, 111, 111,
731            136, 1, 19,
732            146, 1, 2, 19, 20,
733            152, 1, 19,
734            162, 1, 2, 19, 20,
735            173, 1, 10, 0, 0, 0,
736            178, 1, 8, 1, 0, 0, 0, 2, 0, 0, 0,
737            185, 1, 10, 0, 0, 0, 0, 0, 0, 0,
738            194, 1, 16, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0,
739            205, 1, 246, 255, 255, 255,
740            210, 1, 8, 246, 255, 255, 255, 10, 0, 0, 0,
741            217, 1, 246, 255, 255, 255, 255, 255, 255, 255,
742            226, 1, 16, 246, 255, 255, 255, 255, 255, 255, 255, 10, 0, 0, 0, 0, 0, 0, 0,
743        ]);
744        assert_eq!(size, 209);
745    }
746}