Skip to content

Commit 4e10ef9

Browse files
committed
uefi-raw: add convenient into_* helpers
1 parent 5cdb94c commit 4e10ef9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

uefi-raw/src/net.rs

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

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

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

0 commit comments

Comments
 (0)