[−][src]Crate edisp
Dispatch-on-collect for Rust enums.
This crate allows to dispatch enums yielded from an iterator, depending on their variants, with no runtime costs.
Note: This documentation describes what should be done, not the current state of the crate. Every feature documented here will be implemented prior first beta release.
Dispatching on std
enums
This crate provides dispatching for enums defined in std
. Values can be
collected in any type that implements both
Default
and
Extend
traits.
This dispatching consists in a trait generated for each enum, which can be
called on every Iterator
, like so:
use edisp::prelude::*; // Use your regular iterator let iter = vec![ Ok(42), Ok(0), Err("User not found"), Err("System error"), ].into_iter(); // Call the correct method, and that's all! let (some_successes, some_errors): (Vec<_>, Vec<_>) = iter.dispatch_result(); assert_eq!(some_successes, vec![42, 0]); assert_eq!(some_errors, vec!["User not found", "System error"]);
Dispatching on other crate's enums
Dispatching code is generated with either derive
macro or with declarative
macro. The first method allows to quickly generate boilerplate without
needing to write the enum name and variants twice. The second allows to get
rid of the procedural macro dependencies, syn
and quote
, and reduces
compilation time.
Values can then be collected in any type that implements both
Default
and
Extend
traits.
Using derive
macro
Note: This feature is not currently avalaible. It will be implemented before first beta release.
This crate provides a custom derive
macro allowing which automatically
implements traits required for dispatching, as shown in the following code
snippet:
use edisp::prelude::*; #[derive(Dispatch)] enum MyOwnEnum<T> { Character(char), Custom(T), } // Practical use-case: // First, create an iterator of `MyOwnEnum<&'static str>` let iter = vec![ MyOwnEnum::Character('λ'), MyOwnEnum::Custom("horse"), MyOwnEnum::Custom("manatee"), MyOwnEnum::Character('!'), ].into_iter(); // Then call it let (some_characters, some_strs): (Vec<_>, Vec<_>) = MyOwnEnum::dispatch(iter); // And it does what you expect! assert_eq!( some_characters, vec!['λ', '!'], ); assert_eq!( some_strs, vec!["horse", "manatee"], );
Note: This feature is not currently implemented, and as such can't be turned off.
The custom derive feature can be disabled by disabling derive
feature.
Using declarative macro
Note: CollectDispatch2
, CollectDispatch3
, and so on still have to be
merged toghether.
This crate provides a macro entitled implement_dispatch
. It allows to
generate traits required for dispatching. Everything wraps up like this:
use edisp::prelude::*; enum MyOwnEnum<T> { Character(char), Custom(T), } // Implements the required trait (in this case, CollectDispatch2) implement_dispatch!( MyOwnEnum<T>, Character(char), Custom(T), ); // Practical use-case: // First, create an iterator of `MyOwnEnum<&'static str>` let iter = vec![ MyOwnEnum::Character('λ'), MyOwnEnum::Custom("horse"), MyOwnEnum::Custom("manatee"), MyOwnEnum::Character('!'), ].into_iter(); // Then call it let (some_characters, some_strs): (Vec<_>, Vec<_>) = MyOwnEnum::dispatch(iter); // And it does what you expect! assert_eq!( some_characters, vec!['λ', '!'], ); assert_eq!( some_strs, vec!["horse", "manatee"], );
Modules
container | Defines the behavior of a container. |
dispatchers | Contains dispatchers for various kind of n-variants enums. |
prelude | The prelude file imports various |
Macros
create_n_collect_dispatcher | Generates a n-variants enum dispatcher trait. |
implement_dispatch | Implements the dispatch for an enum. |
implement_dispatcher_trait | Implements a given dispatcher trait for a given enum. |
Traits
CollectResult | Allows to collect values from an iterator by dispatching |