Skip to content

Commit ff424b5

Browse files
committed
uefi-raw: small code improvements for IpAddress
1 parent 82bf2b0 commit ff424b5

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

uefi-raw/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Added `HiiConfigAccessProtocol`.
88
- Added `::octets()` for `Ipv4Address`, `Ipv6Address`, and
99
`MacAddress` to streamline the API with `core::net`.
10+
- Added `::ZERO` constant for `IpAddress`
1011

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

uefi-raw/src/net.rs

Lines changed: 20 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, Formatter};
1312

1413
/// An IPv4 internet protocol address.
1514
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
@@ -83,19 +82,31 @@ pub union IpAddress {
8382
}
8483

8584
impl IpAddress {
85+
/// Zeroed variant where all bytes are guaranteed to be initialized to zero.
86+
pub const ZERO: Self = Self { addr: [0; 4] };
87+
8688
/// Construct a new IPv4 address.
89+
///
90+
/// The type won't know that it is an IPv6 address and additional context
91+
/// is needed.
92+
///
93+
/// # Safety
94+
/// The constructor only initializes the bytes needed for IPv4 addresses.
8795
#[must_use]
88-
pub const fn new_v4(ip_addr: [u8; 4]) -> Self {
96+
pub const fn new_v4(octets: [u8; 4]) -> Self {
8997
Self {
90-
v4: Ipv4Address(ip_addr),
98+
v4: Ipv4Address(octets),
9199
}
92100
}
93101

94102
/// Construct a new IPv6 address.
103+
///
104+
/// The type won't know that it is an IPv6 address and additional context
105+
/// is needed.
95106
#[must_use]
96-
pub const fn new_v6(ip_addr: [u8; 16]) -> Self {
107+
pub const fn new_v6(octets: [u8; 16]) -> Self {
97108
Self {
98-
v6: Ipv6Address(ip_addr),
109+
v6: Ipv6Address(octets),
99110
}
100111
}
101112
}
@@ -111,19 +122,15 @@ impl Debug for IpAddress {
111122

112123
impl Default for IpAddress {
113124
fn default() -> Self {
114-
Self { addr: [0u32; 4] }
125+
Self::ZERO
115126
}
116127
}
117128

118129
impl From<core::net::IpAddr> for IpAddress {
119130
fn from(t: core::net::IpAddr) -> Self {
120131
match t {
121-
core::net::IpAddr::V4(ip) => Self {
122-
v4: Ipv4Address::from(ip),
123-
},
124-
core::net::IpAddr::V6(ip) => Self {
125-
v6: Ipv6Address::from(ip),
126-
},
132+
core::net::IpAddr::V4(ip) => Self::new_v4(ip.octets()),
133+
core::net::IpAddr::V6(ip) => Self::new_v6(ip.octets()),
127134
}
128135
}
129136
}

0 commit comments

Comments
 (0)