project_ptr_mut

Macro project_ptr_mut 

Source
macro_rules! project_ptr_mut {
    ($expr:expr => {$( $($props:tt)=>+ ),* $(,)?}) => { ... };
    ($expr:expr => $($props:tt)=>+) => { ... };
}
Expand description

Unsafe: Given a *mut pointer to a struct, obtain *mut pointers to one or more of its fields.

This does not statically check whether multiple pointers to the same data are returned. This must be used in an unsafe block or function.

ยงUsage


let mut bob = Person {
    name: Name { first: "Bob", last: "Jones" },
    age: 35,
    id: (111, 222),
};
let bob_ptr: *mut Person = &mut bob;

unsafe {
    // Pointer to a single field:
    let age: *mut u32 = project_ptr_mut!(bob_ptr => age);
    *age = 36;

    // Pointers to multiple fields:
    let (first, last, id0): (*mut &str, *mut &str, *mut usize) = project_ptr_mut!(
        bob_ptr => { name => first, name => last, id => 0 }
    );
    *first = "Robert";
    *last = "Johns";
    *id0 = 444;
    assert_eq!(bob, Person {
        name: Name { first: "Robert", last: "Johns" },
        age: 36,
        id: (444, 222),
    });
}