[][src]Trait addr_hal::Ipv4Address

pub trait Ipv4Address: Clone + Copy + PartialEq + Ord {
    const LOCALHOST: Self;
    const UNSPECIFIED: Self;
    const BROADCAST: Self;

    fn new(a: u8, b: u8, c: u8, d: u8) -> Self;
fn octets(&self) -> [u8; 4]; }

Describe the internal data structure behavior of Ipv4Addr.

You can implement this trait by yourself or use ffi for specific Platform.

Examples


use addr_hal::Ipv4Address;
use addr_hal::Ipv4Addr;

#[derive(Clone, Copy, PartialOrd, PartialEq, Eq, Ord)]
struct Ipv4AddrInner {
    inner: [u8; 4],
}

impl Ipv4Address for Ipv4AddrInner {
    const LOCALHOST: Self = Self {
        inner: [127, 0, 0, 1],
    };
    const UNSPECIFIED: Self = Self {
        inner: [0, 0, 0, 0],
    };

    const BROADCAST: Self = Self {
        inner: [255, 255, 255, 255],
    };

    fn new(a: u8, b: u8, c: u8, d: u8) -> Self {
        Self {
            inner: [a, b, c, d],
        }
    }

    fn octets(&self) -> [u8; 4] {
        self.inner.clone()
    }
}

let ip: Ipv4Addr<Ipv4AddrInner> = "127.0.0.1".parse().unwrap();
assert_eq!("127.0.0.1".parse(), Ok(ip));

Associated Constants

const LOCALHOST: Self

An IPv4 address with the address pointing to localhost, usually is 127.0.0.1.

const UNSPECIFIED: Self

An IPv4 address with the address pointing to unspecified, usually is 0.0.0.0.

const BROADCAST: Self

An IPv4 address with the address pointing to broadcast, usually is 255.255.255.255.

Loading content...

Required methods

fn new(a: u8, b: u8, c: u8, d: u8) -> Self

Creates a new IPv4 address from four eight-bit octets.

The result will represent the IP address a.b.c.d.

fn octets(&self) -> [u8; 4]

Returns the four eight-bit integers that make up this address.

Loading content...

Implementors

Loading content...