| Crates.io | any_of |
| lib.rs | any_of |
| version | 2.2.0 |
| created_at | 2025-02-06 02:03:57.287307+00 |
| updated_at | 2025-07-11 02:33:56.898647+00 |
| description | A general optional sum of product type which can be Neither, Left, Right or Both. |
| homepage | |
| repository | https://github.com/Trehinos/any_of |
| max_upload_size | |
| id | 1544942 |
| size | 423,262 |
This project implements a flexible Algebraic Data Type : AnyOf.
AnyOf is an optional sum product of two types,
which enables clear and safe data representations in functional and type-driven programming.
At the core of the project is the AnyOf enum, a general-purpose algebraic type,
alongside additional types as EitherOf and BothOf.
These abstractions allow to express dynamic states, optional values, and branching logic in a natural and explicit manner.
AnyOf<L, R>
Neither: No value is present.Either:
Left: Only the left value is present.Right: Only the right value is present.Both: Both values are present.AnyOf<L, R> = Neither
| Either(EitherOf<L, R>)
| Both(BothOf<L, R>)
AnyOf(l: L, r: R) = Neither
| Either(Left(l))
| Either(Right(r))
| Both(BothOf{left = l, right = r})
AnyOf<L, R>::opt2() -> (Option<L>, Option<R>)
Cases :
Neither.opt2() returns (None, None)Left(L).opt2() returns (Some(L), None)Right(R).opt2() returns (None, Some(R))Both(L, R).opt2() returns (Some(L), Some(R))EitherOf<L, R>
Left(L)Right(R)opt2() too :
Left(L).opt2() returns (Some(L), None)Right(R).opt2() returns (None, Some(R))EitherOf<L, R> = Left(L) | Right(R)
BothOf<L, R>
left and right, of potentially different types.opt2() too :
BothOf{ left: L, right: R }.opt2() returns (Some(L), Some(R))BothOf<L, R> = {left: L, right: R}
into_couple(self) -> Couple<L, R>,from_couple(Couple<L, R>) -> Self,Enhanced Type Composition
Couple<T, U> is a (T, U),Pair<T> is a Couple<T, T>,Opt2<T, U> is a Couple<Option<T>, Option<U>>,AnyOf4, AnyOf8, and AnyOf16 are implemented for handling larger,
structured combinations via nested AnyOf structures.LeftOrRight trait :
is_right(), is_left(), opt2(), left() and right().AnyOf, EitherOf and BothOf,Unwrap<L, R>, Swap<L, R> and Map<L, R>.Methods inspired by Rust's Option and Result types:
new, new_left, new_both, etc.is_neither, is_left, is_both, etc.map_left, map_right, swap, etc.unwrap_left, unwrap_right, unwrap_both.Flexible combinations:
+ to combine AnyOf values, or,- to filter AnyOf values, or,! to swap AnyOf, EitherOf and BothOf values,>> to map AnyOf, EitherOf and BothOf values,
AnyOf and its related types simplify dynamic state management and are well-suited for:
The project aims to enrich Rust's type system with expressive and flexible types
for representing data combinations and states.
It is inspired by the Haskel's Either type.
Result type, the types AnyOf, EitherOf or LeftOrRight have not an "error" semantic, they are
general purpose,LeftOrRight<L, R>::opt2() returns a (Option<L>, Option<R>) which is a product of two optional
types, but the two types have different composition conciseness :
AnyOf<AnyOf<LL, LR>, AnyOf<RL, RR>>
vs
(Option<(Option<LL>, Option<LR>)>, Option<(Option<RL>, Option<RR>)>)
AnyOf<AnyOf<AnyOf<LLL, LLR>, AnyOf<LRL, LRR>>, AnyOf<AnyOf<RLL, RLR>, AnyOf<RRL, RRR>>>
vs
(Option<(Option<(Option<LLL>, Option<LLR>)>, Option<(Option<LRL>, Option<LRR>)>)>, Option<(Option<(Option<RLL>, Option<RLR>)>, Option<(Option<RRL>, Option<RRR>)>)>)
The library may evolve following semantic versioning, so the API will be stable in a given major version.
© 2025 Sébastien Geldreich
Distributed under the MIT License.