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 => {
266                use crate::config::IntEncoding;
267                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
268                    | IntEncoding::Variable => {
269                        crate::varint::varint_encode_usize(
270                            self.writer(),
271                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
272                            val,
273                        )
274                    },
275                    | IntEncoding::Fixed => self.encode_u64(val as u64),
276                }
277            },
278            | Format::Cbor | Format::CborDeterministic => {
279                cbor::encode_u64::<_, Self::C>(self.writer(), val as u64)
280            },
281        }
282    }
283
284    /// Encode an `i8` value.
285    ///
286    /// # Errors
287    ///
288    /// Returns `EncodeError` if the encoding fails.
289    #[inline(always)]
290    fn encode_i8(
291        &mut self,
292        val: i8,
293    ) -> Result<(), EncodeError> {
294        use crate::config::Format;
295        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
296            | Format::Bincode | Format::BincodeDeterministic => self.writer().write_u8(val as u8),
297            | Format::Cbor | Format::CborDeterministic => {
298                cbor::encode_i8::<_, Self::C>(self.writer(), val)
299            },
300        }
301    }
302
303    /// Encode an `i16` value.
304    ///
305    /// # Errors
306    ///
307    /// Returns `EncodeError` if the encoding fails.
308    #[inline(always)]
309    fn encode_i16(
310        &mut self,
311        val: i16,
312    ) -> Result<(), EncodeError> {
313        use crate::config::Endianness;
314        use crate::config::Format;
315        use crate::config::IntEncoding;
316        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
317            | Format::Bincode | Format::BincodeDeterministic => {
318                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
319                    | IntEncoding::Variable => {
320                        crate::varint::varint_encode_i16(
321                            self.writer(),
322                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
323                            val,
324                        )
325                    },
326                    | IntEncoding::Fixed => {
327                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
328                            | Endianness::Big => self.writer().write_u16(val.to_be() as u16),
329                            | Endianness::Little => self.writer().write_u16(val.to_le() as u16),
330                        }
331                    },
332                }
333            },
334            | Format::Cbor | Format::CborDeterministic => {
335                cbor::encode_i16::<_, Self::C>(self.writer(), val)
336            },
337        }
338    }
339
340    /// Encode an `i32` value.
341    ///
342    /// # Errors
343    ///
344    /// Returns `EncodeError` if the encoding fails.
345    #[inline(always)]
346    fn encode_i32(
347        &mut self,
348        val: i32,
349    ) -> Result<(), EncodeError> {
350        use crate::config::Endianness;
351        use crate::config::Format;
352        use crate::config::IntEncoding;
353        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
354            | Format::Bincode | Format::BincodeDeterministic => {
355                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
356                    | IntEncoding::Variable => {
357                        crate::varint::varint_encode_i32(
358                            self.writer(),
359                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
360                            val,
361                        )
362                    },
363                    | IntEncoding::Fixed => {
364                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
365                            | Endianness::Big => self.writer().write_u32(val.to_be() as u32),
366                            | Endianness::Little => self.writer().write_u32(val.to_le() as u32),
367                        }
368                    },
369                }
370            },
371            | Format::Cbor | Format::CborDeterministic => {
372                cbor::encode_i32::<_, Self::C>(self.writer(), val)
373            },
374        }
375    }
376
377    /// Encode an `i64` value.
378    ///
379    /// # Errors
380    ///
381    /// Returns `EncodeError` if the encoding fails.
382    #[inline(always)]
383    fn encode_i64(
384        &mut self,
385        val: i64,
386    ) -> Result<(), EncodeError> {
387        use crate::config::Endianness;
388        use crate::config::Format;
389        use crate::config::IntEncoding;
390        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
391            | Format::Bincode | Format::BincodeDeterministic => {
392                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
393                    | IntEncoding::Variable => {
394                        crate::varint::varint_encode_i64(
395                            self.writer(),
396                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
397                            val,
398                        )
399                    },
400                    | IntEncoding::Fixed => {
401                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
402                            | Endianness::Big => self.writer().write_u64(val.to_be() as u64),
403                            | Endianness::Little => self.writer().write_u64(val.to_le() as u64),
404                        }
405                    },
406                }
407            },
408            | Format::Cbor | Format::CborDeterministic => {
409                cbor::encode_i64::<_, Self::C>(self.writer(), val)
410            },
411        }
412    }
413
414    /// Encode an `i128` value.
415    ///
416    /// # Errors
417    ///
418    /// Returns `EncodeError` if the encoding fails.
419    #[inline(always)]
420    fn encode_i128(
421        &mut self,
422        val: i128,
423    ) -> Result<(), EncodeError> {
424        use crate::config::Endianness;
425        use crate::config::Format;
426        use crate::config::IntEncoding;
427        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
428            | Format::Bincode | Format::BincodeDeterministic => {
429                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
430                    | IntEncoding::Variable => {
431                        crate::varint::varint_encode_i128(
432                            self.writer(),
433                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
434                            val,
435                        )
436                    },
437                    | IntEncoding::Fixed => {
438                        match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
439                            | Endianness::Big => self.writer().write_u128(val.to_be() as u128),
440                            | Endianness::Little => self.writer().write_u128(val.to_le() as u128),
441                        }
442                    },
443                }
444            },
445            | Format::Cbor | Format::CborDeterministic => {
446                cbor::encode_i128::<_, Self::C>(self.writer(), val)
447            },
448        }
449    }
450
451    /// Encode an `isize` value.
452    ///
453    /// # Errors
454    ///
455    /// Returns `EncodeError` if the encoding fails.
456    #[inline(always)]
457    fn encode_isize(
458        &mut self,
459        val: isize,
460    ) -> Result<(), EncodeError> {
461        use crate::config::Format;
462        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
463            | Format::Bincode | Format::BincodeDeterministic => {
464                use crate::config::IntEncoding;
465                match <Self::C as crate::config::InternalIntEncodingConfig>::INT_ENCODING {
466                    | IntEncoding::Variable => {
467                        crate::varint::varint_encode_isize(
468                            self.writer(),
469                            <Self::C as crate::config::InternalEndianConfig>::ENDIAN,
470                            val,
471                        )
472                    },
473                    | IntEncoding::Fixed => self.encode_i64(val as i64),
474                }
475            },
476            | Format::Cbor | Format::CborDeterministic => {
477                cbor::encode_i64::<_, Self::C>(self.writer(), val as i64)
478            },
479        }
480    }
481
482    /// Encode an `f32` value.
483    ///
484    /// # Errors
485    ///
486    /// Returns `EncodeError` if the encoding fails.
487    #[inline(always)]
488    fn encode_f32(
489        &mut self,
490        val: f32,
491    ) -> Result<(), EncodeError> {
492        use crate::config::Endianness;
493        use crate::config::Format;
494        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
495            | Format::Bincode | Format::BincodeDeterministic => {
496                match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
497                    | Endianness::Big => self.writer().write_u32(val.to_bits().to_be()),
498                    | Endianness::Little => self.writer().write_u32(val.to_bits().to_le()),
499                }
500            },
501            | Format::Cbor | Format::CborDeterministic => {
502                cbor::encode_f32::<_, Self::C>(self.writer(), val)
503            },
504        }
505    }
506
507    /// Encode an `f64` value.
508    ///
509    /// # Errors
510    ///
511    /// Returns `EncodeError` if the encoding fails.
512    #[inline(always)]
513    fn encode_f64(
514        &mut self,
515        val: f64,
516    ) -> Result<(), EncodeError> {
517        use crate::config::Endianness;
518        use crate::config::Format;
519        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
520            | Format::Bincode | Format::BincodeDeterministic => {
521                match <Self::C as crate::config::InternalEndianConfig>::ENDIAN {
522                    | Endianness::Big => self.writer().write_u64(val.to_bits().to_be()),
523                    | Endianness::Little => self.writer().write_u64(val.to_bits().to_le()),
524                }
525            },
526            | Format::Cbor | Format::CborDeterministic => {
527                cbor::encode_f64::<_, Self::C>(self.writer(), val)
528            },
529        }
530    }
531
532    /// Encode a `bool` value.
533    ///
534    /// # Errors
535    ///
536    /// Returns `EncodeError` if the encoding fails.
537    #[inline(always)]
538    fn encode_bool(
539        &mut self,
540        val: bool,
541    ) -> Result<(), EncodeError> {
542        use crate::config::Format;
543        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
544            | Format::Bincode | Format::BincodeDeterministic => self.encode_u8(u8::from(val)),
545            | Format::Cbor | Format::CborDeterministic => {
546                cbor::encode_bool::<_, Self::C>(self.writer(), val)
547            },
548        }
549    }
550
551    /// Encode a string.
552    ///
553    /// # Errors
554    ///
555    /// Returns `EncodeError` if the encoding fails.
556    #[inline(always)]
557    fn encode_str(
558        &mut self,
559        val: &str,
560    ) -> Result<(), EncodeError> {
561        use crate::config::Format;
562        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
563            | Format::Bincode | Format::BincodeDeterministic => {
564                self.encode_slice_len(val.len())?;
565                self.writer().write(val.as_bytes())
566            },
567            | Format::Cbor | Format::CborDeterministic => {
568                cbor::encode_str::<_, Self::C>(self.writer(), val)
569            },
570        }
571    }
572
573    /// Encode the length of a slice.
574    ///
575    /// # Errors
576    ///
577    /// Returns `EncodeError` if the encoding fails.
578    #[inline(always)]
579    fn encode_slice_len(
580        &mut self,
581        len: usize,
582    ) -> Result<(), EncodeError> {
583        use crate::config::Format;
584        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
585            | Format::Bincode | Format::BincodeDeterministic => self.encode_u64(len as u64),
586            | Format::Cbor | Format::CborDeterministic => {
587                cbor::encode_slice_len::<_, Self::C>(self.writer(), len)
588            },
589        }
590    }
591
592    /// Encode the length of an array.
593    ///
594    /// # Errors
595    ///
596    /// Returns `EncodeError` if the encoding fails.
597    #[inline(always)]
598    fn encode_array_len(
599        &mut self,
600        len: usize,
601    ) -> Result<(), EncodeError> {
602        self.encode_slice_len(len)
603    }
604
605    /// Encode the length of a map.
606    ///
607    /// # Errors
608    ///
609    /// Returns `EncodeError` if the encoding fails.
610    #[inline(always)]
611    fn encode_map_len(
612        &mut self,
613        len: usize,
614    ) -> Result<(), EncodeError> {
615        use crate::config::Format;
616        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
617            | Format::Bincode | Format::BincodeDeterministic => self.encode_u64(len as u64),
618            | Format::Cbor | Format::CborDeterministic => {
619                cbor::encode_map_len::<_, Self::C>(self.writer(), len)
620            },
621        }
622    }
623
624    /// Encode a byte slice.
625    ///
626    /// # Errors
627    ///
628    /// Returns `EncodeError` if the encoding fails.
629    #[inline(always)]
630    fn encode_byte_slice(
631        &mut self,
632        val: &[u8],
633    ) -> Result<(), EncodeError> {
634        use crate::config::Format;
635        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
636            | Format::Bincode | Format::BincodeDeterministic => {
637                self.encode_slice_len(val.len())?;
638                self.writer().write(val)
639            },
640            | Format::Cbor | Format::CborDeterministic => {
641                cbor::encode_byte_slice::<_, Self::C>(self.writer(), val)
642            },
643        }
644    }
645
646    /// Encode a struct header.
647    ///
648    /// # Errors
649    ///
650    /// Returns `EncodeError` if the encoding fails.
651    #[inline(always)]
652    fn encode_struct_header(
653        &mut self,
654        len: usize,
655    ) -> Result<(), EncodeError> {
656        use crate::config::Format;
657        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
658            | Format::Bincode | Format::BincodeDeterministic => Ok(()),
659            | Format::Cbor | Format::CborDeterministic => self.encode_array_len(len),
660        }
661    }
662
663    /// Encode the length of a byte slice.
664    ///
665    /// # Errors
666    ///
667    /// Returns `EncodeError` if the encoding fails.
668    #[inline(always)]
669    fn encode_byte_slice_len(
670        &mut self,
671        len: usize,
672    ) -> Result<(), EncodeError> {
673        use crate::config::Format;
674        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
675            | Format::Bincode | Format::BincodeDeterministic => self.encode_usize(len),
676            | Format::Cbor | Format::CborDeterministic => {
677                cbor::encode_byte_slice_len::<_, Self::C>(self.writer(), len as u64)
678            },
679        }
680    }
681
682    /// Encode an enum variant index.
683    ///
684    /// Variant indices are always encoded as a single `u8` for Bincode format,
685    /// matching the decode side which uses `u8::decode()`.
686    ///
687    /// # Errors
688    ///
689    /// Returns `EncodeError` if the encoding fails.
690    #[inline(always)]
691    fn encode_variant_index(
692        &mut self,
693        idx: u32,
694    ) -> Result<(), EncodeError> {
695        use crate::config::Format;
696        match <Self::C as crate::config::InternalFormatConfig>::FORMAT {
697            | Format::Bincode | Format::BincodeDeterministic => self.encode_u32(idx),
698            | Format::Cbor | Format::CborDeterministic => {
699                cbor::encode_u32::<_, Self::C>(self.writer(), idx)
700            },
701        }
702    }
703}
704
705impl<T> crate::error_path::BincodeErrorPathCovered<1> for &mut T where
706    T: crate::error_path::BincodeErrorPathCovered<1>
707{
708}
709
710impl<T> Encoder for &mut T
711where
712    T: Encoder,
713{
714    type C = T::C;
715    type W = T::W;
716
717    #[inline(always)]
718    fn writer(&mut self) -> &mut Self::W {
719        T::writer(self)
720    }
721
722    #[inline(always)]
723    fn config(&self) -> &Self::C {
724        T::config(self)
725    }
726
727    #[inline(always)]
728    fn encode_u8(
729        &mut self,
730        val: u8,
731    ) -> Result<(), EncodeError> {
732        T::encode_u8(self, val)
733    }
734
735    #[inline(always)]
736    fn encode_u16(
737        &mut self,
738        val: u16,
739    ) -> Result<(), EncodeError> {
740        T::encode_u16(self, val)
741    }
742
743    #[inline(always)]
744    fn encode_u32(
745        &mut self,
746        val: u32,
747    ) -> Result<(), EncodeError> {
748        T::encode_u32(self, val)
749    }
750
751    #[inline(always)]
752    fn encode_u64(
753        &mut self,
754        val: u64,
755    ) -> Result<(), EncodeError> {
756        T::encode_u64(self, val)
757    }
758
759    #[inline(always)]
760    fn encode_u128(
761        &mut self,
762        val: u128,
763    ) -> Result<(), EncodeError> {
764        T::encode_u128(self, val)
765    }
766
767    #[inline(always)]
768    fn encode_usize(
769        &mut self,
770        val: usize,
771    ) -> Result<(), EncodeError> {
772        T::encode_usize(self, val)
773    }
774
775    #[inline(always)]
776    fn encode_i8(
777        &mut self,
778        val: i8,
779    ) -> Result<(), EncodeError> {
780        self.encode_u8(val as u8)
781    }
782
783    #[inline(always)]
784    fn encode_i16(
785        &mut self,
786        val: i16,
787    ) -> Result<(), EncodeError> {
788        T::encode_i16(self, val)
789    }
790
791    #[inline(always)]
792    fn encode_i32(
793        &mut self,
794        val: i32,
795    ) -> Result<(), EncodeError> {
796        T::encode_i32(self, val)
797    }
798
799    #[inline(always)]
800    fn encode_i64(
801        &mut self,
802        val: i64,
803    ) -> Result<(), EncodeError> {
804        T::encode_i64(self, val)
805    }
806
807    #[inline(always)]
808    fn encode_i128(
809        &mut self,
810        val: i128,
811    ) -> Result<(), EncodeError> {
812        T::encode_i128(self, val)
813    }
814
815    #[inline(always)]
816    fn encode_isize(
817        &mut self,
818        val: isize,
819    ) -> Result<(), EncodeError> {
820        T::encode_isize(self, val)
821    }
822
823    #[inline(always)]
824    fn encode_f32(
825        &mut self,
826        val: f32,
827    ) -> Result<(), EncodeError> {
828        T::encode_f32(self, val)
829    }
830
831    #[inline(always)]
832    fn encode_f64(
833        &mut self,
834        val: f64,
835    ) -> Result<(), EncodeError> {
836        T::encode_f64(self, val)
837    }
838
839    #[inline(always)]
840    fn encode_bool(
841        &mut self,
842        val: bool,
843    ) -> Result<(), EncodeError> {
844        T::encode_bool(self, val)
845    }
846
847    #[inline(always)]
848    fn encode_str(
849        &mut self,
850        val: &str,
851    ) -> Result<(), EncodeError> {
852        T::encode_str(self, val)
853    }
854
855    #[inline(always)]
856    fn encode_slice_len(
857        &mut self,
858        len: usize,
859    ) -> Result<(), EncodeError> {
860        T::encode_slice_len(self, len)
861    }
862
863    #[inline(always)]
864    fn encode_array_len(
865        &mut self,
866        len: usize,
867    ) -> Result<(), EncodeError> {
868        T::encode_array_len(self, len)
869    }
870
871    #[inline(always)]
872    fn encode_map_len(
873        &mut self,
874        len: usize,
875    ) -> Result<(), EncodeError> {
876        T::encode_map_len(self, len)
877    }
878
879    #[inline(always)]
880    fn encode_variant_index(
881        &mut self,
882        idx: u32,
883    ) -> Result<(), EncodeError> {
884        T::encode_variant_index(self, idx)
885    }
886
887    #[inline(always)]
888    fn encode_byte_slice(
889        &mut self,
890        val: &[u8],
891    ) -> Result<(), EncodeError> {
892        T::encode_byte_slice(self, val)
893    }
894
895    #[inline(always)]
896    fn encode_struct_header(
897        &mut self,
898        len: usize,
899    ) -> Result<(), EncodeError> {
900        T::encode_struct_header(self, len)
901    }
902}
903
904/// Encode the variant of the given option. Will not encode the option itself.
905///
906/// # Errors
907///
908/// Returns `EncodeError` if the encoding fails.
909#[inline(always)]
910pub(crate) fn encode_option_variant<E: Encoder, T>(
911    encoder: &mut E,
912    value: Option<&T>,
913) -> Result<(), EncodeError> {
914    E::assert_covered();
915    match value {
916        | None => 0u8.encode(encoder),
917        | Some(_) => 1u8.encode(encoder),
918    }
919}
920
921/// Encodes the length of any slice, container, etc into the given encoder
922///
923/// # Errors
924///
925/// Returns `EncodeError` if the encoding fails.
926#[inline(always)]
927pub(crate) fn encode_slice_len<E: Encoder>(
928    encoder: &mut E,
929    len: usize,
930) -> Result<(), EncodeError> {
931    E::assert_covered();
932    encoder.encode_slice_len(len)
933}