Skip to main content

future_form

Attribute Macro future_form 

Source
#[future_form]
Expand description

Generate implementations of a trait for Sendable and/or Local FutureForms.

This attribute macro allows you to write a single implementation that works for both Send and !Send futures, avoiding code duplication.

§Usage

// Generate both Sendable and Local impls
#[future_form(Sendable, Local)]
impl<F: FutureForm> MyTrait<F> for MyType<F> { ... }

// Generate only Sendable impl
#[future_form(Sendable)]
impl<F: FutureForm> MyTrait<F> for MyType<F> { ... }

// Generate only Local impl
#[future_form(Local)]
impl<F: FutureForm> MyTrait<F> for MyType<F> { ... }

// Add bounds only for specific variants
#[future_form(Sendable where T: Send, Local)]
impl<F: FutureForm, T: Clone> MyTrait<F> for Container<T> { ... }
// Generates: impl<T: Clone + Send> MyTrait<Sendable> for Container<T>
//            impl<T: Clone> MyTrait<Local> for Container<T>

§Example

use std::marker::PhantomData;
use future_form::{FutureForm, Sendable, Local, future_form};

trait Counter<F: FutureForm> {
    fn next(&self) -> F::Future<'_, u32>;
}

struct Memory<F> {
    val: u32,
    _marker: PhantomData<F>,
}

#[future_form(Sendable, Local)]
impl<F: FutureForm> Counter<F> for Memory<F> {
    fn next(&self) -> F::Future<'_, u32> {
        let val = self.val;
        F::from_future(async move { val + 1 })
    }
}