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§
- Iter
Bool - 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 particularNonevalue from the value domain (seeNoned) - Optioned
Iter - iterate over an Optioned
Enums§
- Option
Bool - The
OptionBooltype, a space-efficient Optionreplacement
Traits§
- Noned
- A trait whose implementation for any type
Tallows the use ofOptioned<T>whereTis bound by bothSizedandCopy. - OptEq
- Equality within Optioned
- OptOrd
- Ordering within Optioned