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§
Sourcefn from_bytes<'de>(bytes: &'de [u8]) -> ReadResult<&'de Self>where
Self: SchemaRead<'de, Dst = Self> + Sized,
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);Sourcefn from_bytes_mut<'de>(bytes: &'de mut [u8]) -> ReadResult<&'de mut 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,
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 });