short_future 0.1.1

Future with shorter lifetime for async closures
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented0 out of 1 items with examples
  • Size
  • Source code size: 10.68 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 357.65 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 24s Average build duration of successful builds.
  • all releases: 24s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • ldanilek/short_future
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • ldanilek

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.