Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions uefi-raw/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- Added `HiiConfigAccessProtocol`.
- Added `::octets()` for `Ipv4Address`, `Ipv6Address`, and
`MacAddress` to streamline the API with `core::net`.
- Added `::ZERO` constant for `IpAddress`
- `Ipv4Address` and `Ipv6Address` now implement `Display`. They
use the same formatting as `core::net::{Ipv4Addr, Ipv6Addr}`

## Changed
- **Breaking:** The MSRV is now 1.85.1 and the crate uses the Rust 2024 edition.
Expand Down
47 changes: 34 additions & 13 deletions uefi-raw/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
//! - [`Ipv4Address`]
//! - [`Ipv6Address`]

use core::fmt;
use core::fmt::{Debug, Formatter};
use core::fmt::{self, Debug, Display, Formatter};

/// An IPv4 internet protocol address.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
Expand All @@ -36,6 +35,13 @@ impl From<Ipv4Address> for core::net::Ipv4Addr {
}
}

impl Display for Ipv4Address {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let ip = core::net::Ipv4Addr::from(*self);
write!(f, "{}", ip)
}
}

/// An IPv6 internet protocol address.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[repr(transparent)]
Expand All @@ -61,6 +67,13 @@ impl From<Ipv6Address> for core::net::Ipv6Addr {
}
}

impl Display for Ipv6Address {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let ip = core::net::Ipv6Addr::from(*self);
write!(f, "{}", ip)
}
}

/// An IPv4 or IPv6 internet protocol address that is ABI compatible with EFI.
///
/// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This
Expand All @@ -83,19 +96,31 @@ pub union IpAddress {
}

impl IpAddress {
/// Zeroed variant where all bytes are guaranteed to be initialized to zero.
pub const ZERO: Self = Self { addr: [0; 4] };

/// Construct a new IPv4 address.
///
/// The type won't know that it is an IPv6 address and additional context
/// is needed.
///
/// # Safety
/// The constructor only initializes the bytes needed for IPv4 addresses.
#[must_use]
pub const fn new_v4(ip_addr: [u8; 4]) -> Self {
pub const fn new_v4(octets: [u8; 4]) -> Self {
Self {
v4: Ipv4Address(ip_addr),
v4: Ipv4Address(octets),
}
}

/// Construct a new IPv6 address.
///
/// The type won't know that it is an IPv6 address and additional context
/// is needed.
#[must_use]
pub const fn new_v6(ip_addr: [u8; 16]) -> Self {
pub const fn new_v6(octets: [u8; 16]) -> Self {
Self {
v6: Ipv6Address(ip_addr),
v6: Ipv6Address(octets),
}
}
}
Expand All @@ -111,19 +136,15 @@ impl Debug for IpAddress {

impl Default for IpAddress {
fn default() -> Self {
Self { addr: [0u32; 4] }
Self::ZERO
}
}

impl From<core::net::IpAddr> for IpAddress {
fn from(t: core::net::IpAddr) -> Self {
match t {
core::net::IpAddr::V4(ip) => Self {
v4: Ipv4Address::from(ip),
},
core::net::IpAddr::V6(ip) => Self {
v6: Ipv6Address::from(ip),
},
core::net::IpAddr::V4(ip) => Self::new_v4(ip.octets()),
core::net::IpAddr::V6(ip) => Self::new_v6(ip.octets()),
}
}
}
Expand Down
Loading