#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

#209 in Asynchronous

Download history 9/week @ 2024-09-16 4/week @ 2024-09-23 182/week @ 2024-10-07 21/week @ 2024-10-14 10/week @ 2024-10-21 104/week @ 2024-10-28 65/week @ 2024-11-04 189/week @ 2024-11-11 310/week @ 2024-11-18 876/week @ 2024-11-25 983/week @ 2024-12-02

2,359 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

~235–680KB
~16K SLoC