Skip to content

Commit f0ad8a0

Browse files
Merge pull request #1564 from ifd3f/feature/net-type-conversions
Implement conversions for IpAddress and MacAddress
2 parents 13c1c2b + 60b7c64 commit f0ad8a0

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

uefi-raw/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Added
44
- Added `Boolean` type
55
- Added `protocol::network::pxe` module.
6+
- Added conversions between `MacAddress` and the `[u8; 6]` type that's more commonly used to represent MAC addresses.
67

78

89
# uefi-raw - 0.10.0 (2025-02-07)

uefi-raw/src/lib.rs

+19
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ impl Default for IpAddress {
175175
#[repr(transparent)]
176176
pub struct MacAddress(pub [u8; 32]);
177177

178+
impl From<[u8; 6]> for MacAddress {
179+
fn from(octets: [u8; 6]) -> Self {
180+
let mut buffer = [0; 32];
181+
buffer[0] = octets[0];
182+
buffer[1] = octets[1];
183+
buffer[2] = octets[2];
184+
buffer[3] = octets[3];
185+
buffer[4] = octets[4];
186+
buffer[5] = octets[5];
187+
Self(buffer)
188+
}
189+
}
190+
191+
impl From<MacAddress> for [u8; 6] {
192+
fn from(MacAddress(o): MacAddress) -> Self {
193+
[o[0], o[1], o[2], o[3], o[4], o[5]]
194+
}
195+
}
196+
178197
#[cfg(test)]
179198
mod tests {
180199
use super::*;

uefi/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Added
44
- Added `boot::signal_event`.
5+
- Added conversions between `proto::network::IpAddress` and `core::net` types.
6+
- Added conversions between `proto::network::MacAddress` and the `[u8; 6]` type that's more commonly used to represent MAC addresses.
57

68
## Changed
79
- **Breaking:** Removed `BootPolicyError` as `BootPolicy` construction is no

uefi/src/proto/network/mod.rs

+37
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,40 @@ impl IpAddress {
3434
Self(ip_addr)
3535
}
3636
}
37+
38+
impl From<core::net::Ipv4Addr> for IpAddress {
39+
fn from(t: core::net::Ipv4Addr) -> Self {
40+
Self::new_v4(t.octets())
41+
}
42+
}
43+
44+
impl From<IpAddress> for core::net::Ipv4Addr {
45+
fn from(IpAddress(o): IpAddress) -> Self {
46+
Self::from([o[0], o[1], o[2], o[3]])
47+
}
48+
}
49+
50+
impl From<core::net::Ipv6Addr> for IpAddress {
51+
fn from(t: core::net::Ipv6Addr) -> Self {
52+
Self::new_v6(t.octets())
53+
}
54+
}
55+
56+
impl From<IpAddress> for core::net::Ipv6Addr {
57+
fn from(value: IpAddress) -> Self {
58+
Self::from(value.0)
59+
}
60+
}
61+
62+
impl From<core::net::IpAddr> for IpAddress {
63+
fn from(t: core::net::IpAddr) -> Self {
64+
match t {
65+
core::net::IpAddr::V4(a) => a.into(),
66+
core::net::IpAddr::V6(a) => a.into(),
67+
}
68+
}
69+
}
70+
71+
// NOTE: We cannot impl From<IpAddress> for core::net::IpAddr
72+
// because IpAddress is a raw union, with nothing indicating
73+
// whether it should be considered v4 or v6.

0 commit comments

Comments
 (0)