mod encoder;
mod impl_tuples;
mod impls;
use self::write::Writer;
use crate::{config::Config, error::EncodeError, utils::Sealed};
pub mod write;
pub use self::encoder::EncoderImpl;
pub trait Encode {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError>;
}
pub trait Encoder: Sealed {
type W: Writer;
type C: Config;
fn writer(&mut self) -> &mut Self::W;
fn config(&self) -> &Self::C;
}
impl<T> Encoder for &mut T
where
T: Encoder,
{
type W = T::W;
type C = T::C;
fn writer(&mut self) -> &mut Self::W {
T::writer(self)
}
fn config(&self) -> &Self::C {
T::config(self)
}
}
#[inline]
pub(crate) fn encode_option_variant<E: Encoder, T>(
encoder: &mut E,
value: &Option<T>,
) -> Result<(), EncodeError> {
match value {
None => 0u8.encode(encoder),
Some(_) => 1u8.encode(encoder),
}
}
#[inline]
pub(crate) fn encode_slice_len<E: Encoder>(encoder: &mut E, len: usize) -> Result<(), EncodeError> {
(len as u64).encode(encoder)
}