Expand description

Rust enums are coproducts but the datastructure provided in this library allows writing functions that operate on generic coproducts.

For instance, the below function takes any coproduct that may contain a cat.

fn is_cat<C, I>(maybe_cat: C) -> bool
where
    C: coproduct::At<I, Cat>,
{
    maybe_cat.uninject().is_ok()
}

The coproducts take as much memory as the largest variant and 32 bits for the tag, which is pretty close to optimal. They do not benefit from Rust’s enum layout optimizations, but the whole reason for this crate is that those optimizations aren’t perfect. Implementing a coproduct as nested enums akin to a purely functional list results in extremely high memory use. (Tested in Rust 1.66)

Another benefit is that the implementation of some functions is a lot simpler when there is no need to pretend that a nested structure is traversed. The downside is that unlike the coproduct provided by frunk, this library uses unsafe.

Macros

Builds a Coproduct that can hold the types given as arguments.
Builds a CopyableCoproduct that can hold the types given as arguments.

Structs

Can hold any type. You should use CopyableCoproduct if your types are copyable.
A coproduct that can only hold copyable types.

Enums

Traits

Implemented on Coproducts that Source can be embedded into.
Trait for properly deallocating Unions that are not Copy.
This trait is implemented for Unions where variant I has type X.

Functions

Create a coproduct containing X. This standalone function more convenient than the method or trait when writing very abstracted code.

Unions