Crate optional

Source
Expand description

§Space-efficient optional values

§Booleans

Type OptionBool represents an optional boolean value, similar to Option<bool>. Most function implementations are similar or equal. Note that the map_bool(..) and_bool(..), and_then_bool(..), or_bool(..) and or_else_bool(..) functions are working similar to the methods without the _bool suffix, but require and return OptionBool instead of Option<bool>. This allows people to stay within the type.

The OptionBool type is expected to require only 1 byte of storage:

assert!(1 == std::mem::size_of::<optional::OptionBool>());

However, since this crate was originally authored, improvements in the compiler have built in this optimization for Option<bool> as well. The OptionBool type remains however because it may still perform faster in some bench marks.

assert!(1 == std::mem::size_of::<Option<bool>>());

§Any type can be optional

Then there is the Optioned<T> type which wraps a type T as an optional value of T where one particular value represents None. Optioned<T> requires the exact same space as T:

assert!(std::mem::size_of::<optional::Optioned<i64>>() ==
    std::mem::size_of::<i64>());
assert!(std::mem::size_of::<optional::Optioned<f32>>() ==
    std::mem::size_of::<f32>());
assert!(std::mem::size_of::<optional::Optioned<u8>>() ==
    std::mem::size_of::<u8>());

There are implementations for u8..64,usize with std::u..::MAX representing None, also for i8..64,isize with std::i..::MIN representing None, and for f32, f64 with std::f..::NAN representing None.

Using Optioned for your own types is as simple as implementing Noned for your type, provided that your type is already Copy and Sized.

Structs§

IterBool
iterate over an OptionBool
Optioned
An Option<T>-like structure that takes only as much space as the enclosed value, at the cost of removing one particular None value from the value domain (see Noned)
OptionedIter
iterate over an Optioned

Enums§

OptionBool
The OptionBool type, a space-efficient Option replacement

Traits§

Noned
A trait whose implementation for any type T allows the use of Optioned<T> where T is bound by both Sized and Copy.
OptEq
Equality within Optioned
OptOrd
Ordering within Optioned

Functions§

none
Create a None Optioned<T>. Note that the type must be inferrible from the context, or you’d need to call with ::<T> where T is the specific type.
some
Create an Optioned<T> that is some(t).
wrap
Wrap a T into an Optioned<T>, regardless of its None-ness.