network_types/
lib.rs

1#![doc = include_str!("../README.md")]
2#![no_std]
3
4pub mod arp;
5pub mod bitfield;
6pub mod eth;
7pub mod geneve;
8pub mod icmp;
9pub mod ip;
10pub mod llc;
11pub mod mac;
12pub mod mpls;
13pub mod sctp;
14pub mod tcp;
15pub mod udp;
16pub mod vlan;
17pub mod vxlan;
18
19/// Gets the value of the given big-endian field using pointer arithmetic, raw
20/// pointer conversion and, if the target is little-endian, the `swap_bytes`
21/// method. That performs better than `from_be_bytes`.
22///
23/// # Safety
24///
25/// Caller needs to ensure that the provided field name is in bounds of the struct.
26#[cfg(target_endian = "big")]
27#[macro_export]
28macro_rules! getter_be {
29    ($self:expr, $field:ident, $ty:ty) => {
30        ::core::ptr::read_unaligned(
31            (($self as *const Self as usize) + ::memoffset::offset_of!(Self, $field)) as *const $ty,
32        )
33    };
34}
35#[cfg(target_endian = "little")]
36#[macro_export]
37macro_rules! getter_be {
38    ($self:expr, $field:ident, $ty:ty) => {
39        ::core::ptr::read_unaligned(
40            (($self as *const Self as usize) + ::memoffset::offset_of!(Self, $field)) as *const $ty,
41        )
42        .swap_bytes()
43    };
44}
45
46/// Sets the value of the given big-endian field using pointer arithmetic, raw
47/// pointer conversion and, if the target is litte-endian, the `swap_bytes`
48/// method. That performs better than `to_be_bytes`.
49///
50/// # Safety
51///
52/// Caller needs to ensure that the provided field name is in bounds of the struct.
53#[cfg(target_endian = "big")]
54#[macro_export]
55macro_rules! setter_be {
56    ($self:expr, $field:ident, $val:expr) => {
57        // SAFETY: Pointer arithmetics in bounds of the given struct.
58        $self.$field = *((&$val as *const _ as usize) as *const _);
59    };
60}
61#[cfg(target_endian = "little")]
62#[macro_export]
63macro_rules! setter_be {
64    ($self:expr, $field:ident, $val:expr) => {
65        // SAFETY: Pointer arithmetics in bounds of the given struct.
66        $self.$field = *((&$val.swap_bytes() as *const _ as usize) as *const _)
67    };
68}