trait-union

crates.io docs.rs

This crate provides a macro that generates a trait-union type. That is, a trait object type which can contain any one of a pre-determined set of implementors.

The generated type does not allocate. The size of the type is the size of the largest variant plus some constant overhead.

NOTE: As of rustc 1.47, you must enable the untagged_unions feature to store non-Copy types in a trait-union. This will change soon.

Example

```rust use traitunion::traitunion; use std::fmt::Display; use std::mem; use std::ops::Deref;

trait_union! { /// Container can contain either an i32, a &'static str, or a bool. union Container: Display = i32 | &'static str | bool; }

let mut container = Container::new(32); asserteq!(container.tostring(), "32");

container = Container::new("Hello World"); asserteq!(container.tostring(), "Hello World");

container = Container::new(true); asserteq!(container.tostring(), "true"); ```

License

This project is licensed under either of

at your option.