Skip to content

Conversation

@lyeb
Copy link

@lyeb lyeb commented Nov 7, 2025

Changes

Allow async_signature to have:

  • Return type: async_signature(...) -> ReturnType
  • Generic parameters: async_signature<T: TraitA, U>(...) where U: TraitB

If omitted, the synchronous implementation’s generic parameters and return type are used.

Motivation

Sometimes when working with both sync and async implementations, there is a situation where the generic parameters of a function and/or the return value are not the same.

E.g. if we have traits:

trait SyncTrait {
    type SyncReturn;
    fn foo() -> Self::SyncReturn;
}

trait AsyncTrait {
    type AsyncReturn;
    fn foo() -> Future<Output = Self::AsyncReturn>;
}

it is currently not possible to write a function like:

#[async_generic(...)]
fn generic_foo<T: *SyncOrAsyncTrait*>() -> T::*SyncOrAsyncReturn* { ... }

Solution

This PR adds return types and generic parameters to the async_signature argument.

The syntax is just like writing a function named async_signature:

// With return type
#[async_generic(async_signature(...) -> AsyncReturnType)]
fn foo(...) -> SyncReturnType { ... }

// With generics
#[async_generic(async_signature<T: TraitA>(...))]
fn foo<T: TraitB>(...) { ... }

// Or as complex as we want
#[async_generic(async_signature<'a, T: TraitA, U: TraitB, ...>(...) -> AsyncReturnType where C<T, U>: 'a + TraitC + ...)]
fn foo<'a, T: TraitD, ...>(...) -> SyncReturnType where D<T>: 'a + ... { ... }

If the generics or return type are omitted, those of the sync implementation are used. This means there is no breaking change in this PR, the behavior is the same as before, unless some generic parameters or a return type is specified.

Notes

Allow `async_signature` to have:
- Return type: `async_signature(...) -> ReturnType`
- Generic parameters: `async_signature<T: TraitA, U>(...) where U: TraitB`

If omitted, the synchronous implementation’s generic parameters and return type are used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generic dependent on sync/async

1 participant