pub fn read_native<R, W, T>(reader: &mut R) -> Result<T>Available on crate feature
io-std only.Expand description
Read a value in its wire representation and convert it into a native type.
This is the recommended ergonomic pattern for #[derive(Endianize)] types:
- the generated
*Wiretype is the IO/layout type (it implementsEndianRead) - your “real” type is the native type used throughout your program
Conceptually:
- read
Wfrom the stream (endian-correct) - convert into
TusingFrom<W>
Under the hood, this is equivalent to:
ⓘ
let wire: W = read_specific(reader)?;
let native: T = wire.into();§Example
ⓘ
use simple_endian::Endianize;
use simple_endian::read_native;
#[derive(Endianize)]
#[endian(le)]
#[repr(C)]
struct Header {
magic: u32,
version: u16,
}
// Reads `HeaderWire` and converts to `Header`.
let header: Header = read_native::<_, HeaderWire, Header>(&mut reader)?;§Notes
- This composes naturally for nested
Endianizetypes (aPacketWirecontains aHeaderWire). - For enums, the wire representation is
tag + union payload, so you typically want to keep trait derives on the native enum and only convert at the boundary.