Skip to main content

bincode_next/enc/
mod.rs

1//! Encoder-based structs and traits.
2
3pub(crate) mod deterministic;
4mod encoder;
5mod impl_tuples;
6mod impls;
7
8use self::write::Writer;
9use crate::config::Config;
10use crate::error::EncodeError;
11use crate::utils::Sealed;
12
13/// Bit-level writer for space-optimized packing.
14pub mod bit_writer;
15pub(crate) mod cbor;
16pub mod write;
17
18pub use self::encoder::EncoderImpl;
19
20/// Any source that can be encoded. This trait should be implemented for all types that you want to be able to use with any of the `encode_with` methods.
21///
22/// This trait will be automatically implemented if you enable the `derive` feature and add `#[derive(bincode::Encode)]` to your trait.
23///
24/// # Implementing this trait manually
25///
26/// If you want to implement this trait for your type, the easiest way is to add a `#[derive(bincode::Encode)]`, build and check your `target/generated/bincode/` folder. This should generate a `<Struct name>_Encode.rs` file.
27///
28/// For this struct:
29///
30/// ```
31/// struct Entity {
32///     pub x: f32,
33///     pub y: f32,
34/// }
35/// ```
36/// It will look something like:
37///
38/// ```
39/// # struct Entity {
40/// #     pub x: f32,
41/// #     pub y: f32,
42/// # }
43/// impl bincode_next::Encode for Entity {
44///     fn encode<E: bincode_next::enc::Encoder>(
45///         &self,
46///         encoder: &mut E,
47///     ) -> core::result::Result<(), bincode_next::error::EncodeError> {
48///         bincode_next::Encode::encode(&self.x, encoder)?;
49///         bincode_next::Encode::encode(&self.y, encoder)?;
50///         Ok(())
51///     }
52/// }
53/// ```
54///
55/// From here you can add/remove fields, or add custom logic.
56pub trait Encode {
57    /// Encode a given type.
58    ///
59    /// # Errors
60    ///
61    /// Returns any error encountered during encoding.
62    fn encode<E: Encoder>(
63        &self,
64        encoder: &mut E,
65    ) -> Result<(), EncodeError>;
66}
67
68/// Helper trait to encode basic types into.
69pub trait Encoder: Sealed + crate::error_path::BincodeErrorPathCovered<1> {
70    /// The concrete [Writer] type
71    type W: Writer;
72
73    /// The concrete [Config] type
74    type C: Config;
75
76    /// Returns a mutable reference to the writer
77    fn writer(&mut self) -> &mut Self::W;
78
79    /// Returns a reference to the config
80    ///
81    /// # Errors
82    ///
83    /// Returns `EncodeError` if the encoding fails.
84    fn config(&self) -> &Self::C;
85
86    /// Encode a `u8` value.
87    ///
88    /// # Errors
89    ///
90    /// Returns `EncodeError` if the encoding fails.
91    #[inline(always)]
92    fn encode_u8(
93        &mut self,
94        val: u8,
95    ) -> Result<(), EncodeError> {
96        use crate::config::Format;
97        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
98            | Format::Bincode | Format::BincodeDeterministic => self.writer().write_u8(val),
99            | Format::Cbor | Format::CborDeterministic => {
100                cbor::encode_u8::<_, Self::C>(self.writer(), val)
101            },
102        }
103    }
104
105    /// Encode a `u16` value.
106    ///
107    /// # Errors
108    ///
109    /// Returns `EncodeError` if the encoding fails.
110    #[inline(always)]
111    fn encode_u16(
112        &mut self,
113        val: u16,
114    ) -> Result<(), EncodeError> {
115        use crate::config::Endianness;
116        use crate::config::Format;
117        use crate::config::IntEncoding;
118        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
119            | Format::Bincode | Format::BincodeDeterministic => {
120                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
121                    | IntEncoding::Variable => {
122                        crate::varint::varint_encode_u16(
123                            self.writer(),
124                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
125                            val,
126                        )
127                    },
128                    | IntEncoding::Fixed => {
129                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
130                            | Endianness::Big => self.writer().write_u16(val.to_be()),
131                            | Endianness::Little => self.writer().write_u16(val.to_le()),
132                        }
133                    },
134                }
135            },
136            | Format::Cbor | Format::CborDeterministic => {
137                cbor::encode_u16::<_, Self::C>(self.writer(), val)
138            },
139        }
140    }
141
142    /// Encode a `u32` value.
143    ///
144    /// # Errors
145    ///
146    /// Returns `EncodeError` if the encoding fails.
147    #[inline(always)]
148    fn encode_u32(
149        &mut self,
150        val: u32,
151    ) -> Result<(), EncodeError> {
152        use crate::config::Endianness;
153        use crate::config::Format;
154        use crate::config::IntEncoding;
155        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
156            | Format::Bincode | Format::BincodeDeterministic => {
157                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
158                    | IntEncoding::Variable => {
159                        crate::varint::varint_encode_u32(
160                            self.writer(),
161                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
162                            val,
163                        )
164                    },
165                    | IntEncoding::Fixed => {
166                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
167                            | Endianness::Big => self.writer().write_u32(val.to_be()),
168                            | Endianness::Little => self.writer().write_u32(val.to_le()),
169                        }
170                    },
171                }
172            },
173            | Format::Cbor | Format::CborDeterministic => {
174                cbor::encode_u32::<_, Self::C>(self.writer(), val)
175            },
176        }
177    }
178
179    /// Encode a `u64` value.
180    ///
181    /// # Errors
182    ///
183    /// Returns `EncodeError` if the encoding fails.
184    #[inline(always)]
185    fn encode_u64(
186        &mut self,
187        val: u64,
188    ) -> Result<(), EncodeError> {
189        use crate::config::Endianness;
190        use crate::config::Format;
191        use crate::config::IntEncoding;
192        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
193            | Format::Bincode | Format::BincodeDeterministic => {
194                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
195                    | IntEncoding::Variable => {
196                        crate::varint::varint_encode_u64(
197                            self.writer(),
198                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
199                            val,
200                        )
201                    },
202                    | IntEncoding::Fixed => {
203                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
204                            | Endianness::Big => self.writer().write_u64(val.to_be()),
205                            | Endianness::Little => self.writer().write_u64(val.to_le()),
206                        }
207                    },
208                }
209            },
210            | Format::Cbor | Format::CborDeterministic => {
211                cbor::encode_u64::<_, Self::C>(self.writer(), val)
212            },
213        }
214    }
215
216    /// Encode a `u128` value.
217    ///
218    /// # Errors
219    ///
220    /// Returns `EncodeError` if the encoding fails.
221    #[inline(always)]
222    fn encode_u128(
223        &mut self,
224        val: u128,
225    ) -> Result<(), EncodeError> {
226        use crate::config::Endianness;
227        use crate::config::Format;
228        use crate::config::IntEncoding;
229        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
230            | Format::Bincode | Format::BincodeDeterministic => {
231                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
232                    | IntEncoding::Variable => {
233                        crate::varint::varint_encode_u128(
234                            self.writer(),
235                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
236                            val,
237                        )
238                    },
239                    | IntEncoding::Fixed => {
240                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
241                            | Endianness::Big => self.writer().write_u128(val.to_be()),
242                            | Endianness::Little => self.writer().write_u128(val.to_le()),
243                        }
244                    },
245                }
246            },
247            | Format::Cbor | Format::CborDeterministic => {
248                cbor::encode_u128::<_, Self::C>(self.writer(), val)
249            },
250        }
251    }
252
253    /// Encode a `usize` value.
254    ///
255    /// # Errors
256    ///
257    /// Returns `EncodeError` if the encoding fails.
258    #[inline(always)]
259    fn encode_usize(
260        &mut self,
261        val: usize,
262    ) -> Result<(), EncodeError> {
263        use crate::config::Format;
264        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
265            | Format::Bincode | Format::BincodeDeterministic => self.encode_u64(val as u64),
266            | Format::Cbor | Format::CborDeterministic => {
267                cbor::encode_u64::<_, Self::C>(self.writer(), val as u64)
268            },
269        }
270    }
271
272    /// Encode an `i8` value.
273    ///
274    /// # Errors
275    ///
276    /// Returns `EncodeError` if the encoding fails.
277    #[inline(always)]
278    fn encode_i8(
279        &mut self,
280        val: i8,
281    ) -> Result<(), EncodeError> {
282        use crate::config::Format;
283        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
284            | Format::Bincode | Format::BincodeDeterministic => self.writer().write_u8(val as u8),
285            | Format::Cbor | Format::CborDeterministic => {
286                cbor::encode_i8::<_, Self::C>(self.writer(), val)
287            },
288        }
289    }
290
291    /// Encode an `i16` value.
292    ///
293    /// # Errors
294    ///
295    /// Returns `EncodeError` if the encoding fails.
296    #[inline(always)]
297    fn encode_i16(
298        &mut self,
299        val: i16,
300    ) -> Result<(), EncodeError> {
301        use crate::config::Endianness;
302        use crate::config::Format;
303        use crate::config::IntEncoding;
304        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
305            | Format::Bincode | Format::BincodeDeterministic => {
306                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
307                    | IntEncoding::Variable => {
308                        crate::varint::varint_encode_i16(
309                            self.writer(),
310                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
311                            val,
312                        )
313                    },
314                    | IntEncoding::Fixed => {
315                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
316                            | Endianness::Big => self.writer().write_u16(val.to_be() as u16),
317                            | Endianness::Little => self.writer().write_u16(val.to_le() as u16),
318                        }
319                    },
320                }
321            },
322            | Format::Cbor | Format::CborDeterministic => {
323                cbor::encode_i16::<_, Self::C>(self.writer(), val)
324            },
325        }
326    }
327
328    /// Encode an `i32` value.
329    ///
330    /// # Errors
331    ///
332    /// Returns `EncodeError` if the encoding fails.
333    #[inline(always)]
334    fn encode_i32(
335        &mut self,
336        val: i32,
337    ) -> Result<(), EncodeError> {
338        use crate::config::Endianness;
339        use crate::config::Format;
340        use crate::config::IntEncoding;
341        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
342            | Format::Bincode | Format::BincodeDeterministic => {
343                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
344                    | IntEncoding::Variable => {
345                        crate::varint::varint_encode_i32(
346                            self.writer(),
347                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
348                            val,
349                        )
350                    },
351                    | IntEncoding::Fixed => {
352                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
353                            | Endianness::Big => self.writer().write_u32(val.to_be() as u32),
354                            | Endianness::Little => self.writer().write_u32(val.to_le() as u32),
355                        }
356                    },
357                }
358            },
359            | Format::Cbor | Format::CborDeterministic => {
360                cbor::encode_i32::<_, Self::C>(self.writer(), val)
361            },
362        }
363    }
364
365    /// Encode an `i64` value.
366    ///
367    /// # Errors
368    ///
369    /// Returns `EncodeError` if the encoding fails.
370    #[inline(always)]
371    fn encode_i64(
372        &mut self,
373        val: i64,
374    ) -> Result<(), EncodeError> {
375        use crate::config::Endianness;
376        use crate::config::Format;
377        use crate::config::IntEncoding;
378        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
379            | Format::Bincode | Format::BincodeDeterministic => {
380                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
381                    | IntEncoding::Variable => {
382                        crate::varint::varint_encode_i64(
383                            self.writer(),
384                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
385                            val,
386                        )
387                    },
388                    | IntEncoding::Fixed => {
389                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
390                            | Endianness::Big => self.writer().write_u64(val.to_be() as u64),
391                            | Endianness::Little => self.writer().write_u64(val.to_le() as u64),
392                        }
393                    },
394                }
395            },
396            | Format::Cbor | Format::CborDeterministic => {
397                cbor::encode_i64::<_, Self::C>(self.writer(), val)
398            },
399        }
400    }
401
402    /// Encode an `i128` value.
403    ///
404    /// # Errors
405    ///
406    /// Returns `EncodeError` if the encoding fails.
407    #[inline(always)]
408    fn encode_i128(
409        &mut self,
410        val: i128,
411    ) -> Result<(), EncodeError> {
412        use crate::config::Endianness;
413        use crate::config::Format;
414        use crate::config::IntEncoding;
415        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
416            | Format::Bincode | Format::BincodeDeterministic => {
417                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
418                    | IntEncoding::Variable => {
419                        crate::varint::varint_encode_i128(
420                            self.writer(),
421                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
422                            val,
423                        )
424                    },
425                    | IntEncoding::Fixed => {
426                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
427                            | Endianness::Big => self.writer().write_u128(val.to_be() as u128),
428                            | Endianness::Little => self.writer().write_u128(val.to_le() as u128),
429                        }
430                    },
431                }
432            },
433            | Format::Cbor | Format::CborDeterministic => {
434                cbor::encode_i128::<_, Self::C>(self.writer(), val)
435            },
436        }
437    }
438
439    /// Encode an `isize` value.
440    ///
441    /// # Errors
442    ///
443    /// Returns `EncodeError` if the encoding fails.
444    #[inline(always)]
445    fn encode_isize(
446        &mut self,
447        val: isize,
448    ) -> Result<(), EncodeError> {
449        use crate::config::Format;
450        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
451            | Format::Bincode | Format::BincodeDeterministic => self.encode_i64(val as i64),
452            | Format::Cbor | Format::CborDeterministic => {
453                cbor::encode_i64::<_, Self::C>(self.writer(), val as i64)
454            },
455        }
456    }
457
458    /// Encode an `f32` value.
459    ///
460    /// # Errors
461    ///
462    /// Returns `EncodeError` if the encoding fails.
463    #[inline(always)]
464    fn encode_f32(
465        &mut self,
466        val: f32,
467    ) -> Result<(), EncodeError> {
468        use crate::config::Endianness;
469        use crate::config::Format;
470        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
471            | Format::Bincode | Format::BincodeDeterministic => {
472                match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
473                    | Endianness::Big => self.writer().write_u32(val.to_bits().to_be()),
474                    | Endianness::Little => self.writer().write_u32(val.to_bits().to_le()),
475                }
476            },
477            | Format::Cbor | Format::CborDeterministic => {
478                cbor::encode_f32::<_, Self::C>(self.writer(), val)
479            },
480        }
481    }
482
483    /// Encode an `f64` value.
484    ///
485    /// # Errors
486    ///
487    /// Returns `EncodeError` if the encoding fails.
488    #[inline(always)]
489    fn encode_f64(
490        &mut self,
491        val: f64,
492    ) -> Result<(), EncodeError> {
493        use crate::config::Endianness;
494        use crate::config::Format;
495        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
496            | Format::Bincode | Format::BincodeDeterministic => {
497                match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
498                    | Endianness::Big => self.writer().write_u64(val.to_bits().to_be()),
499                    | Endianness::Little => self.writer().write_u64(val.to_bits().to_le()),
500                }
501            },
502            | Format::Cbor | Format::CborDeterministic => {
503                cbor::encode_f64::<_, Self::C>(self.writer(), val)
504            },
505        }
506    }
507
508    /// Encode a `bool` value.
509    ///
510    /// # Errors
511    ///
512    /// Returns `EncodeError` if the encoding fails.
513    #[inline(always)]
514    fn encode_bool(
515        &mut self,
516        val: bool,
517    ) -> Result<(), EncodeError> {
518        use crate::config::Format;
519        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
520            | Format::Bincode | Format::BincodeDeterministic => self.encode_u8(u8::from(val)),
521            | Format::Cbor | Format::CborDeterministic => {
522                cbor::encode_bool::<_, Self::C>(self.writer(), val)
523            },
524        }
525    }
526
527    /// Encode a string.
528    ///
529    /// # Errors
530    ///
531    /// Returns `EncodeError` if the encoding fails.
532    #[inline(always)]
533    fn encode_str(
534        &mut self,
535        val: &str,
536    ) -> Result<(), EncodeError> {
537        use crate::config::Format;
538        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
539            | Format::Bincode | Format::BincodeDeterministic => {
540                self.encode_slice_len(val.len())?;
541                self.writer().write(val.as_bytes())
542            },
543            | Format::Cbor | Format::CborDeterministic => {
544                cbor::encode_str::<_, Self::C>(self.writer(), val)
545            },
546        }
547    }
548
549    /// Encode the length of a slice.
550    ///
551    /// # Errors
552    ///
553    /// Returns `EncodeError` if the encoding fails.
554    #[inline(always)]
555    fn encode_slice_len(
556        &mut self,
557        len: usize,
558    ) -> Result<(), EncodeError> {
559        use crate::config::Format;
560        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
561            | Format::Bincode | Format::BincodeDeterministic => self.encode_u64(len as u64),
562            | Format::Cbor | Format::CborDeterministic => {
563                cbor::encode_slice_len::<_, Self::C>(self.writer(), len)
564            },
565        }
566    }
567
568    /// Encode the length of an array.
569    ///
570    /// # Errors
571    ///
572    /// Returns `EncodeError` if the encoding fails.
573    #[inline(always)]
574    fn encode_array_len(
575        &mut self,
576        len: usize,
577    ) -> Result<(), EncodeError> {
578        self.encode_slice_len(len)
579    }
580
581    /// Encode the length of a map.
582    ///
583    /// # Errors
584    ///
585    /// Returns `EncodeError` if the encoding fails.
586    #[inline(always)]
587    fn encode_map_len(
588        &mut self,
589        len: usize,
590    ) -> Result<(), EncodeError> {
591        use crate::config::Format;
592        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
593            | Format::Bincode | Format::BincodeDeterministic => self.encode_u64(len as u64),
594            | Format::Cbor | Format::CborDeterministic => {
595                cbor::encode_map_len::<_, Self::C>(self.writer(), len)
596            },
597        }
598    }
599
600    /// Encode a byte slice.
601    ///
602    /// # Errors
603    ///
604    /// Returns `EncodeError` if the encoding fails.
605    #[inline(always)]
606    fn encode_byte_slice(
607        &mut self,
608        val: &[u8],
609    ) -> Result<(), EncodeError> {
610        use crate::config::Format;
611        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
612            | Format::Bincode | Format::BincodeDeterministic => {
613                self.encode_slice_len(val.len())?;
614                self.writer().write(val)
615            },
616            | Format::Cbor | Format::CborDeterministic => {
617                cbor::encode_byte_slice::<_, Self::C>(self.writer(), val)
618            },
619        }
620    }
621
622    /// Encode a struct header.
623    ///
624    /// # Errors
625    ///
626    /// Returns `EncodeError` if the encoding fails.
627    #[inline(always)]
628    fn encode_struct_header(
629        &mut self,
630        len: usize,
631    ) -> Result<(), EncodeError> {
632        use crate::config::Format;
633        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
634            | Format::Bincode | Format::BincodeDeterministic => Ok(()),
635            | Format::Cbor | Format::CborDeterministic => self.encode_array_len(len),
636        }
637    }
638
639    /// Encode the length of a byte slice.
640    ///
641    /// # Errors
642    ///
643    /// Returns `EncodeError` if the encoding fails.
644    #[inline(always)]
645    fn encode_byte_slice_len(
646        &mut self,
647        len: usize,
648    ) -> Result<(), EncodeError> {
649        use crate::config::Format;
650        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
651            | Format::Bincode | Format::BincodeDeterministic => self.encode_usize(len),
652            | Format::Cbor | Format::CborDeterministic => {
653                cbor::encode_byte_slice_len::<_, Self::C>(self.writer(), len as u64)
654            },
655        }
656    }
657
658    /// Encode an enum variant index.
659    ///
660    /// Variant indices are always encoded as a single `u8` for Bincode format,
661    /// matching the decode side which uses `u8::decode()`.
662    ///
663    /// # Errors
664    ///
665    /// Returns `EncodeError` if the encoding fails.
666    #[inline(always)]
667    fn encode_variant_index(
668        &mut self,
669        idx: u32,
670    ) -> Result<(), EncodeError> {
671        use crate::config::Format;
672        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
673            | Format::Bincode | Format::BincodeDeterministic => self.encode_u8(idx as u8),
674            | Format::Cbor | Format::CborDeterministic => {
675                cbor::encode_u32::<_, Self::C>(self.writer(), idx)
676            },
677        }
678    }
679}
680
681impl<T> crate::error_path::BincodeErrorPathCovered<1> for &mut T where
682    T: crate::error_path::BincodeErrorPathCovered<1>
683{
684}
685
686impl<T> Encoder for &mut T
687where
688    T: Encoder,
689{
690    type C = T::C;
691    type W = T::W;
692
693    #[inline(always)]
694    fn writer(&mut self) -> &mut Self::W {
695        T::writer(self)
696    }
697
698    #[inline(always)]
699    fn config(&self) -> &Self::C {
700        T::config(self)
701    }
702
703    #[inline(always)]
704    fn encode_u8(
705        &mut self,
706        val: u8,
707    ) -> Result<(), EncodeError> {
708        T::encode_u8(self, val)
709    }
710
711    #[inline(always)]
712    fn encode_u16(
713        &mut self,
714        val: u16,
715    ) -> Result<(), EncodeError> {
716        T::encode_u16(self, val)
717    }
718
719    #[inline(always)]
720    fn encode_u32(
721        &mut self,
722        val: u32,
723    ) -> Result<(), EncodeError> {
724        T::encode_u32(self, val)
725    }
726
727    #[inline(always)]
728    fn encode_u64(
729        &mut self,
730        val: u64,
731    ) -> Result<(), EncodeError> {
732        T::encode_u64(self, val)
733    }
734
735    #[inline(always)]
736    fn encode_u128(
737        &mut self,
738        val: u128,
739    ) -> Result<(), EncodeError> {
740        T::encode_u128(self, val)
741    }
742
743    #[inline(always)]
744    fn encode_usize(
745        &mut self,
746        val: usize,
747    ) -> Result<(), EncodeError> {
748        T::encode_usize(self, val)
749    }
750
751    #[inline(always)]
752    fn encode_i8(
753        &mut self,
754        val: i8,
755    ) -> Result<(), EncodeError> {
756        self.encode_u8(val as u8)
757    }
758
759    #[inline(always)]
760    fn encode_i16(
761        &mut self,
762        val: i16,
763    ) -> Result<(), EncodeError> {
764        T::encode_i16(self, val)
765    }
766
767    #[inline(always)]
768    fn encode_i32(
769        &mut self,
770        val: i32,
771    ) -> Result<(), EncodeError> {
772        T::encode_i32(self, val)
773    }
774
775    #[inline(always)]
776    fn encode_i64(
777        &mut self,
778        val: i64,
779    ) -> Result<(), EncodeError> {
780        T::encode_i64(self, val)
781    }
782
783    #[inline(always)]
784    fn encode_i128(
785        &mut self,
786        val: i128,
787    ) -> Result<(), EncodeError> {
788        T::encode_i128(self, val)
789    }
790
791    #[inline(always)]
792    fn encode_isize(
793        &mut self,
794        val: isize,
795    ) -> Result<(), EncodeError> {
796        T::encode_isize(self, val)
797    }
798
799    #[inline(always)]
800    fn encode_f32(
801        &mut self,
802        val: f32,
803    ) -> Result<(), EncodeError> {
804        T::encode_f32(self, val)
805    }
806
807    #[inline(always)]
808    fn encode_f64(
809        &mut self,
810        val: f64,
811    ) -> Result<(), EncodeError> {
812        T::encode_f64(self, val)
813    }
814
815    #[inline(always)]
816    fn encode_bool(
817        &mut self,
818        val: bool,
819    ) -> Result<(), EncodeError> {
820        T::encode_bool(self, val)
821    }
822
823    #[inline(always)]
824    fn encode_str(
825        &mut self,
826        val: &str,
827    ) -> Result<(), EncodeError> {
828        T::encode_str(self, val)
829    }
830
831    #[inline(always)]
832    fn encode_slice_len(
833        &mut self,
834        len: usize,
835    ) -> Result<(), EncodeError> {
836        T::encode_slice_len(self, len)
837    }
838
839    #[inline(always)]
840    fn encode_array_len(
841        &mut self,
842        len: usize,
843    ) -> Result<(), EncodeError> {
844        T::encode_array_len(self, len)
845    }
846
847    #[inline(always)]
848    fn encode_map_len(
849        &mut self,
850        len: usize,
851    ) -> Result<(), EncodeError> {
852        T::encode_map_len(self, len)
853    }
854
855    #[inline(always)]
856    fn encode_variant_index(
857        &mut self,
858        idx: u32,
859    ) -> Result<(), EncodeError> {
860        T::encode_variant_index(self, idx)
861    }
862
863    #[inline(always)]
864    fn encode_byte_slice(
865        &mut self,
866        val: &[u8],
867    ) -> Result<(), EncodeError> {
868        T::encode_byte_slice(self, val)
869    }
870
871    #[inline(always)]
872    fn encode_struct_header(
873        &mut self,
874        len: usize,
875    ) -> Result<(), EncodeError> {
876        T::encode_struct_header(self, len)
877    }
878}
879
880/// Encode the variant of the given option. Will not encode the option itself.
881///
882/// # Errors
883///
884/// Returns `EncodeError` if the encoding fails.
885#[inline(always)]
886pub(crate) fn encode_option_variant<E: Encoder, T>(
887    encoder: &mut E,
888    value: Option<&T>,
889) -> Result<(), EncodeError> {
890    E::assert_covered();
891    match value {
892        | None => 0u8.encode(encoder),
893        | Some(_) => 1u8.encode(encoder),
894    }
895}
896
897/// Encodes the length of any slice, container, etc into the given encoder
898///
899/// # Errors
900///
901/// Returns `EncodeError` if the encoding fails.
902#[inline(always)]
903pub(crate) fn encode_slice_len<E: Encoder>(
904    encoder: &mut E,
905    len: usize,
906) -> Result<(), EncodeError> {
907    E::assert_covered();
908    encoder.encode_slice_len(len)
909}