@@ -12,6 +12,13 @@ use core::fmt;
12
12
use core:: fmt:: { Debug , Formatter } ;
13
13
14
14
/// An IPv4 internet protocol address.
15
+ ///
16
+ /// # Conversions and Relation to [`core::net`]
17
+ ///
18
+ /// The following [`From`] implementations exist:
19
+ /// - `[u8; 4]` -> [`Ipv4Address`]
20
+ /// - [`core::net::Ipv4Addr`] -> [`Ipv4Address`]
21
+ /// - [`core::net::IpAddr`] -> [`Ipv4Address`]
15
22
#[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
16
23
#[ repr( transparent) ]
17
24
pub struct Ipv4Address ( pub [ u8 ; 4 ] ) ;
@@ -36,7 +43,20 @@ impl From<Ipv4Address> for core::net::Ipv4Addr {
36
43
}
37
44
}
38
45
46
+ impl From < [ u8 ; 4 ] > for Ipv4Address {
47
+ fn from ( octets : [ u8 ; 4 ] ) -> Self {
48
+ Self ( octets)
49
+ }
50
+ }
51
+
39
52
/// An IPv6 internet protocol address.
53
+ ///
54
+ /// # Conversions and Relation to [`core::net`]
55
+ ///
56
+ /// The following [`From`] implementations exist:
57
+ /// - `[u8; 16]` -> [`Ipv6Address`]
58
+ /// - [`core::net::Ipv6Addr`] -> [`Ipv6Address`]
59
+ /// - [`core::net::IpAddr`] -> [`Ipv6Address`]
40
60
#[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
41
61
#[ repr( transparent) ]
42
62
pub struct Ipv6Address ( pub [ u8 ; 16 ] ) ;
@@ -61,12 +81,27 @@ impl From<Ipv6Address> for core::net::Ipv6Addr {
61
81
}
62
82
}
63
83
84
+ impl From < [ u8 ; 16 ] > for Ipv6Address {
85
+ fn from ( octets : [ u8 ; 16 ] ) -> Self {
86
+ Self ( octets)
87
+ }
88
+ }
89
+
64
90
/// An IPv4 or IPv6 internet protocol address that is ABI compatible with EFI.
65
91
///
66
92
/// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This
67
93
/// type is defined in the same way as edk2 for compatibility with C code. Note
68
94
/// that this is an untagged union, so there's no way to tell which type of
69
95
/// address an `IpAddress` value contains without additional context.
96
+ ///
97
+ /// # Conversions and Relation to [`core::net`]
98
+ ///
99
+ /// The following [`From`] implementations exist:
100
+ /// - `[u8; 4]` -> [`IpAddress`]
101
+ /// - `[u8; 16]` -> [`IpAddress`]
102
+ /// - [`core::net::Ipv4Addr`] -> [`IpAddress`]
103
+ /// - [`core::net::Ipv6Addr`] -> [`IpAddress`]
104
+ /// - [`core::net::IpAddr`] -> [`IpAddress`]
70
105
#[ derive( Clone , Copy ) ]
71
106
#[ repr( C ) ]
72
107
pub union IpAddress {
@@ -128,6 +163,30 @@ impl From<core::net::IpAddr> for IpAddress {
128
163
}
129
164
}
130
165
166
+ impl From < core:: net:: Ipv4Addr > for IpAddress {
167
+ fn from ( value : core:: net:: Ipv4Addr ) -> Self {
168
+ Self :: new_v4 ( value. octets ( ) )
169
+ }
170
+ }
171
+
172
+ impl From < core:: net:: Ipv6Addr > for IpAddress {
173
+ fn from ( value : core:: net:: Ipv6Addr ) -> Self {
174
+ Self :: new_v6 ( value. octets ( ) )
175
+ }
176
+ }
177
+
178
+ impl From < [ u8 ; 4 ] > for IpAddress {
179
+ fn from ( octets : [ u8 ; 4 ] ) -> Self {
180
+ Self :: new_v4 ( octets)
181
+ }
182
+ }
183
+
184
+ impl From < [ u8 ; 16 ] > for IpAddress {
185
+ fn from ( octets : [ u8 ; 16 ] ) -> Self {
186
+ Self :: new_v6 ( octets)
187
+ }
188
+ }
189
+
131
190
/// UEFI Media Access Control (MAC) address.
132
191
///
133
192
/// UEFI supports multiple network protocols and hardware types, not just
@@ -137,6 +196,13 @@ impl From<core::net::IpAddr> for IpAddress {
137
196
///
138
197
/// In most cases, this is just a typical `[u8; 6]` Ethernet style MAC
139
198
/// address with the rest of the bytes being zero.
199
+ ///
200
+ /// # Conversions and Relation to [`core::net`]
201
+ ///
202
+ /// There is no matching type in [`core::net`] but the following [`From`]
203
+ /// implementations exist:
204
+ /// - `[u8; 6]` -> [`MacAddress`]
205
+ /// - `[u8; 32]` -> [`MacAddress`]
140
206
#[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
141
207
#[ repr( transparent) ]
142
208
pub struct MacAddress ( pub [ u8 ; 32 ] ) ;
@@ -164,6 +230,13 @@ impl From<MacAddress> for [u8; 6] {
164
230
}
165
231
}
166
232
233
+ // UEFI MAC addresses.
234
+ impl From < [ u8 ; 32 ] > for MacAddress {
235
+ fn from ( octets : [ u8 ; 32 ] ) -> Self {
236
+ Self ( octets)
237
+ }
238
+ }
239
+
167
240
#[ cfg( test) ]
168
241
mod tests {
169
242
use super :: * ;
@@ -216,4 +289,64 @@ mod tests {
216
289
assert_eq ! ( align_of:: <PackedHelper <IpAddress >>( ) , 1 ) ;
217
290
assert_eq ! ( size_of:: <PackedHelper <IpAddress >>( ) , 16 ) ;
218
291
}
292
+
293
+ /// Tests the From-impls from the documentation.
294
+ #[ test]
295
+ fn test_promised_from_impls ( ) {
296
+ // octets -> Ipv4Address
297
+ {
298
+ let octets = [ 0_u8 , 1 , 2 , 3 ] ;
299
+ assert_eq ! ( Ipv4Address :: from( octets) , Ipv4Address ( octets) ) ;
300
+ let uefi_addr = IpAddress :: from ( octets) ;
301
+ assert_eq ! ( & octets, & unsafe { uefi_addr. v4. octets( ) } ) ;
302
+ }
303
+ // octets -> Ipv6Address
304
+ {
305
+ let octets = [ 0_u8 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 ] ;
306
+ assert_eq ! ( Ipv6Address :: from( octets) , Ipv6Address ( octets) ) ;
307
+ let uefi_addr = IpAddress :: from ( octets) ;
308
+ assert_eq ! ( & octets, & unsafe { uefi_addr. v6. octets( ) } ) ;
309
+ }
310
+ // StdIpv4Addr -> Ipv4Address
311
+ {
312
+ let octets = [ 7 , 5 , 3 , 1 ] ;
313
+ let core_ipv4_addr = core:: net:: Ipv4Addr :: from ( octets) ;
314
+ assert_eq ! ( Ipv4Address :: from( core_ipv4_addr) . octets( ) , octets) ;
315
+ assert_eq ! (
316
+ unsafe { IpAddress :: from( core_ipv4_addr) . v4. octets( ) } ,
317
+ octets
318
+ ) ;
319
+ }
320
+ // StdIpv6Addr -> Ipv6Address
321
+ {
322
+ let octets = [ 7 , 5 , 3 , 1 , 6 , 3 , 8 , 5 , 2 , 5 , 2 , 7 , 3 , 5 , 2 , 6 ] ;
323
+ let core_ipv6_addr = core:: net:: Ipv6Addr :: from ( octets) ;
324
+ assert_eq ! ( Ipv6Address :: from( core_ipv6_addr) . octets( ) , octets) ;
325
+ assert_eq ! (
326
+ unsafe { IpAddress :: from( core_ipv6_addr) . v6. octets( ) } ,
327
+ octets
328
+ ) ;
329
+ }
330
+ // StdIpAddr -> IpAddress
331
+ {
332
+ let octets = [ 8 , 8 , 2 , 6 ] ;
333
+ let core_ip_addr = core:: net:: IpAddr :: from ( octets) ;
334
+ assert_eq ! ( unsafe { IpAddress :: from( core_ip_addr) . v4. octets( ) } , octets) ;
335
+ }
336
+ // octets -> MacAddress
337
+ {
338
+ let octets = [ 8 , 8 , 2 , 6 , 6 , 7 ] ;
339
+ let uefi_mac_addr = MacAddress :: from ( octets) ;
340
+ assert_eq ! ( uefi_mac_addr. octets( ) [ 0 ..6 ] , octets) ;
341
+ }
342
+ // octets -> MacAddress
343
+ {
344
+ let octets = [
345
+ 8_u8 , 8 , 2 , 6 , 6 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 5 , 7 , 0 , 0 , 0 ,
346
+ 0 , 0 , 0 , 0 , 42 ,
347
+ ] ;
348
+ let uefi_mac_addr = MacAddress :: from ( octets) ;
349
+ assert_eq ! ( uefi_mac_addr. octets( ) , octets) ;
350
+ }
351
+ }
219
352
}
0 commit comments