Skip to content

Commit 86d1387

Browse files
committed
uefi-raw
1 parent da896c6 commit 86d1387

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

uefi-raw/src/lib.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ impl IpAddress {
150150
pub const fn octets(&self) -> [u8; 16] {
151151
unsafe { self.v6.octets() }
152152
}
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+
}
153165
}
154166

155167
impl Debug for IpAddress {
@@ -246,16 +258,29 @@ mod tests {
246258
assert_eq!(size_of::<Ipv6Addr>(), 16);
247259
assert_eq!(align_of::<Ipv6Addr>(), 1);
248260
}
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+
249274
/// Test conversion from `core::net::IpAddr` to `IpvAddress`.
250275
///
251276
/// Note that conversion in the other direction is not possible.
252277
#[test]
253278
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));
255280
let uefi_addr = IpAddress::from(core_addr);
256281
assert_eq!(unsafe { uefi_addr.v4.octets() }, TEST_IPV4);
257282

258-
let core_addr = IpAddr::V6(core::net::Ipv6Addr::from(TEST_IPV6));
283+
let core_addr = IpAddr::V6(Ipv6Addr::from(TEST_IPV6));
259284
let uefi_addr = IpAddress::from(core_addr);
260285
assert_eq!(unsafe { uefi_addr.v6.octets() }, TEST_IPV6);
261286
}

0 commit comments

Comments
 (0)