Skip to content

Commit f7b0f30

Browse files
authored
Merge pull request #1776 from rust-osdev/core-net-v2-2
uefi-raw: net: code improvements
2 parents c49add9 + fa873da commit f7b0f30

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

uefi-raw/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
`MacAddress` to streamline the API with `core::net`.
1010
- Added `::into_core_addr()` for `IpAddress`
1111
- Added `::into_ethernet_addr()` for `MacAddress`
12+
- Added `::ZERO` constant for `IpAddress`
13+
- `Ipv4Address` and `Ipv6Address` now implement `Display`. They
14+
use the same formatting as `core::net::{Ipv4Addr, Ipv6Addr}`
1215

1316
## Changed
1417
- **Breaking:** The MSRV is now 1.85.1 and the crate uses the Rust 2024 edition.

uefi-raw/src/net.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
//! - [`Ipv4Address`]
99
//! - [`Ipv6Address`]
1010
11-
use core::fmt;
12-
use core::fmt::{Debug, Formatter};
11+
use core::fmt::{self, Debug, Display, Formatter};
1312

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

38+
impl Display for Ipv4Address {
39+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
40+
let ip = core::net::Ipv4Addr::from(*self);
41+
write!(f, "{}", ip)
42+
}
43+
}
44+
3945
/// An IPv6 internet protocol address.
4046
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
4147
#[repr(transparent)]
@@ -61,6 +67,13 @@ impl From<Ipv6Address> for core::net::Ipv6Addr {
6167
}
6268
}
6369

70+
impl Display for Ipv6Address {
71+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
72+
let ip = core::net::Ipv6Addr::from(*self);
73+
write!(f, "{}", ip)
74+
}
75+
}
76+
6477
/// An IPv4 or IPv6 internet protocol address that is ABI compatible with EFI.
6578
///
6679
/// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This
@@ -83,19 +96,31 @@ pub union IpAddress {
8396
}
8497

8598
impl IpAddress {
99+
/// Zeroed variant where all bytes are guaranteed to be initialized to zero.
100+
pub const ZERO: Self = Self { addr: [0; 4] };
101+
86102
/// Construct a new IPv4 address.
103+
///
104+
/// The type won't know that it is an IPv6 address and additional context
105+
/// is needed.
106+
///
107+
/// # Safety
108+
/// The constructor only initializes the bytes needed for IPv4 addresses.
87109
#[must_use]
88-
pub const fn new_v4(ip_addr: [u8; 4]) -> Self {
110+
pub const fn new_v4(octets: [u8; 4]) -> Self {
89111
Self {
90-
v4: Ipv4Address(ip_addr),
112+
v4: Ipv4Address(octets),
91113
}
92114
}
93115

94116
/// Construct a new IPv6 address.
117+
///
118+
/// The type won't know that it is an IPv6 address and additional context
119+
/// is needed.
95120
#[must_use]
96-
pub const fn new_v6(ip_addr: [u8; 16]) -> Self {
121+
pub const fn new_v6(octets: [u8; 16]) -> Self {
97122
Self {
98-
v6: Ipv6Address(ip_addr),
123+
v6: Ipv6Address(octets),
99124
}
100125
}
101126

@@ -132,19 +157,15 @@ impl Debug for IpAddress {
132157

133158
impl Default for IpAddress {
134159
fn default() -> Self {
135-
Self { addr: [0u32; 4] }
160+
Self::ZERO
136161
}
137162
}
138163

139164
impl From<core::net::IpAddr> for IpAddress {
140165
fn from(t: core::net::IpAddr) -> Self {
141166
match t {
142-
core::net::IpAddr::V4(ip) => Self {
143-
v4: Ipv4Address::from(ip),
144-
},
145-
core::net::IpAddr::V6(ip) => Self {
146-
v6: Ipv6Address::from(ip),
147-
},
167+
core::net::IpAddr::V4(ip) => Self::new_v4(ip.octets()),
168+
core::net::IpAddr::V6(ip) => Self::new_v6(ip.octets()),
148169
}
149170
}
150171
}

0 commit comments

Comments
 (0)