pub enum AnyOf<L, R = L> {
Neither,
Either(EitherOf<L, R>),
Both(BothOf<L, R>),
}Expand description
Represents a type that can hold one of several variants: Neither, Either (with Left or Right),
or Both (containing both Left and Right values).
This enum allows for representing a flexible combination of values, or the absence of any values.
§Variants
Neither: Represents the absence of bothLeftandRightvalues.Either: Represents a value that is eitherLeftorRight.Both: Represents a combination of bothLeftandRightvalues.
§Type Parameters
L: The type of theLeftvalue.R: The type of theRightvalue. Defaults to the same type asLif not specified.
§Examples
use any_of::{AnyOf, EitherOf, BothOf, LeftOrRight, Map, Right, Left, Neither, Either, Both};
let neither: AnyOf<i32, &str> = Neither;
let neither: AnyOf<i32, &str> = AnyOf::new(None, None);
let left: AnyOf<i32, &str> = Either(Left(42));
let left: AnyOf<i32, &str> = AnyOf::new(Some(42), None);
let both: AnyOf<i32, &str> = Both(BothOf { 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_left());
assert!(left.map_left(|l| l).is_left());
assert!(both.map_left(|l| l).is_both());
Variants§
Implementations§
Source§impl<LL, LR, RL, RR> AnyOf<AnyOf<LL, LR>, AnyOf<RL, RR>>
impl<LL, LR, RL, RR> AnyOf<AnyOf<LL, LR>, AnyOf<RL, RR>>
Sourcepub fn new4(
ll: Option<LL>,
lr: Option<LR>,
rl: Option<RL>,
rr: Option<RR>,
) -> Self
pub fn new4( ll: Option<LL>, lr: Option<LR>, rl: Option<RL>, rr: Option<RR>, ) -> Self
Creates a new AnyOf4 instance from four optional values.
The parameters correspond to the ll, lr, rl, and rr positions in
the nested AnyOf structure. This mirrors AnyOf::new.
§Examples
use any_of::{AnyOf4, AnyOf};
let value = AnyOf4::new4(Some(1), None::<i32>, None::<i32>, None::<i32>);
assert_eq!(value.ll(), Some(&1));Sourcepub fn opt4(&self) -> Opt4<&LL, &LR, &RL, &RR>
pub fn opt4(&self) -> Opt4<&LL, &LR, &RL, &RR>
Returns a tuple containing the four optional values represented by this AnyOf4.
This is useful when you need a simple (Option<LL>, Option<LR>, Option<RL>, Option<RR>)
representation instead of nested enums.
§Examples
use any_of::{AnyOf4, AnyOf, Opt4};
let any: AnyOf4<i32, i32, i32, i32> = AnyOf4::new_left(AnyOf::new_left(1));
assert_eq!(any.opt4(), (Some(&1), None, None, None));Source§impl<LLL, LLR, LRL, LRR, RLL, RLR, RRL, RRR> AnyOf<AnyOf<AnyOf<LLL, LLR>, AnyOf<LRL, LRR>>, AnyOf<AnyOf<RLL, RLR>, AnyOf<RRL, RRR>>>
impl<LLL, LLR, LRL, LRR, RLL, RLR, RRL, RRR> AnyOf<AnyOf<AnyOf<LLL, LLR>, AnyOf<LRL, LRR>>, AnyOf<AnyOf<RLL, RLR>, AnyOf<RRL, RRR>>>
Sourcepub fn new8(
lll: Option<LLL>,
llr: Option<LLR>,
lrl: Option<LRL>,
lrr: Option<LRR>,
rll: Option<RLL>,
rlr: Option<RLR>,
rrl: Option<RRL>,
rrr: Option<RRR>,
) -> Self
pub fn new8( lll: Option<LLL>, llr: Option<LLR>, lrl: Option<LRL>, lrr: Option<LRR>, rll: Option<RLL>, rlr: Option<RLR>, rrl: Option<RRL>, rrr: Option<RRR>, ) -> Self
Creates a new AnyOf8 from eight optional values.
§Examples
use any_of::AnyOf8;
let value = AnyOf8::new8(
Some(1),
None::<i32>, None::<i32>, None::<i32>,
None::<i32>, None::<i32>, None::<i32>, None::<i32>,
);
assert!(value.lll().is_some());Sourcepub fn opt8(&self) -> Opt8<&LLL, &LLR, &LRL, &LRR, &RLL, &RLR, &RRL, &RRR>
pub fn opt8(&self) -> Opt8<&LLL, &LLR, &LRL, &LRR, &RLL, &RLR, &RRL, &RRR>
Returns the eight optional values represented by this AnyOf8 as a tuple.
The returned Opt8 allows easier pattern matching and conversion into
other structures.
§Examples
use any_of::{AnyOf, AnyOf4, AnyOf8, Opt8};
let any: AnyOf8<i32, i32, i32, i32, i32, i32, i32, i32> =
AnyOf8::new_left(AnyOf4::new_left(AnyOf::new_left(1)));
let tuple = any.opt8();
assert_eq!(tuple.0, Some(&1));Source§impl<LLLL, LLLR, LLRL, LLRR, LRLL, LRLR, LRRL, LRRR, RLLL, RLLR, RLRL, RLRR, RRLL, RRLR, RRRL, RRRR> AnyOf<AnyOf<AnyOf<AnyOf<LLLL, LLLR>, AnyOf<LLRL, LLRR>>, AnyOf<AnyOf<LRLL, LRLR>, AnyOf<LRRL, LRRR>>>, AnyOf<AnyOf<AnyOf<RLLL, RLLR>, AnyOf<RLRL, RLRR>>, AnyOf<AnyOf<RRLL, RRLR>, AnyOf<RRRL, RRRR>>>>
impl<LLLL, LLLR, LLRL, LLRR, LRLL, LRLR, LRRL, LRRR, RLLL, RLLR, RLRL, RLRR, RRLL, RRLR, RRRL, RRRR> AnyOf<AnyOf<AnyOf<AnyOf<LLLL, LLLR>, AnyOf<LLRL, LLRR>>, AnyOf<AnyOf<LRLL, LRLR>, AnyOf<LRRL, LRRR>>>, AnyOf<AnyOf<AnyOf<RLLL, RLLR>, AnyOf<RLRL, RLRR>>, AnyOf<AnyOf<RRLL, RRLR>, AnyOf<RRRL, RRRR>>>>
Sourcepub fn new16(
llll: Option<LLLL>,
lllr: Option<LLLR>,
llrl: Option<LLRL>,
llrr: Option<LLRR>,
lrll: Option<LRLL>,
lrlr: Option<LRLR>,
lrrl: Option<LRRL>,
lrrr: Option<LRRR>,
rlll: Option<RLLL>,
rllr: Option<RLLR>,
rlrl: Option<RLRL>,
rlrr: Option<RLRR>,
rrll: Option<RRLL>,
rrlr: Option<RRLR>,
rrrl: Option<RRRL>,
rrrr: Option<RRRR>,
) -> Self
pub fn new16( llll: Option<LLLL>, lllr: Option<LLLR>, llrl: Option<LLRL>, llrr: Option<LLRR>, lrll: Option<LRLL>, lrlr: Option<LRLR>, lrrl: Option<LRRL>, lrrr: Option<LRRR>, rlll: Option<RLLL>, rllr: Option<RLLR>, rlrl: Option<RLRL>, rlrr: Option<RLRR>, rrll: Option<RRLL>, rrlr: Option<RRLR>, rrrl: Option<RRRL>, rrrr: Option<RRRR>, ) -> Self
Creates a new AnyOf16 from sixteen optional values.
§Examples
use any_of::AnyOf16;
let value = AnyOf16::new16(
Some(1), None::<i32>, None::<i32>, None::<i32>,
None::<i32>, None::<i32>, None::<i32>, None::<i32>,
None::<i32>, None::<i32>, None::<i32>, None::<i32>,
None::<i32>, None::<i32>, None::<i32>, None::<i32>,
);
assert!(value.llll().is_some());Sourcepub fn from_opt16(
opt: Opt16<LLLL, LLLR, LLRL, LLRR, LRLL, LRLR, LRRL, LRRR, RLLL, RLLR, RLRL, RLRR, RRLL, RRLR, RRRL, RRRR>,
) -> Self
pub fn from_opt16( opt: Opt16<LLLL, LLLR, LLRL, LLRR, LRLL, LRLR, LRRL, LRRR, RLLL, RLLR, RLRL, RLRR, RRLL, RRLR, RRRL, RRRR>, ) -> Self
Builds an AnyOf16 from an Opt16 tuple.
§Examples
use any_of::{AnyOf16, Opt16};
let tuple: Opt16<i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32,
i32, i32, i32, i32, i32> =
(Some(1), None, None, None, None, None, None, None,
None, None, None, None, None, None, None, None);
let value = AnyOf16::from_opt16(tuple);
assert_eq!(value.llll(), Some(&1));Sourcepub fn opt16(
&self,
) -> Opt16<&LLLL, &LLLR, &LLRL, &LLRR, &LRLL, &LRLR, &LRRL, &LRRR, &RLLL, &RLLR, &RLRL, &RLRR, &RRLL, &RRLR, &RRRL, &RRRR>
pub fn opt16( &self, ) -> Opt16<&LLLL, &LLLR, &LLRL, &LLRR, &LRLL, &LRLR, &LRRL, &LRRR, &RLLL, &RLLR, &RLRL, &RLRR, &RRLL, &RRLR, &RRRL, &RRRR>
Returns all sixteen optional values represented by this AnyOf16.
This method provides a direct mapping to the Opt16 type for easier
inspection or conversion.
§Examples
use any_of::{AnyOf, AnyOf4, AnyOf8, AnyOf16, Opt16};
let any: AnyOf16<i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32, i32> =
AnyOf16::new_left(AnyOf8::new_left(AnyOf4::new_left(AnyOf::new_left(1))));
let tuple = any.opt16();
assert_eq!(tuple.0, Some(&1));Source§impl<L, R> AnyOf<L, R>
impl<L, R> AnyOf<L, R>
Sourcepub fn new(left: Option<L>, right: Option<R>) -> Self
pub fn new(left: Option<L>, right: Option<R>) -> Self
Creates a new AnyOf instance based on the presence of left and right values.
§Parameters
left: AnOptioncontaining the left value of typeL, orNoneif absent.right: AnOptioncontaining the right value of typeR, orNoneif absent.
§Returns
- Returns
Neitherif bothleftandrightareNone. - Returns
Either(Left)iflefthas a value andrightisNone. - Returns
Either(Right)ifrighthas a value andleftisNone. - Returns
Bothif bothleftandrighthave values.
§Examples
use any_of::{AnyOf, Either, Both, LeftOrRight};
let neither: AnyOf<i32, &str> = AnyOf::new(None, None);
assert!(neither.is_neither());
let left: AnyOf<i32, &str> = AnyOf::new(Some(42), None);
assert!(left.is_left());
let right: AnyOf<i32, &str> = AnyOf::new(None, Some("Hello"));
assert!(right.is_right());
let both: AnyOf<i32, &str> = AnyOf::new(Some(42), Some("Hello"));
assert!(both.is_both());Sourcepub fn new_neither() -> Self
pub fn new_neither() -> Self
Creates an Neither variant.
§Examples
use any_of::AnyOf;
let neither: AnyOf<i32, &str> = AnyOf::new_neither();
assert!(neither.is_neither());Sourcepub fn new_left(left: L) -> Self
pub fn new_left(left: L) -> Self
Creates an Either variant containing a Left value.
§Examples
use any_of::{AnyOf, LeftOrRight};
let left: AnyOf<i32, &str> = AnyOf::new_left(42);
assert!(left.is_left());Sourcepub fn new_right(right: R) -> Self
pub fn new_right(right: R) -> Self
Creates an Either variant containing a Right value.
§Examples
use any_of::{AnyOf, LeftOrRight};
let right: AnyOf<i32, &str> = AnyOf::new_right("Hello");
assert!(right.is_right());Sourcepub fn new_both(l: L, r: R) -> Self
pub fn new_both(l: L, r: R) -> Self
Creates an Both variant containing both a Left and Right value.
§Examples
use any_of::AnyOf;
let both: AnyOf<i32, &str> = AnyOf::new_both(42, "Hello");
assert!(both.is_both());Sourcepub fn from_both(both: BothOf<L, R>) -> Self
pub fn from_both(both: BothOf<L, R>) -> Self
Creates an Both variant from a Both struct.
§Examples
use any_of::{AnyOf, BothOf};
let both_struct = BothOf { left: 42, right: "Hello" };
let both = AnyOf::from_both(both_struct);
assert!(both.is_both());Sourcepub fn from_opt2(any: Opt2<L, R>) -> Self
pub fn from_opt2(any: Opt2<L, R>) -> Self
Creates a new AnyOf instance based on the presence of left and right values.
See Self::new.
Sourcepub fn from_either(either: EitherOf<L, R>) -> Self
pub fn from_either(either: EitherOf<L, R>) -> Self
Creates an AnyOf variant from an Either struct.
§Examples
use any_of::{AnyOf, EitherOf, LeftOrRight};
use any_of::either::EitherOf::{Right, Left};
let either: EitherOf<i32, ()> = Left(42);
let any_of: AnyOf<i32, ()> = AnyOf::from_either(either);
assert!(any_of.is_left());Sourcepub fn into_either(self) -> EitherOf<L, R>
pub fn into_either(self) -> EitherOf<L, R>
Sourcepub fn to_either_pair(&self) -> Pair<Option<EitherOf<L, R>>>
pub fn to_either_pair(&self) -> Pair<Option<EitherOf<L, R>>>
Converts the current AnyOf variant into a pair of options containing Either variants.
§Returns
(Some(Left), None)forEither(Left).(None, Some(Right))forEither(Right).(Some(Left), Some(Right))forBoth.(None, None)forNeither.
§Examples
use any_of::{AnyOf, Either, Right, Left};
let any_of = AnyOf::new_both(42, "Hello");
let (left, right) = any_of.to_either_pair();
assert_eq!(left, Some(Left(42)));
assert_eq!(right, Some(Right("Hello")));Sourcepub fn is_neither(&self) -> bool
pub fn is_neither(&self) -> bool
True if Neither
Sourcepub fn is_neither_or_both(&self) -> bool
pub fn is_neither_or_both(&self) -> bool
True if not Either
Sourcepub fn both_or_none(&self) -> Option<Couple<&L, &R>>
pub fn both_or_none(&self) -> Option<Couple<&L, &R>>
Returns Some((&L, &R)) if self.is_both() is true, or None.
Sourcepub fn both_or_else(self, f: impl FnOnce() -> BothOf<L, R>) -> BothOf<L, R>
pub fn both_or_else(self, f: impl FnOnce() -> BothOf<L, R>) -> BothOf<L, R>
Returns both values if present, or computes them with the provided function.
Sourcepub fn both_or(self, other: BothOf<L, R>) -> BothOf<L, R>
pub fn both_or(self, other: BothOf<L, R>) -> BothOf<L, R>
Returns both values if present, or the provided default values.
Sourcepub fn unwrap_both(self) -> BothOf<L, R>
pub fn unwrap_both(self) -> BothOf<L, R>
Unwraps and returns both values, panicking if not available.
Sourcepub fn filter_left(self) -> Self
pub fn filter_left(self) -> Self
Filters to keep only the left value if present, otherwise returns Neither.
Sourcepub fn filter_right(self) -> Self
pub fn filter_right(self) -> Self
Filters to keep only the right value if present, otherwise returns Neither.
Sourcepub fn with_right(self, right: R) -> Self
pub fn with_right(self, right: R) -> Self
Adds or replaces the right value, keeping or constructing the instance accordingly.
Sourcepub fn with_left(self, left: L) -> Self
pub fn with_left(self, left: L) -> Self
Adds or replaces the left value, keeping or constructing the instance accordingly.
Sourcepub fn combine(self, other: Self) -> Self
pub fn combine(self, other: Self) -> Self
Combines (+ operator) two Either values into a single one.
§General rules
Neitheris always substituted by the other operand,Both:- as left operand : substitutes the other operand,
- as right operand : completes the other operand,
LeftorRight:L + RorR + Lcombines to an instance ofBoth,L + lorr + Rselects the operand placed on the correct side of the operator :- leftLeft + rightLeft = leftLeft
- leftRight + rightRight = rightRight
§All cases
- Neither cases :
- Neither + Neither = Neither
- Neither + other = other
- self + Neither = self
- Trivial cases :
- Left(x) + Left(y) = Left(x)
- Right(x) + Right(y) = Right(y)
- Both(x, y) + other = Both(x, y)
- Combined cases :
- Left(x) + Right(y) = Both(x, y)
- Right(x) + Left(y) = Both(y, x)
- Left(x) + Both(_, y) = Both(x, y)
- Right(x) + Both(y, _) = Both(y, x)
Sourcepub fn filter(self, other: Self) -> Self
pub fn filter(self, other: Self) -> Self
Filters (- operator) the current AnyOf instance using another AnyOf instance.
§General rules
The filtering behavior depends on the specific variants of self and other:
other == Neitherfilters nothing (self is returned),other == Bothfilters all (Neitheris returned),LeftandRightasotherfilters the corresponding side ofself.
§All cases
- Neither cases:
Neither - other = NeitherLeft(x) - Left(y) = NeitherRight(x) - Right(y) = Neitherother - Both(x, y) = Neither
- Trivial case:
other - Neither = other
- Filtered cases:
Left(x) - Right(y) = Left(x)Right(x) - Left(y) = Right(x)Both(x, y) - Right(z) = Left(x)Both(x, y) - Left(z) = Right(y)
§Examples
use any_of::{AnyOf, Both ,BothOf, Either, Left, Neither, Right};
let both = Both(BothOf { left: 5, right: 10 });
let left_only = Either(Left(5));
let right_only = Either(Right(10));
let neither: AnyOf<i32, i32> = Neither;
// Filtering Both with Right results in Left
assert_eq!(both - right_only, left_only);
// Filtering Both with Left results in Right
assert_eq!(both - left_only, right_only);
// Filtering with Neither doesn't affect the original value
assert_eq!(both - neither, both);
assert_eq!(left_only - neither, left_only);
assert_eq!(right_only - neither, right_only);
// Filtering with Both always results in Neither
assert_eq!(both - both, neither);
assert_eq!(left_only - both, neither);
assert_eq!(right_only - both, neither);Trait Implementations§
Source§impl<L, R> LeftOrRight<L, R> for AnyOf<L, R>
impl<L, R> LeftOrRight<L, R> for AnyOf<L, R>
Source§impl<L, R> Map<L, R> for AnyOf<L, R>
impl<L, R> Map<L, R> for AnyOf<L, R>
Source§impl<L, R> Not for AnyOf<L, R>
impl<L, R> Not for AnyOf<L, R>
Source§fn not(self) -> Self::Output
fn not(self) -> Self::Output
Swaps (! operator) the left and right components, creating a new AnyOf with reversed types.
§Returns
A new AnyOf<R, L> instance where the left and right components have been swapped.
- If
selfisNeither, the result will also beNeither. - If
selfis aLeft, the result will contain the value as aRight. - If
selfis aRight, the result will contain the value as aLeft. - If
selfis aBoth, the left and right values are swapped in the result.
Source§impl<L, R, FL, FR, L2, R2> Shr<BothOf<FL, FR>> for AnyOf<L, R>
impl<L, R, FL, FR, L2, R2> Shr<BothOf<FL, FR>> for AnyOf<L, R>
Source§fn shr(self, rhs: BothOf<FL, FR>) -> Self::Output
fn shr(self, rhs: BothOf<FL, FR>) -> Self::Output
Maps two functions with the parts of this AnyOf
§Examples
use any_of::{AnyOf, Left, Unwrap};
let left_fn = |x: i32| x * 2;
let right_fn = |y: i32| y + 3;
let both_fn = (left_fn, right_fn).into();
let both:AnyOf<i32, i32> = (5, 10).into();
let result = both >> both_fn;
assert_eq!(result, (10, 13).into());
let left = AnyOf::Either(Left(25));
let left_result = left >> both_fn;
assert_eq!(left_result.unwrap_left(), 50);Source§impl<L, R> Unwrap<L, R> for AnyOf<L, R>
impl<L, R> Unwrap<L, R> for AnyOf<L, R>
Source§fn left_or_else(self, f: impl FnOnce() -> L) -> L
fn left_or_else(self, f: impl FnOnce() -> L) -> L
Returns the left value if present, or computes it with the provided function.
Source§fn right_or_else(self, f: impl FnOnce() -> R) -> R
fn right_or_else(self, f: impl FnOnce() -> R) -> R
Returns the right value if present, or computes it with the provided function.
Source§fn left_or(self, l: L) -> Lwhere
Self: Sized,
fn left_or(self, l: L) -> Lwhere
Self: Sized,
Source§fn right_or(self, r: R) -> Rwhere
Self: Sized,
fn right_or(self, r: R) -> Rwhere
Self: Sized,
Source§fn left_or_default(self) -> L
fn left_or_default(self) -> L
Source§fn right_or_default(self) -> R
fn right_or_default(self) -> R
Source§fn expect_left(self, msg: &str) -> Lwhere
Self: Sized,
fn expect_left(self, msg: &str) -> Lwhere
Self: Sized,
Right. Read moreSource§fn expect_right(self, msg: &str) -> Rwhere
Self: Sized,
fn expect_right(self, msg: &str) -> Rwhere
Self: Sized,
Left. Read moreSource§fn unwrap_left(self) -> Lwhere
Self: Sized,
fn unwrap_left(self) -> Lwhere
Self: Sized,
Right. Read moreSource§fn unwrap_right(self) -> Rwhere
Self: Sized,
fn unwrap_right(self) -> Rwhere
Self: Sized,
Left. Read more