Skip to content

Commit 6e891e5

Browse files
committed
uefi-raw: add .octets() for each type
This aligns the API with the core::net API.
1 parent f116086 commit 6e891e5

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

uefi-raw/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
- Added `PciRootBridgeIoProtocol`.
66
- Added `ConfigKeywordHandlerProtocol`.
77
- Added `HiiConfigAccessProtocol`.
8+
- Added `::octets()` for `IpAddress`, `Ipv4Address`, `Ipv6Address`, and
9+
`MacAddress` to streamline the API with `core::net`.
810

911
## Changed
1012
- The documentation for UEFI protocols has been streamlined and improved.

uefi-raw/src/net.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ use core::fmt::{Debug, Formatter};
1616
#[repr(transparent)]
1717
pub struct Ipv4Address(pub [u8; 4]);
1818

19+
impl Ipv4Address {
20+
/// Returns the octets of the IP address.
21+
#[must_use]
22+
pub const fn octets(self) -> [u8; 4] {
23+
self.0
24+
}
25+
}
26+
1927
impl From<core::net::Ipv4Addr> for Ipv4Address {
2028
fn from(ip: core::net::Ipv4Addr) -> Self {
2129
Self(ip.octets())
@@ -33,6 +41,14 @@ impl From<Ipv4Address> for core::net::Ipv4Addr {
3341
#[repr(transparent)]
3442
pub struct Ipv6Address(pub [u8; 16]);
3543

44+
impl Ipv6Address {
45+
/// Returns the octets of the IP address.
46+
#[must_use]
47+
pub const fn octets(self) -> [u8; 16] {
48+
self.0
49+
}
50+
}
51+
3652
impl From<core::net::Ipv6Addr> for Ipv6Address {
3753
fn from(ip: core::net::Ipv6Addr) -> Self {
3854
Self(ip.octets())
@@ -82,6 +98,16 @@ impl IpAddress {
8298
v6: Ipv6Address(ip_addr),
8399
}
84100
}
101+
102+
/// Returns the octets of the union. Without additional context, it is not
103+
/// clear whether the octets represent an IPv4 or IPv6 address.
104+
///
105+
/// # Safety
106+
/// Callers must be sure that all underlying bytes were initialized.
107+
#[must_use]
108+
pub const unsafe fn octets(&self) -> [u8; 16] {
109+
unsafe { self.v6.octets() }
110+
}
85111
}
86112

87113
impl Debug for IpAddress {
@@ -125,6 +151,15 @@ impl From<core::net::IpAddr> for IpAddress {
125151
#[repr(transparent)]
126152
pub struct MacAddress(pub [u8; 32]);
127153

154+
impl MacAddress {
155+
/// Returns the octets of the MAC address.
156+
#[must_use]
157+
pub const fn octets(self) -> [u8; 32] {
158+
self.0
159+
}
160+
}
161+
162+
// Normal/typical MAC addresses, such as in Ethernet.
128163
impl From<[u8; 6]> for MacAddress {
129164
fn from(octets: [u8; 6]) -> Self {
130165
let mut buffer = [0; 32];

0 commit comments

Comments
 (0)