@@ -150,6 +150,18 @@ impl IpAddress {
150
150
pub const fn octets ( & self ) -> [ u8 ; 16 ] {
151
151
unsafe { self . v6 . octets ( ) }
152
152
}
153
+
154
+ /// Returns a raw pointer to the IP address.
155
+ #[ must_use]
156
+ pub const fn as_ptr ( & self ) -> * const u8 {
157
+ core:: ptr:: addr_of!( * self ) . cast ( )
158
+ }
159
+
160
+ /// Returns a raw mutable pointer to the IP address.
161
+ #[ must_use]
162
+ pub fn as_ptr_mut ( & mut self ) -> * mut u8 {
163
+ core:: ptr:: addr_of_mut!( * self ) . cast ( )
164
+ }
153
165
}
154
166
155
167
impl Debug for IpAddress {
@@ -246,16 +258,29 @@ mod tests {
246
258
assert_eq ! ( size_of:: <Ipv6Addr >( ) , 16 ) ;
247
259
assert_eq ! ( align_of:: <Ipv6Addr >( ) , 1 ) ;
248
260
}
261
+
262
+ #[ test]
263
+ fn ip_ptr ( ) {
264
+ let mut ip = IpAddress :: new_v4 ( [ 0 ; 4 ] ) ;
265
+ unsafe {
266
+ core:: ptr:: write ( ip. as_ptr_mut ( ) , 192 ) ;
267
+ core:: ptr:: write ( ip. as_ptr_mut ( ) . add ( 1 ) , 168 ) ;
268
+ core:: ptr:: write ( ip. as_ptr_mut ( ) . add ( 2 ) , 42 ) ;
269
+ core:: ptr:: write ( ip. as_ptr_mut ( ) . add ( 3 ) , 73 ) ;
270
+ }
271
+ unsafe { assert_eq ! ( ip. v4. octets( ) , [ 192 , 168 , 42 , 73 ] ) }
272
+ }
273
+
249
274
/// Test conversion from `core::net::IpAddr` to `IpvAddress`.
250
275
///
251
276
/// Note that conversion in the other direction is not possible.
252
277
#[ test]
253
278
fn test_ip_addr_conversion ( ) {
254
- let core_addr = IpAddr :: V4 ( core :: net :: Ipv4Addr :: from ( TEST_IPV4 ) ) ;
279
+ let core_addr = IpAddr :: V4 ( Ipv4Addr :: from ( TEST_IPV4 ) ) ;
255
280
let uefi_addr = IpAddress :: from ( core_addr) ;
256
281
assert_eq ! ( unsafe { uefi_addr. v4. octets( ) } , TEST_IPV4 ) ;
257
282
258
- let core_addr = IpAddr :: V6 ( core :: net :: Ipv6Addr :: from ( TEST_IPV6 ) ) ;
283
+ let core_addr = IpAddr :: V6 ( Ipv6Addr :: from ( TEST_IPV6 ) ) ;
259
284
let uefi_addr = IpAddress :: from ( core_addr) ;
260
285
assert_eq ! ( unsafe { uefi_addr. v6. octets( ) } , TEST_IPV6 ) ;
261
286
}
0 commit comments