munge makes it easy and safe to destructure MaybeUninits, Cells,
UnsafeCells, ManuallyDrops, and more.
Just use the munge! macro to destructure opaque types the same way you'd
destructure a value. The munge! macro may be used to perform either borrow
destructuring (e.g. let (a, b) = c where c is a reference) or move
destructuring (e.g. let (a, b) = c where c is a value) depending on the
type.
munge has no features and is always #![no_std].
Examples
munge makes it easy to initialize MaybeUninits:
use ;
let mut mu = uninit;
munge!;
assert_eq!;
assert_eq!;
assert_eq!;
// Note that `mut` bindings can be reassigned like you'd expect:
f = &mut uninit;
// SAFETY: `mu` is completely initialized.
let init = unsafe ;
assert_eq!;
assert_eq!;
assert_eq!;
It can also be used to destructure Cells:
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!;
You can even extend munge to work with your own types by implementing its
Destructure and Restructure traits:
use ;
;
// SAFETY:
// - `Invariant<T>` is destructured by move, so its `Destructuring` type is
// `Move`.
// - `underlying` returns a pointer to its inner type, so it is guaranteed
// to be non-null, properly aligned, and valid for reads.
unsafe
// SAFETY: `restructure` returns an `Invariant<U>` that takes ownership of
// the restructured field because `Invariant<T>` is destructured by move.
unsafe
// SAFETY: `(1, 2, 3)` upholds my custom invariant.
let value = unsafe ;
munge!;
assert_eq!;
assert_eq!;
assert_eq!;