munge
makes it easy and safe to destructure raw pointers, MaybeUninit
s, Cell
s, and Pin
s.
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]
.
Examples
munge
makes it easy to initialize MaybeUninit
s:
use ;
let mut mu = uninit;
munge!;
assert_eq!;
assert_eq!;
assert_eq!;
f = &mut uninit;
// SAFETY: `mu` is completely initialized.
let init = unsafe ;
assert_eq!;
assert_eq!;
assert_eq!;
It can also be used to destructure Cell
s:
use ;
let value = Example ;
let cell = new;
munge!;
assert_eq!;
a.set;
assert_eq!;
c.set;
assert_eq!;
f.set;
let value = cell.into_inner;
assert_eq!;
assert_eq!;
assert_eq!;
And Pin
s as long as all fields are structurally pinned:
use ;
// SAFETY: `Example` obeys structural pinning.
unsafe
let mut value = Example ;
// SAFETY: `value` will not be moved before being dropped.
let mut pin = unsafe ;
munge!;
*a.get_mut = 1;
*b.get_mut = 'a';
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
You can even extend munge
to work with your own types by implementing its Destructure
and
Restructure
traits.