From ecf8f892bffb6650ab2b78c011cf3ae6063bd71b Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Sat, 23 Aug 2025 14:40:39 +0200 Subject: [PATCH 1/2] uefi-raw: net: small code improvements for IpAddress --- uefi-raw/CHANGELOG.md | 1 + uefi-raw/src/net.rs | 33 ++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index 8468d9e0d..99f1535f8 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -7,6 +7,7 @@ - Added `HiiConfigAccessProtocol`. - Added `::octets()` for `Ipv4Address`, `Ipv6Address`, and `MacAddress` to streamline the API with `core::net`. +- Added `::ZERO` constant for `IpAddress` ## Changed - **Breaking:** The MSRV is now 1.85.1 and the crate uses the Rust 2024 edition. diff --git a/uefi-raw/src/net.rs b/uefi-raw/src/net.rs index 773dd2e8f..c13f4e9b8 100644 --- a/uefi-raw/src/net.rs +++ b/uefi-raw/src/net.rs @@ -8,8 +8,7 @@ //! - [`Ipv4Address`] //! - [`Ipv6Address`] -use core::fmt; -use core::fmt::{Debug, Formatter}; +use core::fmt::{self, Debug, Formatter}; /// An IPv4 internet protocol address. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] @@ -83,19 +82,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), } } } @@ -111,19 +122,15 @@ impl Debug for IpAddress { impl Default for IpAddress { fn default() -> Self { - Self { addr: [0u32; 4] } + Self::ZERO } } impl From 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()), } } } From 0a68863ac22c2983300e5446818bbafcaf36104a Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Tue, 16 Sep 2025 08:38:09 +0200 Subject: [PATCH 2/2] uefi-raw: net: implement Display for Ipv4Address and Ipv6Address --- uefi-raw/CHANGELOG.md | 2 ++ uefi-raw/src/net.rs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/uefi-raw/CHANGELOG.md b/uefi-raw/CHANGELOG.md index 99f1535f8..811d72b30 100644 --- a/uefi-raw/CHANGELOG.md +++ b/uefi-raw/CHANGELOG.md @@ -8,6 +8,8 @@ - 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. diff --git a/uefi-raw/src/net.rs b/uefi-raw/src/net.rs index c13f4e9b8..e4d2cfd69 100644 --- a/uefi-raw/src/net.rs +++ b/uefi-raw/src/net.rs @@ -8,7 +8,7 @@ //! - [`Ipv4Address`] //! - [`Ipv6Address`] -use core::fmt::{self, Debug, Formatter}; +use core::fmt::{self, Debug, Display, Formatter}; /// An IPv4 internet protocol address. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] @@ -35,6 +35,13 @@ impl From 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)] @@ -60,6 +67,13 @@ impl From 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