#lifetime #closures #async #future #bounds #hrtb #borrow

short_future

Future with shorter lifetime for async closures

2 releases

0.1.1 Aug 16, 2024
0.1.0 Aug 16, 2024

#334 in Asynchronous

Download history 1133/week @ 2024-08-16 306/week @ 2024-08-23 813/week @ 2024-08-30 488/week @ 2024-09-06 496/week @ 2024-09-13 314/week @ 2024-09-20 311/week @ 2024-09-27 553/week @ 2024-10-04 800/week @ 2024-10-11 656/week @ 2024-10-18 567/week @ 2024-10-25 135/week @ 2024-11-01 168/week @ 2024-11-08 110/week @ 2024-11-15

1,027 downloads per month

MIT license

10KB
181 lines

Futures with short lifetimes

short_future is a library defining a utility for working with Future lifetimes: ShortBoxFuture<'a, 'b, T> can capture borrows with lifetimes of 'a and 'b at the same time.

ShortBoxFuture works around limitations of HRTBs and explicit lifetime bounds. This is useful when wrapping async closures, where the closure returns a future that depends on both:

  1. references in the enclosing scope with lifetime 'a.
  2. references in the closure's arguments with lifetime 'b.

For example, you can write a helper that retries a database operation, where a new transaction is created for every retry, and the data is borrowed from the enclosing scope:

async fn run_twice<'a>(&'a self, f: F) -> anyhow::Result<()>
where F: for<'b> Fn(&'b mut Transaction) -> ShortBoxFuture<'b, 'a, anyhow::Result<()>>
{
    for i in 0..2 {
        let mut tx = self.begin();
        f(&mut tx).0.await?;
    }
    Ok(())
}

async fn go(&self) {
    let data = get_data();
    self.run_twice(|tx| async {
        tx.get(&data.id).await;
    }.into()).await
}

See the tests for more examples, and for demonstrations of the issues that necessitate ShortBoxFuture.

Dependencies

~1MB
~15K SLoC