File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff 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
149170impl 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.
You can’t perform that action at this time.
0 commit comments