Crate any_of

Source
Expand description

§any_of

any_of is a flexible and lightweight Rust crate designed to handle scenarios where a variable may have one out of several possible values, or none at all. It provides the AnyOf enum with variants to represent these possibilities.

§Features

  • Enum Variants:
    • Neither to represent the absence of values.
    • Either to represent a single value (Left or Right).
    • Both to represent a combination of values (Left and Right).
  • Convenient Methods: Create, transform, and check the state of AnyOf values easily with utility functions like new, is_neither, is_both, map_left, etc.
  • Extensible with Type Parameters: Allows customization of the Left and Right types.

§Example Usage

use any_of::{AnyOf, either::Either, both::Both};

let neither: AnyOf<i32, &str> = AnyOf::Neither;
let neither: AnyOf<i32, &str> = AnyOf::new(None, None);
let left: AnyOf<i32, &str> = AnyOf::Either(Either::Left(42));
let left: AnyOf<i32, &str> = AnyOf::new(Some(42), None);
let both: AnyOf<i32, &str> = AnyOf::Both(Both { left: 42, right: "Hello" });
let both: AnyOf<i32, &str> = AnyOf::new(Some(42), Some("Hello"));

assert!(neither.is_neither());
assert!(left.is_left());
assert!(both.is_both());

assert!(neither.map_right(|r| r).is_neither());
assert!(left.map_right(|r| r).is_neither());
assert!(left.map_left(|l| l).is_left());
assert!(both.map_left(|l| l).is_left());

§Use Cases

  • Representing optional or branching data in a concise manner.
  • Handling dynamic states with variants like Neither, Either, and Both.
  • Reducing boilerplate for mapping and transforming multiple optional values.

§Notable methods :

Re-exports§

pub use crate::both::Both;
pub use crate::either::Either;

Modules§

any_of_x
This module provides type definitions representing combinations of multiple possible types and their associated utility implementations.
both
This module defines the Both struct, a utility for pairing two values together.
either
This module provides the Either enum, a utility type that represents a value that can be one of two variants: Left or Right.

Enums§

AnyOf
Represents a type that can hold one of several variants: Neither, Either (with Left or Right), or Both (containing both Left and Right values).

Type Aliases§

Couple
The (T, U) tuple.
Pair
A shortcut for Couple<T, T>.