option_trait

Crates.iooption_trait
lib.rsoption_trait
version1.0.7
sourcesrc
created_at2023-06-24 01:35:53.809685+00
updated_at2025-02-05 01:21:39.526753+00
descriptionHelper traits for more generalized options
homepage
repositoryhttps://github.com/sigurd4/option_trait
max_upload_size
id898736
size187,202
sigurd4 (sigurd4)

documentation

README

Build Status (nightly) Build Status (nightly, all features)

Build Status (stable) Build Status (stable, all features)

Test Status Lint Status

Latest Version License:MIT Documentation Coverage Status

option_trait

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>
    • Run-time managed
    • Also implements Optional and PureMaybe
  • T and ()
    • Compile-time managed
    • Also implements PureStaticMaybe, PureMaybe and StaticMaybe
  • [T; 1] and [T; 0]
    • Compile-time managed
    • Can be managed using constant expressions, but with some difficulty
    • Also implements StaticMaybe
  • OptCell<T, _> (only if feature opt_cell is enabled)
    • Compile-time managed
    • Can be more easily managed using boolean constant expressions
    • Has const methods
    • Also implements StaticMaybe

Examples

This 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());
Commit count: 0

cargo fmt