EncodeBytes

Trait EncodeBytes 

Source
pub trait EncodeBytes<M> {
    type EncodeBytes: ?Sized + EncodeBytes<M>;

    const ENCODE_BYTES_PACKED: bool = false;

    // Required methods
    fn encode_bytes<E>(&self, encoder: E) -> Result<(), E::Error>
       where E: Encoder<Mode = M>;
    fn as_encode_bytes(&self) -> &Self::EncodeBytes;

    // Provided method
    fn size_hint(&self) -> Option<usize> { ... }
}
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 {
    type Encode = Self;

    #[inline]
    fn encode<E>(&self, encoder: E) -> Result<(), E::Error>
    where
        E: Encoder,
    {
        self.data.encode_bytes(encoder)
    }

    #[inline]
    fn as_encode(&self) -> &Self::Encode {
        self
    }
}

Provided Associated Constants§

Source

const ENCODE_BYTES_PACKED: bool = false

Whether the type is packed. Packed types can be bitwise copied if the representation of the serialization format is identical to the memory layout of the type.

Note that setting this to true has safety implications, since it implies that assuming the type is correctly aligned it can be validly bitwise copied when encoded. Setting it to false is always safe.

Required Associated Types§

Source

type EncodeBytes: ?Sized + EncodeBytes<M>

The underlying type being encoded.

This is used to “peek through” types like references being encoded.

Required Methods§

Source

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

Encode the given output as bytes.

Source

fn as_encode_bytes(&self) -> &Self::EncodeBytes

Coerce into the underlying value being encoded.

Provided Methods§

Source

fn size_hint(&self) -> Option<usize>

The number of fields in the type.

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§

const ENCODE_BYTES_PACKED: bool = false

Source§

type EncodeBytes = [u8]

Source§

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

Source§

fn as_encode_bytes(&self) -> &Self::EncodeBytes

Source§

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

Source§

const ENCODE_BYTES_PACKED: bool = false

Source§

type EncodeBytes = [u8]

Source§

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

Source§

fn as_encode_bytes(&self) -> &Self::EncodeBytes

Source§

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

Source§

const ENCODE_BYTES_PACKED: bool = false

Source§

type EncodeBytes = VecDeque<u8>

Source§

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

Source§

fn as_encode_bytes(&self) -> &Self::EncodeBytes

Source§

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

Source§

const ENCODE_BYTES_PACKED: bool = false

Source§

type EncodeBytes = [u8]

Source§

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

Source§

fn as_encode_bytes(&self) -> &Self::EncodeBytes

Source§

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

Source§

const ENCODE_BYTES_PACKED: bool = false

Source§

type EncodeBytes = [u8]

Source§

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

Source§

fn as_encode_bytes(&self) -> &Self::EncodeBytes

Source§

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

Source§

const ENCODE_BYTES_PACKED: bool = false

Source§

type EncodeBytes = T

Source§

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

Source§

fn as_encode_bytes(&self) -> &Self::EncodeBytes

Source§

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

Source§

const ENCODE_BYTES_PACKED: bool = false

Source§

type EncodeBytes = T

Source§

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

Source§

fn as_encode_bytes(&self) -> &Self::EncodeBytes

Source§

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

Source§

const ENCODE_BYTES_PACKED: bool = true

Source§

type EncodeBytes = [u8; N]

Source§

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

Source§

fn as_encode_bytes(&self) -> &Self::EncodeBytes

Implementors§