Expand description
Byte order-aware numeric primitives.
This module contains equivalents of the native multi-byte integer types with no alignment requirement and supporting byte order conversions.
For each native multi-byte integer type - u16
, i16
, u32
, etc - and
floating point type - f32
and f64
- an equivalent type is defined by
this module - U16
, I16
, U32
, F32
, F64
, etc. Unlike their
native counterparts, these types have alignment 1, and take a type parameter
specifying the byte order in which the bytes are stored in memory. Each type
implements this crate’s relevant conversion and marker traits.
These two properties, taken together, make these types useful for defining data structures whose memory layout matches a wire format such as that of a network protocol or a file format. Such formats often have multi-byte values at offsets that do not respect the alignment requirements of the equivalent native types, and stored in a byte order not necessarily the same as that of the target platform.
Type aliases are provided for common byte orders in the big_endian
,
little_endian
, network_endian
, and native_endian
submodules.
§Example
One use of these types is for representing network packet formats, such as UDP:
use zerocopy::{*, byteorder::network_endian::U16};
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
#[repr(C)]
struct UdpHeader {
src_port: U16,
dst_port: U16,
length: U16,
checksum: U16,
}
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
#[repr(C, packed)]
struct UdpPacket {
header: UdpHeader,
body: [u8],
}
impl UdpPacket {
fn parse(bytes: &[u8]) -> Option<&UdpPacket> {
UdpPacket::ref_from_bytes(bytes).ok()
}
}
Modules§
- Numeric primitives stored in big-endian byte order.
- Numeric primitives stored in little-endian byte order.
- Numeric primitives stored in native-endian byte order.
- Numeric primitives stored in network-endian byte order.
Structs§
- A 32-bit floating point number stored in a given byte order.
- A 64-bit floating point number stored in a given byte order.
- A 16-bit signed integer stored in a given byte order.
- A 32-bit signed integer stored in a given byte order.
- A 64-bit signed integer stored in a given byte order.
- A 128-bit signed integer stored in a given byte order.
- A word-sized signed integer stored in a given byte order.
- A 16-bit unsigned integer stored in a given byte order.
- A 32-bit unsigned integer stored in a given byte order.
- A 64-bit unsigned integer stored in a given byte order.
- A 128-bit unsigned integer stored in a given byte order.
- A word-sized unsigned integer stored in a given byte order.
Enums§
- Big-endian byte order.
- Little-endian byte order.
Traits§
- A type-level representation of byte order.
Type Aliases§
- A type alias for
BigEndian
. - A type alias for
LittleEndian
. - The endianness used by this platform.
- The endianness used in many network protocols.