pub trait DecodeBytes<'de, M, A>: Sizedwhere
A: Allocator,{
const DECODE_BYTES_PACKED: bool = false;
// Required method
fn decode_bytes<D>(decoder: D) -> Result<Self, <D as Decoder<'de>>::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§
Sourceconst DECODE_BYTES_PACKED: bool = false
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§
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,
impl<'de, M, A> DecodeBytes<'de, M, A> for &'de [u8]where
A: Allocator,
Implementors§
Source§impl<'de, M, A> DecodeBytes<'de, M, A> for musli::alloc::Vec<u8, A>where
A: Allocator,
Decode implementation for a Müsli-allocated byte array stored in a Vec.
impl<'de, M, A> DecodeBytes<'de, M, A> for musli::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>
}