@@ -38,6 +38,7 @@ pub use uguid::{guid, Guid};
3838
3939use core:: ffi:: c_void;
4040use core:: fmt:: { self , Debug , Formatter } ;
41+ use core:: net:: { IpAddr , Ipv4Addr , Ipv6Addr } ;
4142
4243/// Handle to an event structure.
4344pub type Event = * mut c_void ;
@@ -106,40 +107,6 @@ impl From<Boolean> for bool {
106107 }
107108}
108109
109- /// An IPv4 internet protocol address.
110- #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
111- #[ repr( transparent) ]
112- pub struct Ipv4Address ( pub [ u8 ; 4 ] ) ;
113-
114- impl From < core:: net:: Ipv4Addr > for Ipv4Address {
115- fn from ( ip : core:: net:: Ipv4Addr ) -> Self {
116- Self ( ip. octets ( ) )
117- }
118- }
119-
120- impl From < Ipv4Address > for core:: net:: Ipv4Addr {
121- fn from ( ip : Ipv4Address ) -> Self {
122- Self :: from ( ip. 0 )
123- }
124- }
125-
126- /// An IPv6 internet protocol address.
127- #[ derive( Clone , Copy , Debug , Default , Eq , PartialEq , Ord , PartialOrd , Hash ) ]
128- #[ repr( transparent) ]
129- pub struct Ipv6Address ( pub [ u8 ; 16 ] ) ;
130-
131- impl From < core:: net:: Ipv6Addr > for Ipv6Address {
132- fn from ( ip : core:: net:: Ipv6Addr ) -> Self {
133- Self ( ip. octets ( ) )
134- }
135- }
136-
137- impl From < Ipv6Address > for core:: net:: Ipv6Addr {
138- fn from ( ip : Ipv6Address ) -> Self {
139- Self :: from ( ip. 0 )
140- }
141- }
142-
143110/// An IPv4 or IPv6 internet protocol address.
144111///
145112/// Corresponds to the `EFI_IP_ADDRESS` type in the UEFI specification. This
@@ -150,10 +117,10 @@ impl From<Ipv6Address> for core::net::Ipv6Addr {
150117#[ repr( C ) ]
151118pub union IpAddress {
152119 /// An IPv4 internet protocol address.
153- pub v4 : Ipv4Address ,
120+ pub v4 : Ipv4Addr ,
154121
155122 /// An IPv6 internet protocol address.
156- pub v6 : Ipv6Address ,
123+ pub v6 : Ipv6Addr ,
157124
158125 /// This member serves to align the whole type to 4 bytes as required by
159126 /// the spec. Note that this is slightly different from `repr(align(4))`,
@@ -164,17 +131,17 @@ pub union IpAddress {
164131impl IpAddress {
165132 /// Construct a new IPv4 address.
166133 #[ must_use]
167- pub const fn new_v4 ( ip_addr : [ u8 ; 4 ] ) -> Self {
134+ pub fn new_v4 ( ip_addr : [ u8 ; 4 ] ) -> Self {
168135 Self {
169- v4 : Ipv4Address ( ip_addr) ,
136+ v4 : Ipv4Addr :: from ( ip_addr) ,
170137 }
171138 }
172139
173140 /// Construct a new IPv6 address.
174141 #[ must_use]
175- pub const fn new_v6 ( ip_addr : [ u8 ; 16 ] ) -> Self {
142+ pub fn new_v6 ( ip_addr : [ u8 ; 16 ] ) -> Self {
176143 Self {
177- v6 : Ipv6Address ( ip_addr) ,
144+ v6 : Ipv6Addr :: from ( ip_addr) ,
178145 }
179146 }
180147}
@@ -190,18 +157,20 @@ impl Debug for IpAddress {
190157
191158impl Default for IpAddress {
192159 fn default ( ) -> Self {
193- Self { _align_helper : [ 0u32 ; 4 ] }
160+ Self {
161+ _align_helper : [ 0u32 ; 4 ] ,
162+ }
194163 }
195164}
196165
197- impl From < core :: net :: IpAddr > for IpAddress {
198- fn from ( t : core :: net :: IpAddr ) -> Self {
166+ impl From < IpAddr > for IpAddress {
167+ fn from ( t : IpAddr ) -> Self {
199168 match t {
200- core :: net :: IpAddr :: V4 ( ip) => Self {
201- v4 : Ipv4Address :: from ( ip) ,
169+ IpAddr :: V4 ( ip) => Self {
170+ v4 : Ipv4Addr :: from ( ip) ,
202171 } ,
203- core :: net :: IpAddr :: V6 ( ip) => Self {
204- v6 : Ipv6Address :: from ( ip) ,
172+ IpAddr :: V6 ( ip) => Self {
173+ v6 : Ipv6Addr :: from ( ip) ,
205174 } ,
206175 }
207176 }
@@ -261,33 +230,27 @@ mod tests {
261230 assert ! ( bool :: from( Boolean ( 0b11111111 ) ) ) ;
262231 }
263232
264- /// Test round-trip conversion between `Ipv4Address` and `core::net::Ipv4Addr`.
265- #[ test]
266- fn test_ip_addr4_conversion ( ) {
267- let uefi_addr = Ipv4Address ( TEST_IPV4 ) ;
268- let core_addr = core:: net:: Ipv4Addr :: from ( uefi_addr) ;
269- assert_eq ! ( uefi_addr, Ipv4Address :: from( core_addr) ) ;
270- }
271-
272- /// Test round-trip conversion between `Ipv6Address` and `core::net::Ipv6Addr`.
233+ /// We test that the core::net-types are ABI compatible with the EFI types.
234+ /// As long as this is the case, we can reuse core functionality and
235+ /// prevent type duplication.
273236 #[ test]
274- fn test_ip_addr6_conversion ( ) {
275- let uefi_addr = Ipv6Address ( TEST_IPV6 ) ;
276- let core_addr = core:: net:: Ipv6Addr :: from ( uefi_addr) ;
277- assert_eq ! ( uefi_addr, Ipv6Address :: from( core_addr) ) ;
237+ fn net_abi ( ) {
238+ assert_eq ! ( size_of:: <Ipv4Addr >( ) , 4 ) ;
239+ assert_eq ! ( align_of:: <Ipv4Addr >( ) , 1 ) ;
240+ assert_eq ! ( size_of:: <Ipv6Addr >( ) , 16 ) ;
241+ assert_eq ! ( align_of:: <Ipv6Addr >( ) , 1 ) ;
278242 }
279-
280243 /// Test conversion from `core::net::IpAddr` to `IpvAddress`.
281244 ///
282245 /// Note that conversion in the other direction is not possible.
283246 #[ test]
284247 fn test_ip_addr_conversion ( ) {
285- let core_addr = core :: net :: IpAddr :: V4 ( core:: net:: Ipv4Addr :: from ( TEST_IPV4 ) ) ;
248+ let core_addr = IpAddr :: V4 ( core:: net:: Ipv4Addr :: from ( TEST_IPV4 ) ) ;
286249 let uefi_addr = IpAddress :: from ( core_addr) ;
287- assert_eq ! ( unsafe { uefi_addr. v4. 0 } , TEST_IPV4 ) ;
250+ assert_eq ! ( unsafe { uefi_addr. v4. octets ( ) } , TEST_IPV4 ) ;
288251
289- let core_addr = core :: net :: IpAddr :: V6 ( core:: net:: Ipv6Addr :: from ( TEST_IPV6 ) ) ;
252+ let core_addr = IpAddr :: V6 ( core:: net:: Ipv6Addr :: from ( TEST_IPV6 ) ) ;
290253 let uefi_addr = IpAddress :: from ( core_addr) ;
291- assert_eq ! ( unsafe { uefi_addr. v6. 0 } , TEST_IPV6 ) ;
254+ assert_eq ! ( unsafe { uefi_addr. v6. octets ( ) } , TEST_IPV6 ) ;
292255 }
293256}
0 commit comments