macro_rules! map_field { ($volatile:ident.$place:ident) => { ... }; }
Expand description
Provides safe field projection for volatile pointers referencing structs.
§Examples
Accessing a struct field:
use volatile::{VolatilePtr, map_field};
use core::ptr::NonNull;
struct Example { field_1: u32, field_2: u8, }
let mut value = Example { field_1: 15, field_2: 255 };
let mut volatile = unsafe { VolatilePtr::new((&mut value).into()) };
// construct a volatile reference to a field
let field_2 = map_field!(volatile.field_2);
assert_eq!(field_2.read(), 255);
Creating VolatilePtr
s to unaligned field in packed structs is not allowed:
ⓘ
use volatile::{VolatilePtr, map_field};
use core::ptr::NonNull;
#[repr(packed)]
struct Example { field_1: u8, field_2: usize, }
let mut value = Example { field_1: 15, field_2: 255 };
let mut volatile = unsafe { VolatilePtr::new((&mut value).into()) };
// Constructing a volatile reference to an unaligned field doesn't compile.
let field_2 = map_field!(volatile.field_2);