pub struct MacAddress(/* private fields */);net only.Expand description
A MAC address.
This type provides type-safe MAC addresses with IEEE 802 validation.
It uses the newtype pattern with #[repr(transparent)] for zero-cost abstraction.
§Invariants
- Exactly 6 bytes (48 bits)
- Each octet is 0-255
§Examples
use bare_types::net::MacAddress;
// Create a MAC address
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55])?;
// Access the string representation
assert_eq!(mac.as_str(), "00:11:22:33:44:55");
// Check if it's unicast
assert!(mac.is_unicast());
// Get the octets
assert_eq!(mac.octets(), [0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);
// Parse from string
let mac: MacAddress = "00:11:22:33:44:55".parse()?;Implementations§
Source§impl MacAddress
impl MacAddress
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, MacAddressError>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, MacAddressError>
Creates a new MAC address from a slice of bytes.
§Errors
Returns MacAddressError if the slice is not exactly 6 bytes.
§Examples
use bare_types::net::MacAddress;
let bytes = vec![0x00, 0x11, 0x22, 0x33, 0x44, 0x55];
let mac = MacAddress::from_bytes(&bytes)?;
assert_eq!(mac.as_str(), "00:11:22:33:44:55");Sourcepub fn from_str(s: &str) -> Result<Self, MacAddressError>
pub fn from_str(s: &str) -> Result<Self, MacAddressError>
Sourcepub fn as_str(&self) -> String
pub fn as_str(&self) -> String
Returns the MAC address as a string slice in colon-separated format.
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
assert_eq!(mac.as_str(), "00:11:22:33:44:55");Sourcepub fn as_str_dash(&self) -> String
pub fn as_str_dash(&self) -> String
Returns the MAC address as a string slice in dash-separated format.
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
assert_eq!(mac.as_str_dash(), "00-11-22-33-44-55");Sourcepub fn as_str_lower(&self) -> String
pub fn as_str_lower(&self) -> String
Returns the MAC address as a string slice in lowercase colon-separated format.
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
assert_eq!(mac.as_str_lower(), "00:11:22:33:44:55");Sourcepub const fn as_inner(&self) -> &[u8; 6]
pub const fn as_inner(&self) -> &[u8; 6]
Returns a reference to the underlying array of 6 bytes.
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
let octets: &[u8; 6] = mac.as_inner();
assert_eq!(octets, &[0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);Sourcepub const fn into_inner(self) -> [u8; 6]
pub const fn into_inner(self) -> [u8; 6]
Consumes this MAC address and returns the underlying array of 6 bytes.
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
let octets = mac.into_inner();
assert_eq!(octets, [0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);Sourcepub const fn octets(&self) -> [u8; 6]
pub const fn octets(&self) -> [u8; 6]
Returns the octets of the MAC address.
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
assert_eq!(mac.octets(), [0x00, 0x11, 0x22, 0x33, 0x44, 0x55]);Sourcepub const fn is_unicast(&self) -> bool
pub const fn is_unicast(&self) -> bool
Returns true if this is a unicast MAC address.
The least significant bit of the first octet indicates unicast (0) or multicast (1).
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
assert!(mac.is_unicast());Sourcepub const fn is_multicast(&self) -> bool
pub const fn is_multicast(&self) -> bool
Returns true if this is a multicast MAC address.
The least significant bit of the first octet indicates unicast (0) or multicast (1).
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x01, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
assert!(mac.is_multicast());Sourcepub const fn is_globally_administered(&self) -> bool
pub const fn is_globally_administered(&self) -> bool
Returns true if this is a globally administered MAC address.
The second least significant bit of the first octet indicates globally (0) or locally (1) administered.
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
assert!(mac.is_globally_administered());Sourcepub const fn is_locally_administered(&self) -> bool
pub const fn is_locally_administered(&self) -> bool
Returns true if this is a locally administered MAC address.
The second least significant bit of the first octet indicates globally (0) or locally (1) administered.
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x02, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
assert!(mac.is_locally_administered());Sourcepub const fn oui(&self) -> [u8; 3]
pub const fn oui(&self) -> [u8; 3]
Returns the OUI (Organizationally Unique Identifier) of the MAC address.
The OUI is the first 3 octets of the MAC address.
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
assert_eq!(mac.oui(), [0x00, 0x11, 0x22]);Sourcepub const fn nic(&self) -> [u8; 3]
pub const fn nic(&self) -> [u8; 3]
Returns the NIC (Network Interface Controller) specific part of the MAC address.
The NIC specific part is the last 3 octets of the MAC address.
§Examples
use bare_types::net::MacAddress;
let mac = MacAddress::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55]).unwrap();
assert_eq!(mac.nic(), [0x33, 0x44, 0x55]);Trait Implementations§
Source§impl<'a> Arbitrary<'a> for MacAddress
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for MacAddress
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl Clone for MacAddress
impl Clone for MacAddress
Source§fn clone(&self) -> MacAddress
fn clone(&self) -> MacAddress
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more