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, 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, andBoth. - Reducing boilerplate for mapping and transforming multiple optional values.
§Notable methods :
Re-exports§
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
Bothstruct, a utility for pairing two values together. - either
- This module provides the
Eitherenum, 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).