How can I allow lifetime arguments on this type parameter?

I have this code:

lambda: &(dyn for<'b> Fn(
  PlayerCreateLambdaArgs<'b>,
) -> Pin<Box<dyn Future<Output = Result<DomainPlayer, String>> + Send + 'b>>
      + Sync
      + '_)

and I would like to create a type for lambda to use it like:

lambda: &Lambda<'_, PlayerCreateLambdaArgs<'a>, DomainPlayer>,

I tried to use:

pub type Lambda<'a, Args, Res> = dyn for<'b> Fn(Args<'b>) -> Pin<Box<dyn Future<Output = Result<Res, String>> + Send + 'b>> + Sync + 'a;

but I'm getting this error:

error[E0109]: lifetime arguments are not allowed on type parameter `Args`
  --> src/main.rs:56:54
   |
56 | pub type Lambda<'a, Args, Res> = dyn for<'b> Fn(Args<'b>) -> Pin<Box<dyn Future<Output = Result<Res, String>> + Send + 'b>>
   |                                                 ---- ^^ lifetime argument not allowed
   |                                                 |
   |                                                 not allowed on type parameter `Args`
   |
note: type parameter `Args` defined here
  --> src/main.rs:56:21
   |
56 | pub type Lambda<'a, Args, Res> = dyn for<'b> Fn(Args<'b>) -> Pin<Box<dyn Future<Output = Result<Res, String>> + Send + 'b>>
   |                     ^^^^

error[E0582]: binding for associated type `Output` references lifetime `'b`, which does not appear in the trait input types
  --> src/main.rs:56:62
   |
56 | pub type Lambda<'a, Args, Res> = dyn for<'b> Fn(Args<'b>) -> Pin<Box<dyn Future<Output = Result<Res, String>> + Send + 'b>>
   |                                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Some errors have detailed explanations: E0109, E0582.
For more information about an error, try `rustc --explain E0109`.

How can I allow lifetime args on type parameter Args?

Rust doesn't have generic type constructors -- parameters like Args which are themselves parameterized.

You could require them to opt into something more concrete.

trait ReadyPlayerOne {
    type Args<'s>; // plus any sensible bounds
    type Result; // or a GAT if it might borrow from `Args`
}

pub type Lambda<'a, RPO> = dyn for<'b> 
    Fn(RPO::Args<'b>) -> Pin<Box<
        dyn Future<Output = Result<RPO::Result, String>> + Send + 'b
    >> + Sync + 'a;

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.