#async-trait #traits #impl #async #dynamic-dispatch

no-std dynosaur

Dynamic dispatch for return position impl traits and async in Rust

3 releases

0.1.2 Nov 19, 2024
0.1.1 Oct 10, 2024
0.1.0 Jul 12, 2024

#207 in Asynchronous

Download history 8/week @ 2024-09-12 4/week @ 2024-09-19 1/week @ 2024-09-26 196/week @ 2024-10-10 11/week @ 2024-10-17 47/week @ 2024-10-24 96/week @ 2024-10-31 83/week @ 2024-11-07 353/week @ 2024-11-14 492/week @ 2024-11-21 983/week @ 2024-11-28 1242/week @ 2024-12-05

3,156 downloads per month
Used in zcash_client_backend

MIT/Apache

17KB
72 lines

Latest Version Documentation GHA Status License

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(&mut 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

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 this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~255–710KB
~17K SLoC