| Crates.io | option_trait |
| lib.rs | option_trait |
| version | 1.0.7 |
| created_at | 2023-06-24 01:35:53.809685+00 |
| updated_at | 2025-02-05 01:21:39.526753+00 |
| description | Helper traits for more generalized options |
| homepage | |
| repository | https://github.com/sigurd4/option_trait |
| max_upload_size | |
| id | 898736 |
| size | 187,202 |
Provides the Optional trait for Options, as well as compile-time managed Option alternatives, all generalized under the trait Maybe.
Maybe<T> is implemented for:
Option<T>
Optional and PureMaybeT and ()
PureStaticMaybe, PureMaybe and StaticMaybe[T; 1] and [T; 0]
StaticMaybeOptCell<T, _> (only if feature opt_cell is enabled)
StaticMaybeThis is how i like to handle optional function arguments with maximum flexibility.
use option_trait::*;
fn f<O>(required: i32, optional: O)
where
O: Maybe<i32>
{
if O::IS_MAYBE_SOME
{
let param = optional.unwrap_or(0);
// This part of the code will be disabled at compile-time if the maybe cannot
// possibly contain a value.
}
// Do whatever
}
f(1, 2);
f(1, ());
f(1, Some(2));
f(1, None);
f(1, [2]);
f(1, [] as [i32; 0]);
f(1, OptCell::some(2));
f(1, OptCell::none());