occupied
A simple utility library for transitioning enums into and out of occupied states
occupied provides compile-time guaranteed ways to interact with inserting and removing items into Option. This simplifies more complicated access patterns, when you're interacting with a handful of options and don't want to .take() anything out of them until they've all been verified somehow.
Example
Suppose you had a tuple of options, and wanted to unwrap them all, but only
if they're all Some, and leave them all untouched otherwise. If you own the
tuple, this is easy to do with a match, but if not, you have to either:
- Manually check
.is_some()on all of them, and then.unwrap()them only after they've all been checked, or .take()them one-by-one, and take care to restore each option to its original state after.
occupied provides a way to preserve the is_some() check within the type
system, making it checked at compile-time, without actually mutating the option.
use OptionExt as _;
let mut opts = ;
assert_eq!;
assert_eq!;
opts.3 = Some;
assert_eq!;
assert_eq!;