DecodeBytes

Trait DecodeBytes 

Source
pub trait DecodeBytes<'de, M, A>: Sized
where A: Allocator,
{ const DECODE_BYTES_PACKED: bool = false; // Required method fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error> where D: Decoder<'de, Mode = M, Allocator = A>; }
Expand description

Trait governing how types are decoded as bytes.

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

§Examples

use musli::Decode;

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

Implementing manually:

use musli::{Allocator, Decode, Decoder};
use musli::de::DecodeBytes;

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

impl<'de, M, A> Decode<'de, M, A> for MyType
where
    A: Allocator,
{
    #[inline]
    fn decode<D>(decoder: D) -> Result<Self, D::Error>
    where
        D: Decoder<'de>,
    {
        Ok(Self {
            data: DecodeBytes::decode_bytes(decoder)?,
        })
    }
}

Provided Associated Constants§

Source

const DECODE_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 Methods§

Source

fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
where D: Decoder<'de, Mode = M, Allocator = A>,

Decode the given input 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<'de, M, A> DecodeBytes<'de, M, A> for &'de [u8]
where A: Allocator,

Source§

const DECODE_BYTES_PACKED: bool = false

Source§

fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
where D: Decoder<'de, Allocator = A>,

Source§

impl<'de, M, A> DecodeBytes<'de, M, A> for Cow<'de, [u8]>
where A: Allocator,

Source§

const DECODE_BYTES_PACKED: bool = false

Source§

fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
where D: Decoder<'de, Mode = M, Allocator = A>,

Source§

impl<'de, M, A> DecodeBytes<'de, M, A> for Box<[u8]>
where A: Allocator,

Source§

const DECODE_BYTES_PACKED: bool = false

Source§

fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
where D: Decoder<'de, Mode = M, Allocator = A>,

Source§

impl<'de, M, A> DecodeBytes<'de, M, A> for VecDeque<u8>
where A: Allocator,

Source§

const DECODE_BYTES_PACKED: bool = false

Source§

fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
where D: Decoder<'de, Mode = M, Allocator = A>,

Source§

impl<'de, M, A> DecodeBytes<'de, M, A> for Rc<[u8]>
where A: Allocator,

Source§

const DECODE_BYTES_PACKED: bool = false

Source§

fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
where D: Decoder<'de, Mode = M, Allocator = A>,

Source§

impl<'de, M, A> DecodeBytes<'de, M, A> for Arc<[u8]>
where A: Allocator,

Source§

const DECODE_BYTES_PACKED: bool = false

Source§

fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
where D: Decoder<'de, Mode = M, Allocator = A>,

Source§

impl<'de, M, A> DecodeBytes<'de, M, A> for Vec<u8>
where A: Allocator,

Source§

const DECODE_BYTES_PACKED: bool = false

Source§

fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
where D: Decoder<'de, Mode = M, Allocator = A>,

Source§

impl<'de, M, A, const N: usize> DecodeBytes<'de, M, A> for [u8; N]
where A: Allocator,

Source§

const DECODE_BYTES_PACKED: bool = true

Source§

fn decode_bytes<D>(decoder: D) -> Result<Self, D::Error>
where D: Decoder<'de, Allocator = A>,

Implementors§

Source§

impl<'de, M, A> DecodeBytes<'de, M, A> for musli_core::alloc::Vec<u8, A>
where A: Allocator,

Decode implementation for a Müsli-allocated byte array stored in a Vec.

§Examples

use musli::alloc::Vec;
use musli::{Allocator, Decode};

#[derive(Decode)]
struct Struct<A> where A: Allocator {
    #[musli(bytes)]
    field: Vec<u8, A>
}