Skip to content

Commit 864620d

Browse files
committed
uefi-raw: small code improvements for IpAddress
1 parent 93a2d47 commit 864620d

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
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: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,31 @@ pub union IpAddress {
8383
}
8484

8585
impl IpAddress {
86+
/// Zeroed variant where all bytes are guaranteed to be initialized to zero.
87+
pub const ZERO: Self = Self { addr: [0; 4] };
88+
8689
/// Construct a new IPv4 address.
90+
///
91+
/// The type won't know that it is an IPv6 address and additional context
92+
/// is needed.
93+
///
94+
/// # Safety
95+
/// The constructor only initializes the bytes needed for IPv4 addresses.
8796
#[must_use]
88-
pub const fn new_v4(ip_addr: [u8; 4]) -> Self {
97+
pub const fn new_v4(octets: [u8; 4]) -> Self {
8998
Self {
90-
v4: Ipv4Address(ip_addr),
99+
v4: Ipv4Address(octets),
91100
}
92101
}
93102

94103
/// Construct a new IPv6 address.
104+
///
105+
/// The type won't know that it is an IPv6 address and additional context
106+
/// is needed.
95107
#[must_use]
96-
pub const fn new_v6(ip_addr: [u8; 16]) -> Self {
108+
pub const fn new_v6(octets: [u8; 16]) -> Self {
97109
Self {
98-
v6: Ipv6Address(ip_addr),
110+
v6: Ipv6Address(octets),
99111
}
100112
}
101113
}
@@ -111,19 +123,15 @@ impl Debug for IpAddress {
111123

112124
impl Default for IpAddress {
113125
fn default() -> Self {
114-
Self { addr: [0u32; 4] }
126+
Self::ZERO
115127
}
116128
}
117129

118130
impl From<StdIpAddr> for IpAddress {
119131
fn from(t: StdIpAddr) -> Self {
120132
match t {
121-
StdIpAddr::V4(ip) => Self {
122-
v4: Ipv4Address::from(ip),
123-
},
124-
StdIpAddr::V6(ip) => Self {
125-
v6: Ipv6Address::from(ip),
126-
},
133+
StdIpAddr::V4(ip) => Self::new_v4(ip.octets()),
134+
StdIpAddr::V6(ip) => Self::new_v6(ip.octets()),
127135
}
128136
}
129137
}

0 commit comments

Comments
 (0)