#try #future #futures #work #before #convenient

try_future

Convenient short-hand for returning early from futures-based functions

4 releases

Uses old Rust 2015

0.1.3 Aug 14, 2018
0.1.2 Mar 6, 2018
0.1.1 Mar 5, 2018
0.1.0 Mar 5, 2018
Download history 17/week @ 2022-11-28 15/week @ 2022-12-05 27/week @ 2022-12-12 13/week @ 2022-12-19 9/week @ 2022-12-26 19/week @ 2023-01-02 10/week @ 2023-01-09 3/week @ 2023-01-16 25/week @ 2023-01-23 26/week @ 2023-01-30 14/week @ 2023-02-06 15/week @ 2023-02-13 26/week @ 2023-02-20 4/week @ 2023-02-27 13/week @ 2023-03-06 6/week @ 2023-03-13

53 downloads per month
Used in noob

MIT license

10KB
125 lines

try_future

Build Status Dependency Status crates.io

This crate aims to provide a convenient short-hand for returning early from futures-based functions.

The general pattern it supports is where before a function performs an asynchronous task, it does some work that might result in an early termination, for example:

  • certain parsing or validation logic might fail, upon which the function should return immediately with an error
  • some local cache lookup or other optimization that might render the asynchronous task unnecessary, and where the function would want immediately return a value instead

Examples

Using impl Future<_>

#[macro_use] extern crate try_future;

fn make_request<C: Connect>(target: &str, client: &Client<C>) ->
    impl Future<Item=Response, Error=Error>
{
    let uri = try_future!(target.parse::<Uri>());

    client.get(uri).into()
}

Using Box<Future<_>>

#[macro_use] extern crate try_future;

fn make_request<C: Connect>(target: &str, client: &Client<C>) ->
    Box<Future<Item=Response, Error=Error>>
{
    let uri = try_future_box!(target.parse::<Uri>());

    Box::new(client.get(uri))
}

Dependencies

~52KB