Skip to content

spastorino/dynosaur

Repository files navigation

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.

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages