2 releases
0.1.1 | Oct 10, 2024 |
---|---|
0.1.0 | Jul 12, 2024 |
#233 in Asynchronous
203 downloads per month
12KB
66 lines
dynosaur lets you use dynamic dispatch on traits with async fn
and
methods returning impl Trait
.
#[dynosaur::dynosaur(DynNext)]
trait Next {
type Item;
async fn next(&self) -> Self::Item;
}
The macro above generates a type called DynNext
which can be used like this:
async fn dyn_dispatch(iter: &mut DynNext<'_, i32>) {
while let Some(item) = iter.next().await {
println!("- {item}");
}
}
let a = [1, 2, 3];
dyn_dispatch(DynNext::from_mut(&mut a.into_iter())).await;
The general rule is that anywhere you would write dyn Trait
(which would
result in a compiler error), you instead write DynTrait
.
Methods returning impl Trait
box their return types when dispatched
dynamically, but not when dispatched statically.
License and usage notes
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Dependencies
~240–690KB
~16K SLoC