Skip to main content

maybe_async_trait

Macro maybe_async_trait 

Source
macro_rules! maybe_async_trait {
    ($item:item) => { ... };
}
Expand description

Apply async_trait with the target-correct Send-ness, in one line instead of the two-attribute cfg_attr dance.

Every async trait definition and impl in pipecrab needs the same pair — plain #[async_trait] on native (its boxed futures are Send, matching the MaybeSend/MaybeSendSync bounds), and #[async_trait(?Send)] on wasm32 (where they can’t be). Writing both by hand is easy to get subtly wrong (swap the two cfgs and native silently loses Send). Wrap the item instead — the trait definition or the impl block:

use pipecrab_runtime::{maybe_async_trait, MaybeSend};

maybe_async_trait! {
    pub trait Widget: MaybeSend {
        async fn poll(&mut self) -> u32;
    }
}

struct Zero;
maybe_async_trait! {
    impl Widget for Zero {
        async fn poll(&mut self) -> u32 { 0 }
    }
}

The macro pulls in async_trait through this crate, so a stage or interface crate that uses it needs only a pipecrab-runtime dependency, not a direct one on async-trait.