Enum exchange
Suppose Enum!(A,B,C,..)
denotes a type similar to enum
that composed of
variants A,B,C.., with extra features:
-
Variants of the duplicated type are merged. For instance,
Enum!(A,B,A)
isEnum!(A,B)
. -
Order of variants does not matter. For instance,
Enum!(A,B)
isEnum!(B,A)
. -
Enum!()
s as variants are flattened. For instance,Enum!(A, Enum!(B,C))
isEnum!(A,B,C)
. -
Any subset of an
Enum!()
can be converted to it. For instance,A
,Enum!(A)
andEnum!(A,B)
can be converted toEnum!(A,B,C)
.
Such types, which are similar to Racket's "union types", do not exist in Rust's type systems. With the help of this library, the best we can get is "union values":
-
Enum!()
s that has duplicated variant types cannot be converted to each other without extra annotation, which is not practicable. -
Two
Enum!()
s composed of the same variant types but with different order can be converted to each other. For instance,Enum!(A,B)
can be converted toEnum!(B,A)
, and vise vesa. -
Enum!()
s as variants are not flattened in conversion. This library might support convertingEnum!(A,C)
toEnum!(A, Enum!(B,C))
in the future( perhaps if Rust supportswhere T != U
), but not now. -
Any subset of an
Enum!()
can be converted to it.
This library names the conversion in #2 and #4 as "enum exchange", and defines
an derivable Exchange
trait.