try_future

Crates.iotry_future
lib.rstry_future
version0.1.3
sourcesrc
created_at2018-03-05 11:16:34.881161
updated_at2018-08-14 10:02:20.214072
descriptionConvenient short-hand for returning early from `futures`-based functions
homepage
repositoryhttps://github.com/srijs/rust-try-future
max_upload_size
id53940
size10,539
Sam Rijs (srijs)

documentation

README

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))
}
Commit count: 15

cargo fmt