disponent
disponent is an alternative to using dyn Trait trait objects for dispatching to multiple implementations of a trait. Works with async methods, generics, #[cfg] attributes, no_std and even traits that are not object safe.
Usage
use declare;
declare!;
;
;
Configuration Options
Apply #[disponent::configure(...)] to the enum with any combination of:
inherent: Generate inherent methods on the enum (vs trait impl)inline: Add#[inline]to all generated methodsfrom: GenerateFromimpls for each varianttry_into: GenerateTryIntoimpls for each variant
Remote Traits
Use #[disponent::remote(path::to::Trait)] on the trait to forward to a trait defined elsewhere:
use ExternalTrait;
declare!;
Generated Code
The above example generates the following code:
In many cases, this can be substantially faster than using dyn Trait trait objects, especially when the enum is small and the methods are simple. See the benchmarks of enum_dispatch for more details (benchmarks for disponent are coming soon).
See also
ambassadordeclarative_enum_dispatchdelegatedelegationenum_delegateenum_dispatchpolymorphic_enumspire_enum
With the exception of declarative_enum_dispatch, these crates all share state between macro invocations, which can lead to issues with rust-analyzer and similar tools. disponent avoids this by generating all code in a single macro invocation, at the cost of some flexibility in how the traits are defined (all while rust-fmt and error reporting works like normal).
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in disponent by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.