munge
makes it easy and safe to destructure MaybeUninit
s, Cell
s, ManuallyDrop
s, and more.
Just use the munge!
macro to destructure opaque types the same way you'd destructure a value.
munge
has no features and is always #![no_std]
.
munge
makes it easy to initialize MaybeUninit
s:
```rust use { ::core::mem::MaybeUninit, ::munge::munge, };
pub struct Example { a: u32, b: (char, f32), }
let mut mu = MaybeUninit::
munge!(let Example { a, b: (c, mut f) } = &mut mu);
asserteq!(a.write(10), &10);
asserteq!(c.write('x'), &'x');
assert_eq!(f.write(3.14), &3.14);
// Note that mut
bindings can be reassigned like you'd expect:
f = &mut MaybeUninit::uninit();
// SAFETY: mu
is completely initialized.
let init = unsafe { mu.assumeinit() };
asserteq!(init.a, 10);
asserteq!(init.b.0, 'x');
asserteq!(init.b.1, 3.14);
```
It can also be used to destructure Cell
s:
```rust use { ::core::cell::Cell, ::munge::munge, };
pub struct Example { a: u32, b: (char, f32), }
let value = Example {
a: 10,
b: ('x', 3.14),
};
let cell = Cell::
munge!(let Example { a, b: (c, f) } = &cell); asserteq!(a.get(), 10); a.set(42); asserteq!(c.get(), 'x'); c.set('!'); assert_eq!(f.get(), 3.14); f.set(1.41);
let value = cell.intoinner(); asserteq!(value.a, 42); asserteq!(value.b.0, '!'); asserteq!(value.b.1, 1.41); ```
You can even extend munge
to work with your own types by implementing its Destructure
and
Restructure
traits.