NetworkRepr

Trait NetworkRepr 

Source
pub trait NetworkRepr<U: Copy> {
    // Required methods
    fn to_network(self) -> U;
    fn from_network(val: U) -> Self;
}
Expand description

Types which can be converted to and from bitstrings and byte arrays for serialisation as fields of network packets.

This can be used for better type-checking (e.g., bitfields or newtypes). We might represent a next-header type using a primitive:

#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, Ord, PartialOrd, Default)]
struct Ethertype(u16);

impl NetworkRepr<u16be> for Ethertype {
    #[inline]
    fn to_network(self) -> u16be {
        self.0
    }

    #[inline]
    fn from_network(val: u16be) -> Self {
        Self(val)
    }
}

…or, a byte array (such as [u8; 16]).

Required Methods§

Source

fn to_network(self) -> U

Converts a local value into raw bytes or integer type.

Source

fn from_network(val: U) -> Self

Converts a raw value into a local type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl NetworkRepr<u8> for bool

Source§

fn to_network(self) -> u1

Source§

fn from_network(val: u1) -> Self

Source§

impl NetworkRepr<[u8; 6]> for MacAddr6

Source§

fn to_network(self) -> [u8; 6]

Source§

fn from_network(val: [u8; 6]) -> Self

Implementors§