Difficulty with trait function lifetimes

async fn and RPITIT (return position impl trait in traits ) capture input lifetimes of the function/method, but RPIT outside of traits does not. On stable, you can do this, with some cost/loss of ergonomics/loss of auto-trait flexibility:

trait Database {
    async fn load_events(&self) -> Pin<Box<dyn Stream<Item = String> + Send>>;

On nightly, you can use TAITIT or whatever they're calling it instead.

The current plan is for RPIT outside of traits to start capturing lifetimes in the next edition, which will break your current non-trait code. An analogous workaround is also planned, which is to be more explicit about what is captured using a TAIT (type alias impl trait). Here is your non-trait code with a TAIT on edition 2024. The Send bound was also required, so the fix is even less trivial than typical. I suggest mentioning your use case in the tracking issue for the lifetime capture change.

1 Like