Skip to main content

ZeroCopy

Trait ZeroCopy 

Source
pub unsafe trait ZeroCopy: 'static {
    // Provided methods
    fn from_bytes<'de>(bytes: &'de [u8]) -> ReadResult<&'de Self>
       where Self: SchemaRead<'de, Dst = Self> + Sized { ... }
    fn from_bytes_mut<'de>(bytes: &'de mut [u8]) -> ReadResult<&'de mut Self>
       where Self: SchemaRead<'de, Dst = Self> + Sized { ... }
}
Expand description

Marker trait for types that can be deserialized via direct borrows from a Reader.

You should not manually implement this trait for your own type unless you absolutely know what you're doing. The derive macros will automatically implement this trait for your type if it is eligible for zero-copy deserialization.

§Safety

  • The type must not have any invalid bit patterns, no layout requirements, no endianness checks, etc.

Provided Methods§

Source

fn from_bytes<'de>(bytes: &'de [u8]) -> ReadResult<&'de Self>
where Self: SchemaRead<'de, Dst = Self> + Sized,

Get a reference to a type from the given bytes.

§Examples
#[derive(SchemaWrite, SchemaRead)]
#[repr(C)]
struct Data {
    bytes: [u8; 7],
    the_answer: u8,
}

let data = Data { bytes: *b"wincode", the_answer: 42 };

let serialized = wincode::serialize(&data).unwrap();
let data_ref = Data::from_bytes(&serialized).unwrap();

assert_eq!(data_ref, &data);
Source

fn from_bytes_mut<'de>(bytes: &'de mut [u8]) -> ReadResult<&'de mut Self>
where Self: SchemaRead<'de, Dst = Self> + Sized,

Get a mutable reference to a type from the given bytes.

§Examples
#[derive(SchemaWrite, SchemaRead)]
#[repr(C)]
struct Data {
    bytes: [u8; 7],
    the_answer: u8,
}

let data = Data { bytes: [0; 7], the_answer: 0 };

let mut serialized = wincode::serialize(&data).unwrap();
let data_mut = Data::from_bytes_mut(&mut serialized).unwrap();
data_mut.bytes = *b"wincode";
data_mut.the_answer = 42;

let deserialized: Data = wincode::deserialize(&serialized).unwrap();
assert_eq!(deserialized, Data { bytes: *b"wincode", the_answer: 42 });

Implementations on Foreign Types§

Source§

impl ZeroCopy for f32

Source§

impl ZeroCopy for f64

Source§

impl ZeroCopy for i8

Source§

impl ZeroCopy for i16

Source§

impl ZeroCopy for i32

Source§

impl ZeroCopy for i64

Source§

impl ZeroCopy for i128

Source§

impl ZeroCopy for u8

Source§

impl ZeroCopy for u16

Source§

impl ZeroCopy for u32

Source§

impl ZeroCopy for u64

Source§

impl ZeroCopy for u128

Source§

impl<const N: usize, T> ZeroCopy for [T; N]
where T: ZeroCopy,

Implementors§

Source§

impl<T> ZeroCopy for Pod<T>
where T: Copy + 'static,