AnyOf : a versatile type for Rust
This project implements a flexible Algebraic Data Type : AnyOf.
AnyOf is an optional sum of product of two types,
which enables clear and safe data representations in functional and type-driven programming.
Overview
At the core of the project is the AnyOf enum, a general-purpose algebraic type,
alongside additional types such as Either and Both.
These abstractions allow to express dynamic states, optional values, and branching logic in a natural and explicit manner.
Key Types
-
AnyOf<L, R>- A flexible type that represents four possible states:
Neither: No value is present.Either::Left: Only the left value is present.Either::Right: Only the right value is present.Both: Both values are present.
- Conceptually, it combines variants in the following way:
AnyOf<L, R> = Neither | Either<L, R> | Both<L, R> - Its cases are:
AnyOf(L, R) = Neither | Either::Left(L) | Either::Right(R) | Both(L, R) - It can also be viewed as a product of two optional types:
AnyOf<L, R>::any = (Option<L>, Option<R>)- with:
Neither::any = (None, None) Either::Left(L)::any = (Some(L), None) Either::Right(R)::any = (None, Some(R)) Both::any = (Some(L), Some(R))
- with:
- A flexible type that represents four possible states:
-
Either<L, R>- A simple sum type representing one of two values.
- Variants:
Left(L)Right(R)
- Ideal for binary decision-making.
- Conceptually, it is the type :
Either<L, R> = Left(L) | Right(R)
-
Both<L, R>- A product type that pairs two values,
leftandright, of potentially different types. - Conceptually, it is the type:
Both<L, R> = (L, R)
- A product type that pairs two values,
-
Enhanced Type Composition
- Complex types like
AnyOf4,AnyOf8, andAnyOf16are implemented for handling larger, structured combinations via nestedAnyOfstructures. - The types implement the
LeftOrRight<L, R>trait.
- Complex types like
Features and Utilities
-
Methods inspired by Rust's
OptionandResulttypes:- Creation utilities:
new,new_left,new_both, etc. - State checks:
is_neither,is_left,is_both, etc. - Transformations:
map_left,map_right,swap, etc. - Unwrapping:
unwrap_left,unwrap_right,unwrap_both.
- Creation utilities:
-
Flexible combinations:
- Operators :
&to combineAnyOfvalues, or,|to filterAnyOfvalues, or,!to swapAnyOf,EitherandBothvalues.
- Default value handling and state manipulation methods.
- Operators :
Use Cases
AnyOf and its related types simplify dynamic state management and are well-suited for:
- Branching logic in functional programming.
- Handling optional or partial data.
- Implementing explicit and exhaustive handling of all potential states.
- Minimizing boilerplate for complex decision-making.
Motivation
The project aims to enrich Rust's type system with expressive and flexible types for representing data combinations and states.
- Unlike the Rust's
Resulttype, the typesEitherorAnyOfhas no error semantic, AnyOf<L, R>can be also be viewed as two options:(Option<L>, Option<R>)(a product of two optional types),
Status
The library is still under development, but it follows semantic versioning.
So the API will be stable in the same major version.
License
© 2025 Sébastien Geldreich
Distributed under the MIT License.