trait-union
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
use trait_union;
use Display;
trait_union!
let mut container = new;
assert_eq!;
container = new;
assert_eq!;
container = new;
assert_eq!;
Implementation
The generated type looks roughly as follows:
,
vtable: *mut ,
}
Its size is therefore similar to the size of an enum
with one variant per implementor.
Depending on the number of implementors, compile times should be significantly lower than
with an enum
. The run-time performance is similar to that of Box<dyn Trait>
.
License
This project is licensed under either of
- Apache License, Version 2.0
- MIT License
at your option.