cu_bincode/enc/
mod.rs

1//! Encoder-based structs and traits.
2
3mod encoder;
4mod impl_tuples;
5mod impls;
6
7use self::write::Writer;
8use crate::{config::Config, error::EncodeError, utils::Sealed};
9
10pub mod write;
11
12pub use self::encoder::EncoderImpl;
13
14/// 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.
15///
16/// This trait will be automatically implemented if you enable the `derive` feature and add `#[derive(bincode::Encode)]` to your trait.
17///
18/// # Implementing this trait manually
19///
20/// 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.
21///
22/// For this struct:
23///
24/// ```
25/// # extern crate cu_bincode as bincode;
26/// struct Entity {
27///     pub x: f32,
28///     pub y: f32,
29/// }
30/// ```
31/// It will look something like:
32///
33/// ```
34/// # extern crate cu_bincode as bincode;
35/// # struct Entity {
36/// #     pub x: f32,
37/// #     pub y: f32,
38/// # }
39/// impl bincode::Encode for Entity {
40///     fn encode<E: bincode::enc::Encoder>(
41///         &self,
42///         encoder: &mut E,
43///     ) -> core::result::Result<(), bincode::error::EncodeError> {
44///         bincode::Encode::encode(&self.x, encoder)?;
45///         bincode::Encode::encode(&self.y, encoder)?;
46///         Ok(())
47///     }
48/// }
49/// ```
50/// # extern crate cu_bincode as bincode;
51///
52/// From here you can add/remove fields, or add custom logic.
53pub trait Encode {
54    /// Encode a given type.
55    fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError>;
56}
57
58/// Helper trait to encode basic types into.
59pub trait Encoder: Sealed {
60    /// The concrete [Writer] type
61    type W: Writer;
62
63    /// The concrete [Config] type
64    type C: Config;
65
66    /// Returns a mutable reference to the writer
67    fn writer(&mut self) -> &mut Self::W;
68
69    /// Returns a reference to the config
70    fn config(&self) -> &Self::C;
71}
72
73impl<T> Encoder for &mut T
74where
75    T: Encoder,
76{
77    type W = T::W;
78
79    type C = T::C;
80
81    fn writer(&mut self) -> &mut Self::W {
82        T::writer(self)
83    }
84
85    fn config(&self) -> &Self::C {
86        T::config(self)
87    }
88}
89
90/// Encode the variant of the given option. Will not encode the option itself.
91#[inline]
92pub(crate) fn encode_option_variant<E: Encoder, T>(
93    encoder: &mut E,
94    value: &Option<T>,
95) -> Result<(), EncodeError> {
96    match value {
97        None => 0u8.encode(encoder),
98        Some(_) => 1u8.encode(encoder),
99    }
100}
101
102/// Encodes the length of any slice, container, etc into the given encoder
103#[inline]
104pub(crate) fn encode_slice_len<E: Encoder>(encoder: &mut E, len: usize) -> Result<(), EncodeError> {
105    (len as u64).encode(encoder)
106}