#async-trait #traits #async #impl

macro no-std dynosaur_derive

Dynamic dispatch for return position impl traits and async in Rust

4 releases

0.2.0 Mar 20, 2025
0.1.3 Feb 10, 2025
0.1.2 Nov 19, 2024
0.1.1 Oct 10, 2024

#55 in #impl

Download history 1201/week @ 2024-12-07 1177/week @ 2024-12-14 345/week @ 2024-12-21 1578/week @ 2024-12-28 5259/week @ 2025-01-04 5178/week @ 2025-01-11 5143/week @ 2025-01-18 6328/week @ 2025-01-25 6390/week @ 2025-02-01 7080/week @ 2025-02-08 6599/week @ 2025-02-15 6302/week @ 2025-02-22 8410/week @ 2025-03-01 7788/week @ 2025-03-08 7961/week @ 2025-03-15 6087/week @ 2025-03-22

31,247 downloads per month
Used in 9 crates (via dynosaur)

MIT/Apache

38KB
857 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) -> Option<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

~215–650KB
~15K SLoC