net_types::ethernet

Struct Mac

Source
pub struct Mac(/* private fields */);
Expand description

A media access control (MAC) address.

MAC addresses are used to identify devices in the Ethernet protocol.

MAC addresses can be derived from multicast IP addresses; see the From implementation for more details.

§Layout

Mac has the same layout as [u8; 6], which is the layout that most protocols use to represent a MAC address in their packet formats. This can be useful when parsing a MAC address from a packet. For example:

/// The header of an Ethernet frame.
///
/// `EthernetHeader` has the same layout as the header of an Ethernet frame.
#[repr(C)]
struct EthernetHeader {
    dst: Mac,
    src: Mac,
    ethertype: [u8; 2],
}

Implementations§

Source§

impl Mac

Source

pub const BYTES: usize = 6usize

The number of bytes in a Mac address.

Source

pub const BROADCAST: Mac = _

The broadcast MAC address.

The broadcast MAC address, FF:FF:FF:FF:FF:FF, indicates that a frame should be received by all receivers regardless of their local MAC address.

Source

pub const DEFAULT_EUI_MAGIC: [u8; 2] = _

The default RFC 4291 EUI-64 magic value used by the to_eui64 method.

Source

pub const UNSPECIFIED: Mac = _

The all-zeroes MAC address.

Source

pub const fn new(bytes: [u8; 6]) -> Mac

Constructs a new MAC address.

Source

pub const fn bytes(self) -> [u8; 6]

Gets the bytes of the MAC address.

Source

pub fn to_eui64(self) -> [u8; 8]

Returns the RFC 4291 EUI-64 interface identifier for this MAC address with the default EUI magic value.

mac.to_eui64() is equivalent to mac.to_eui64_with_magic(Mac::DEFAULT_EUI_MAGIC).

Source

pub fn to_eui64_with_magic(self, eui_magic: [u8; 2]) -> [u8; 8]

Returns the RFC 4291 EUI-64 interface identifier for this MAC address with a custom EUI magic value.

eui_magic is the two bytes that are inserted between the bytes of the MAC address to form the identifier. Also see the to_eui64 method, which uses the default magic value of 0xFFFE.

Returns the link-local unicast IPv6 address and subnet for this MAC address, as per [RFC 4862], with the default EUI magic value.

mac.to_ipv6_link_local() is equivalent to mac.to_ipv6_link_local_with_magic(Mac::DEFAULT_EUI_MAGIC).

Returns the link-local unicast IPv6 address and subnet for this MAC address, as per RFC 4862.

eui_magic is the two bytes that are inserted between the bytes of the MAC address to form the identifier. Also see the to_ipv6_link_local method, which uses the default magic value of 0xFFFE.

The subnet prefix length is 128 - Ipv6::UNICAST_INTERFACE_IDENTIFIER_BITS.

Trait Implementations§

Source§

impl AsMut<[u8]> for Mac

Source§

fn as_mut(&mut self) -> &mut [u8]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl AsRef<[u8]> for Mac

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl BroadcastAddress for Mac

Source§

fn is_broadcast(&self) -> bool

Is this the broadcast MAC address?

Returns true if this is the broadcast MAC address, FF:FF:FF:FF:FF:FF. Note that the broadcast address is also considered a multicast address, so addr.is_broadcast() implies addr.is_multicast().

Source§

impl Clone for Mac

Source§

fn clone(&self) -> Mac

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Mac

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Mac

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, A: IpAddress> From<&'a MulticastAddr<A>> for Mac

Source§

fn from(addr: &'a MulticastAddr<A>) -> Mac

Converts a multicast IP address to a MAC address.

This method is equivalent to MulticastAddr::<Mac>::from(addr).get().

Source§

impl From<BroadcastAddr<Mac>> for Mac

Source§

fn from(addr: BroadcastAddr<Mac>) -> Mac

Converts to this type from the input type.
Source§

impl<A: IpAddress> From<MulticastAddr<A>> for Mac

Source§

fn from(addr: MulticastAddr<A>) -> Mac

Converts a multicast IP address to a MAC address.

This method is equivalent to (&addr).into().

Source§

impl From<MulticastAddr<Mac>> for Mac

Source§

fn from(addr: MulticastAddr<Mac>) -> Mac

Converts to this type from the input type.
Source§

impl From<UnicastAddr<Mac>> for Mac

Source§

fn from(addr: UnicastAddr<Mac>) -> Mac

Converts to this type from the input type.
Source§

impl FromBytes for Mac
where [u8; 6]: FromBytes,

§

fn ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: KnownLayout + Immutable,

Interprets the given source as a &Self. Read more
§

fn ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: KnownLayout + Immutable,

Interprets the prefix of the given source as a &Self without copying. Read more
§

fn ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, Infallible>>
where Self: Immutable + KnownLayout,

Interprets the suffix of the given bytes as a &Self. Read more
§

fn mut_from_bytes( source: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout,

Interprets the given source as a &mut Self. Read more
§

fn mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout,

Interprets the prefix of the given source as a &mut Self without copying. Read more
§

fn mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, Infallible>>
where Self: IntoBytes + KnownLayout,

Interprets the suffix of the given source as a &mut Self without copying. Read more
§

fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the given source. Read more
§

fn read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the prefix of the given source. Read more
§

fn read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), SizeError<&[u8], Self>>
where Self: Sized,

Reads a copy of Self from the suffix of the given source. Read more
Source§

impl FromZeros for Mac
where [u8; 6]: FromZeros,

§

fn zero(&mut self)

Overwrites self with zeros. Read more
§

fn new_zeroed() -> Self
where Self: Sized,

Creates an instance of Self from zeroed bytes. Read more
Source§

impl Hash for Mac

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IntoBytes for Mac
where [u8; 6]: IntoBytes,

§

fn as_bytes(&self) -> &[u8]
where Self: Immutable,

Gets the bytes of this value. Read more
§

fn as_mut_bytes(&mut self) -> &mut [u8]
where Self: FromBytes,

Gets the bytes of this value mutably. Read more
§

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst. Read more
§

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst. Read more
§

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst. Read more
Source§

impl KnownLayout for Mac
where Self: Sized,

Source§

type PointerMetadata = ()

The type of metadata stored in a pointer to Self. Read more
Source§

impl MulticastAddress for Mac

Source§

fn is_multicast(&self) -> bool

Is this a multicast MAC address?

Returns true if the least significant bit of the first byte of the address is 1.

Source§

fn is_non_multicast(&self) -> bool

Is this a non-multicast address? The inverse of is_multicast().
Source§

impl PartialEq for Mac

Source§

fn eq(&self, other: &Mac) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFromBytes for Mac
where [u8; 6]: TryFromBytes,

§

fn try_ref_from_bytes( source: &[u8], ) -> Result<&Self, ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the given source as a &Self. Read more
§

fn try_ref_from_prefix( source: &[u8], ) -> Result<(&Self, &[u8]), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the prefix of the given source as a &Self. Read more
§

fn try_ref_from_suffix( source: &[u8], ) -> Result<(&[u8], &Self), ConvertError<AlignmentError<&[u8], Self>, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: KnownLayout + Immutable,

Attempts to interpret the suffix of the given source as a &Self. Read more
§

fn try_mut_from_bytes( bytes: &mut [u8], ) -> Result<&mut Self, ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout,

Attempts to interpret the given source as a &mut Self without copying. Read more
§

fn try_mut_from_prefix( source: &mut [u8], ) -> Result<(&mut Self, &mut [u8]), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout,

Attempts to interpret the prefix of the given source as a &mut Self. Read more
§

fn try_mut_from_suffix( source: &mut [u8], ) -> Result<(&mut [u8], &mut Self), ConvertError<AlignmentError<&mut [u8], Self>, SizeError<&mut [u8], Self>, ValidityError<&mut [u8], Self>>>
where Self: KnownLayout,

Attempts to interpret the suffix of the given source as a &mut Self. Read more
§

fn try_read_from_bytes( source: &[u8], ) -> Result<Self, ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read the given source as a Self. Read more
§

fn try_read_from_prefix( source: &[u8], ) -> Result<(Self, &[u8]), ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the prefix of the given source. Read more
§

fn try_read_from_suffix( source: &[u8], ) -> Result<(&[u8], Self), ConvertError<Infallible, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>
where Self: Sized,

Attempts to read a Self from the suffix of the given source. Read more
Source§

impl UnicastAddress for Mac

Source§

fn is_unicast(&self) -> bool

Is this a unicast MAC address?

Returns true if the least significant bit of the first byte of the address is 0.

Source§

impl Copy for Mac

Source§

impl Eq for Mac

Source§

impl Immutable for Mac
where [u8; 6]: Immutable,

Source§

impl StructuralPartialEq for Mac

Source§

impl Unaligned for Mac
where [u8; 6]: Unaligned,

Auto Trait Implementations§

§

impl Freeze for Mac

§

impl RefUnwindSafe for Mac

§

impl Send for Mac

§

impl Sync for Mac

§

impl Unpin for Mac

§

impl UnwindSafe for Mac

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.