network-types 0.2.0

Rust structs representing network-related types in Linux.
Documentation
#![doc = include_str!("../README.md")]
#![no_std]
#![expect(unsafe_op_in_unsafe_fn)]

pub mod arp;
pub mod bitfield;
pub mod ecn_codepoint;
pub mod eth;
pub mod geneve;
pub mod icmp;
pub mod igmp;
pub mod ip;
pub mod llc;
pub mod mac;
pub mod mpls;
pub mod sctp;
pub mod tcp;
pub mod udp;
pub mod vlan;
pub mod vxlan;

/// Gets the value of the given big-endian field using pointer arithmetic, raw
/// pointer conversion and, if the target is little-endian, the `swap_bytes`
/// method. That performs better than `from_be_bytes`.
///
/// # Safety
///
/// Caller needs to ensure that the provided field name is in bounds of the struct.
#[cfg(target_endian = "big")]
#[macro_export]
macro_rules! getter_be {
    ($self:expr, $field:ident, $ty:ty) => {
        core::ptr::read_unaligned(core::ptr::addr_of!($self.$field).cast::<$ty>())
    };
}
#[cfg(target_endian = "little")]
#[macro_export]
macro_rules! getter_be {
    ($self:expr, $field:ident, $ty:ty) => {
        core::ptr::read_unaligned(core::ptr::addr_of!($self.$field).cast::<$ty>()).swap_bytes()
    };
}

/// Sets the value of the given big-endian field using pointer arithmetic, raw
/// pointer conversion and, if the target is little-endian, the `swap_bytes`
/// method. That performs better than `to_be_bytes`.
///
/// # Safety
///
/// Caller needs to ensure that the provided field name is in bounds of the struct.
#[cfg(target_endian = "big")]
#[macro_export]
macro_rules! setter_be {
    ($self:expr, $field:ident, $val:expr) => {
        // SAFETY: Pointer arithmetics in bounds of the given struct.
        $self.$field = *core::ptr::from_ref(&$val).cast()
    };
}
#[cfg(target_endian = "little")]
#[macro_export]
macro_rules! setter_be {
    ($self:expr, $field:ident, $val:expr) => {
        // SAFETY: Pointer arithmetics in bounds of the given struct.
        $self.$field = *core::ptr::from_ref(&$val.swap_bytes()).cast()
    };
}