futures-option

Crates.iofutures-option
lib.rsfutures-option
version0.2.0
sourcesrc
created_at2019-07-19 17:21:52.868904
updated_at2020-01-05 00:03:42.465695
descriptionExtension traits for dealing with optional futures and streams
homepagehttps://github.com/udoprog/futures-option
repositoryhttps://github.com/udoprog/futures-option
max_upload_size
id150176
size21,279
John-John Tedro (udoprog)

documentation

https://docs.rs/futures-option

README

Option Extensions for Futures

Build Status

Extension traits for dealing with optional futures and streams.

Examples

use futures::{future::{self, FusedFuture as _}};
use futures_option::OptionExt as _;

futures::executor::block_on(async {
    let mut f = Some(future::ready::<u32>(1));
    assert!(f.is_some());
    assert_eq!(f.current().await, 1);
    assert!(f.is_none());
    assert!(f.current().is_terminated());
});

This is useful when you want to implement optional branches using the select! macro.

#![recursion_limit="128"]

use futures::{future, stream, StreamExt as _};
use futures_option::OptionExt as _;

futures::executor::block_on(async {
    let mut value = None;
    let mut values = Some(stream::iter(vec![1u32, 2u32, 4u32].into_iter()).fuse());
    let mut parked = None;

    let mut sum = 0;

    loop {
        futures::select! {
            value = value.current() => {
                sum += value;
                std::mem::swap(&mut parked, &mut values);
            }
            v = values.next() => {
                match v {
                    Some(v) => {
                        value = Some(future::ready(v));
                        std::mem::swap(&mut parked, &mut values);
                    },
                    None => break,
                }
            }
        }
    }

    assert_eq!(7, sum);
});
Commit count: 54

cargo fmt