Expand description
Some types to allow deciding at compile time if an option contains a value or which variant from the either type is active. This might be useful when you have some const generic type that should decide whether to use one datastructure or another, or no datastructure at all.
§Syntax
ⓘ
let _definitely_none = ConstOption::<String, false>::new();
let definitely_some = ConstOption::<String, true>::new("hello, world".to_string());
// When there is definitely some value, the `Deref` trait can be used.
println!("{}", &*definitely_some);
// Obtain the string inside.
let contained_string = definitely_some.into_inner();
struct Container<T, const IS_UNIQUE: bool> {
data: ConstEither<Vec<T>, HashSet<T>, UNIQUE>,
}
impl<T> Container<T, false> {
fn insert(&mut self, val: T) {
/* ... */
}
}
impl<T: Eq + Hash> Container<T, true> {
fn insert(&mut self, val: T) -> Result<(), T> {
/* ... */
}
}
§Drawbacks
Because of the current state of rust, the type ConstEither<L, R>
will have the size and
alignment of the largest from L
and R
.
Structs§
- Const
Either - An
Either
type that is known to hold a left or right value at compile-time. This allows data structures choose an appropriate type based on some compile-time determined policy. - Const
Option - An
Option
type that is known at compile-time to have or not some value. This is usefull for writing data structures that use const generics and would like to hold or not a value of some type based on a compile-time rule.