Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 09994b8

Browse files
committed
uefi-raw: add convenient into_* helpers
1 parent 9fa22d9 commit 09994b8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

‎uefi-raw/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
- `[u8; 32]` --> `MacAddress`
1717
- `[u8; 4]` --> `Ipv4Address`, `IpAddress`
1818
- `[u8; 16]` --> `Ipv6Address`, `IpAddress`
19+
- Added `::into_core_ip_addr()` for `IpAddress`
20+
- Added `::try_into_ethernet_mac_addr()` for `MacAddress`
1921

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

‎uefi-raw/src/net.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,27 @@ impl IpAddress {
144144
v6: Ipv6Address(octets),
145145
}
146146
}
147+
148+
/// Transforms this EFI type to the Rust standard library's type
149+
/// [`StdIpAddr`].
150+
///
151+
/// # Arguments
152+
/// - `is_ipv6`: Whether the internal data should be interpreted as IPv6 or
153+
/// IPv4 address.
154+
///
155+
/// # Safety
156+
/// Callers must ensure that the `v4` field is valid if `is_ipv6` is false,
157+
/// and that the `v6` field is valid if `is_ipv6` is true
158+
#[must_use]
159+
pub unsafe fn into_core_ip_addr(self, is_ipv6: bool) -> core::net::IpAddr {
160+
if is_ipv6 {
161+
// SAFETY: Caller assumes that the underlying data is initialized.
162+
core::net::IpAddr::V6(core::net::Ipv6Addr::from(unsafe { self.v6.octets() }))
163+
} else {
164+
// SAFETY: Caller assumes that the underlying data is initialized.
165+
core::net::IpAddr::V4(core::net::Ipv4Addr::from(unsafe { self.v4.octets() }))
166+
}
167+
}
147168
}
148169

149170
impl Debug for IpAddress {
@@ -220,6 +241,17 @@ impl MacAddress {
220241
pub const fn octets(self) -> [u8; 32] {
221242
self.0
222243
}
244+
245+
/// Tries to interpret the MAC address as normal 6-byte MAC address, as used
246+
/// in ethernet.
247+
pub fn try_into_ethernet_mac_addr(self) -> Result<[u8; 6], [u8; 32]> {
248+
let extra = self.octets()[4..].iter().any(|&x| x != 0);
249+
if extra {
250+
Err(self.0)
251+
} else {
252+
Ok(self.octets()[..4].try_into().unwrap())
253+
}
254+
}
223255
}
224256

225257
// Normal/typical MAC addresses, such as in Ethernet.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /