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 @@ -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
150171impl 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.
You can’t perform that action at this time.
0 commit comments