Derive Macro TryFromBytes
#[derive(TryFromBytes)]
{
// Attributes available to this derive:
#[zerocopy]
}
Expand description
Implements TryFromBytes
.
This derive synthesizes the runtime checks required to check whether a sequence of initialized bytes corresponds to a valid instance of a type. This derive can be applied to structs, enums, and unions; e.g.:
#[derive(TryFromBytes)]
struct MyStruct {
...
}
#[derive(TryFromBytes)]
#[repr(u8)]
enum MyEnum {
...
}
#[derive(TryFromBytes, Immutable)]
union MyUnion {
...
}
ยงPortability
To ensure consistent endianness for enums with multi-byte representations,
explicitly specify and convert each discriminant using .to_le()
or
.to_be()
; e.g.:
// `DataStoreVersion` is encoded in little-endian.
#[derive(TryFromBytes)]
#[repr(u32)]
pub enum DataStoreVersion {
/// Version 1 of the data store.
V1 = 9u32.to_le(),
/// Version 2 of the data store.
V2 = 10u32.to_le(),
}