macro_rules! munge {
($($t:tt)*) => { ... };
}Expand description
Destructures a type using a pattern.
To prevent unsound union destructurings, this macro emits field accesses
which fail to compile in safe Rust. However, if munge! is used inside of
an unsafe block, these accesses will compile without emitting an error.
This matches the behavior of regular destructuring, but may be surprising in
some situations.
ยงExample
pub struct Example {
a: u32,
b: (char, f32),
}
let mut mu = MaybeUninit::<Example>::uninit();
munge!(let Example { a, b: (c, mut f) } = &mut mu);
assert_eq!(a.write(10), &10);
assert_eq!(c.write('x'), &'x');
assert_eq!(f.write(3.14), &3.14);
// Note that `mut` bindings can be reassigned like you'd expect:
let mut new_f = MaybeUninit::uninit();
f = &mut new_f;
// SAFETY: `mu` is completely initialized.
let init = unsafe { mu.assume_init() };
assert_eq!(init.a, 10);
assert_eq!(init.b.0, 'x');
assert_eq!(init.b.1, 3.14);