munge
Munge makes it easy and safe to destructure MaybeUninit
s, Cell
s,
UnsafeCell
s, ManuallyDrop
s, and more.
Documentation
- munge, the core library
- munge_macro, the proc macro at the core of munge.
Examples
Initialize MaybeUninit
s:
use MaybeUninit;
use munge;
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!;
Destructure Cell
s:
use Cell;
use munge;
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!;