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:
Neitherto represent the absence of values.Eitherto represent a single value (LeftorRight).Bothto represent a combination of values (LeftandRight).
- Convenient Methods: Create, transform, and check the state of
AnyOfvalues easily with utility functions likenew,is_neither,is_both,map_left, etc. - Extensible with Type Parameters: Allows customization of the
LeftandRighttypes.
§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, andBoth. - Reducing boilerplate for mapping and transforming multiple optional values.
§Notable methods :
- AnyOf::new
- AnyOf::combine
- AnyOf::filter
- AnyOf::map
- AnyOf::swap
- AnyOf::unwrap_left and AnyOf::left
- AnyOf::unwrap_right and AnyOf::right
- AnyOf::unwrap_both and AnyOf::both_or_none
§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
BothOfstruct, 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
EitherOfenum, a utility type that represents a value that can be one of two variants:LeftorRight.
Enums§
- AnyOf
- Represents a type that can hold one of several variants:
Neither,Either(withLeftorRight), orBoth(containing bothLeftandRightvalues).