Skip to main content

net_wire/ethernet/
address.rs

1use core::fmt;
2
3/// A six-octet Ethernet hardware address.
4///
5/// Ethernet destination and source address fields precede the protocol type in the
6/// Ethernet encapsulation described by [RFC 894](https://www.rfc-editor.org/rfc/rfc894).
7#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
8pub struct MacAddress([u8; 6]);
9
10impl MacAddress {
11    /// Constructs an address from its six wire-order octets.
12    ///
13    /// Ethernet addresses occupy the destination and source fields described by
14    /// [RFC 894](https://www.rfc-editor.org/rfc/rfc894).
15    #[inline]
16    pub const fn new(octets: [u8; 6]) -> Self {
17        Self(octets)
18    }
19
20    /// Returns the six wire-order octets of this address.
21    ///
22    /// The returned order is the Ethernet address-field order described by
23    /// [RFC 894](https://www.rfc-editor.org/rfc/rfc894).
24    #[inline]
25    pub const fn octets(self) -> [u8; 6] {
26        self.0
27    }
28}
29
30impl fmt::Display for MacAddress {
31    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
32        let [a, b, c, d, e, f] = self.0;
33        write!(formatter, "{a:02x}:{b:02x}:{c:02x}:{d:02x}:{e:02x}:{f:02x}")
34    }
35}