mod encoder;
mod impl_tuples;
mod impls;
pub mod write;
use crate::{config::Config, error::Error, utils::Sealed};
pub use self::encoder::EncoderImpl;
pub use self::write::{SizeWriter, SliceWriter, Writer};
#[cfg(feature = "alloc")]
pub use self::write::VecWriter;
#[cfg(feature = "std")]
pub use self::write::{IoWriter, StdWriter};
pub trait Encode {
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), Error>;
}
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]
#[allow(dead_code)]
pub(crate) fn encode_option_variant<E: Encoder, T>(
encoder: &mut E,
value: &Option<T>,
) -> Result<(), Error> {
match value {
None => 0u8.encode(encoder),
Some(_) => 1u8.encode(encoder),
}
}
#[inline]
#[allow(dead_code)]
pub(crate) fn encode_slice_len<E: Encoder>(encoder: &mut E, len: usize) -> Result<(), Error> {
(len as u64).encode(encoder)
}