Crate any_of

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, Both , BothOf, Either , Left, LeftOrRight, Map, Neither};

let neither: AnyOf<i32, &str> = AnyOf::new(None, None);
let neither: AnyOf<i32, &str> = Neither;

let left: AnyOf<i32, &str> = AnyOf::new(Some(42), None);
let left: AnyOf<i32, &str> = Either(Left(42));

let both: AnyOf<i32, &str> = AnyOf::new(Some(42), Some("Hello"));
let both: AnyOf<i32, &str> = Both(BothOf { left: 42, right: "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_left());
assert!(left.map_left(|l| l).is_left());
assert!(both.map_left(|l| l).is_both());

§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 :

§Exported elements :

Re-exports§

pub use crate::both::BothOf;
pub use crate::concepts::Couple;
pub use crate::concepts::LeftOrRight;
pub use crate::concepts::Map;
pub use crate::concepts::Opt16;
pub use crate::concepts::Opt2;
pub use crate::concepts::Opt4;
pub use crate::concepts::Opt8;
pub use crate::concepts::Pair;
pub use crate::concepts::Swap;
pub use crate::concepts::Unwrap;
pub use crate::either::EitherOf;
pub use crate::either::EitherOf::Left;
pub use crate::either::EitherOf::Right;
pub use crate::AnyOf::Both;
pub use crate::AnyOf::Either;
pub use crate::AnyOf::Neither;

Modules§

both
This module defines the BothOf struct, a utility for pairing two values together.
concepts
This module defines utility types and traits for working with dual-variant data structures, such as “product types” (tuples) and “sum types” (LeftOrRight) that can hold one of two possible variants.
either
This module provides the EitherOf 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§

AnyOf4
A type representing a combination of four possible types.
AnyOf8
A type representing a combination of eight possible types.
AnyOf16
A type representing a combination of sixteen possible types.