6 releases (breaking)

0.7.0 Jun 18, 2022
0.6.1 Jun 18, 2022
0.6.0 Jan 29, 2022
0.4.0 Nov 11, 2021
0.2.0 Nov 9, 2021

#17 in #dynamic-dispatch

Download history 1/week @ 2023-12-18 2/week @ 2023-12-25 10/week @ 2024-02-12 21/week @ 2024-02-19 17/week @ 2024-02-26 15/week @ 2024-03-04 14/week @ 2024-03-11 10/week @ 2024-03-18 24/week @ 2024-03-25

65 downloads per month
Used in 2 crates (via async_t)

MIT license

24KB
551 lines

async_t

This library allows for zero-cost compile-time async-traits. This library needs nightly and features generic_associated_types and type_alias_impl_trait to be enabled. Compiling in stable will automatically use dtolnay's async_trait instead.

It supports everything a normal trait would except:

  • default async methods
  • blanket implementations
  • dynamic dispatch

It can also have problems with lifetimes where they have to be specified.

// spawn example

#[async_trait]
trait Spawn {
    // supports self, &self, &mut self and no self
    async fn spawn() -> JoinHandle<()>;
}

#[async_trait]
impl Spawn for Spawner {
    async fn spawn() -> JoinHandle<()> {
        task::spawn(async {
            // ...
        })
    }
}

async fn spawn<T: Spawn>() -> JoinHandle<()> {
    T::spawn().await
}

#[async_trait]
trait Sleeper {
    #[unsend]
    async fn sleep<T>(t: T) -> T;
}

#[async_trait]
impl Sleeper for () {
    #[unsend]
    async fn sleep<T>(t: T) -> T {
        task::sleep(Duration::from_secs(2)).await;
        t
    }
}

Dependencies

~1.5MB
~35K SLoC