[][src]Crate futures_option

Extension traits for dealing with Options as Futures or Streams.

Examples

#![feature(async_await)]

use futures::future::{self, FusedFuture as _};
use futures_option::OptionExt as _;
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.

#![feature(async_await)]
#![recursion_limit="128"]

use futures::{future, stream, StreamExt as _};
use futures_option::OptionExt as _;
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);

Structs

Current

Adapter future for Option to get the next value of the stored stream.

Next

Adapter future for Option to get the next value of the stored future.

SelectNextSome

Adapter future for Option to get the next value of the stored future.

Traits

OptionExt

Extension methods for Option of Streams or Futures.