31 lines
834 B
Rust
31 lines
834 B
Rust
|
use core::fmt;
|
||
|
|
||
|
/// A four-octet IPv4 address.
|
||
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
||
|
pub struct Address(pub [u8; 4]);
|
||
|
|
||
|
impl Address {
|
||
|
/// Construct an IPv4 address from a sequence of octets, in big-endian.
|
||
|
///
|
||
|
/// # Panics
|
||
|
/// The function panics if `data` is not four octets long.
|
||
|
pub fn from_bytes(data: &[u8]) -> Address {
|
||
|
let mut bytes = [0; 4];
|
||
|
bytes.copy_from_slice(data);
|
||
|
Address(bytes)
|
||
|
}
|
||
|
|
||
|
/// Return an IPv4 address as a sequence of octets, in big-endian.
|
||
|
pub fn as_bytes(&self) -> &[u8] {
|
||
|
&self.0
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl fmt::Display for Address {
|
||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||
|
let bytes = self.0;
|
||
|
write!(f, "{:02x}.{:02x}.{:02x}.{:02x}",
|
||
|
bytes[0], bytes[1], bytes[2], bytes[3])
|
||
|
}
|
||
|
}
|