AnyOf<L, R> : Neither | Either<L, R> | Both<L, R>

86 points OkResponsibility9677 27 comments

My first crate mature enough to talk about:
any_of.

🔗 crates io
🔗 github

ℹ️ This library allows you to use the AnyOf type, which is a sum type of a product type of two types.

ℹ️ It enables you to represent anything in a type-safe manner. It is an algebraic data type (on Wikipedia).

✏️ Formally, it can be written as:
AnyOf<L, R> = Neither | Either<L, R> | Both<L, R>

✏️ The Either and Both types allow different combinations of types:
Either<L, R> = Left(L) | Right(R)
Both<L, R> = (L, R)

✏️ The traits LeftOrRight, Unwrap, Map, and Swap provide extensibility to the library.

The type diagram:

https://preview.redd.it/3fz3wkyl2uhe1.png?width=1407&format=png&auto=webp&s=2920314340cf98c967f59e80156254d3253c6efd