musli_core::en

Trait EncodeBytes

Source
pub trait EncodeBytes<M> {
    // Required method
    fn encode_bytes<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
       where E: Encoder<Mode = M>;
}
Expand description

Trait governing how a type is encoded as bytes.

This is typically used automatically through the #[musli(bytes)] attribute through the Encode derive.

§Examples

use musli::Encode;

#[derive(Encode)]
struct MyType {
    #[musli(bytes)]
    data: [u8; 128],
}

Implementing manually:

use musli::{Encode, Encoder};
use musli::en::EncodeBytes;

struct MyType {
    data: [u8; 128],
}

impl<M> Encode<M> for MyType {
    fn encode<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
    where
        E: Encoder,
    {
        self.data.encode_bytes(cx, encoder)
    }
}

Required Methods§

Source

fn encode_bytes<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where E: Encoder<Mode = M>,

Encode the given output as bytes.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<M> EncodeBytes<M> for Cow<'_, [u8]>

Source§

fn encode_bytes<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where E: Encoder<Mode = M>,

Source§

impl<M> EncodeBytes<M> for Box<[u8]>

Source§

fn encode_bytes<E>(&self, _: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where E: Encoder<Mode = M>,

Source§

impl<M> EncodeBytes<M> for VecDeque<u8>

Source§

fn encode_bytes<E>(&self, _: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where E: Encoder<Mode = M>,

Source§

impl<M> EncodeBytes<M> for Vec<u8>

Source§

fn encode_bytes<E>(&self, _: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where E: Encoder<Mode = M>,

Source§

impl<M> EncodeBytes<M> for [u8]

Source§

fn encode_bytes<E>(&self, _: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where E: Encoder<Mode = M>,

Source§

impl<T, M> EncodeBytes<M> for &T
where T: ?Sized + EncodeBytes<M>,

Source§

fn encode_bytes<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where E: Encoder<Mode = M>,

Source§

impl<T, M> EncodeBytes<M> for &mut T
where T: ?Sized + EncodeBytes<M>,

Source§

fn encode_bytes<E>(&self, cx: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where E: Encoder<Mode = M>,

Source§

impl<const N: usize, M> EncodeBytes<M> for [u8; N]

Source§

fn encode_bytes<E>(&self, _: &E::Cx, encoder: E) -> Result<E::Ok, E::Error>
where E: Encoder<Mode = M>,

Implementors§