4 releases
0.2.0 | Jan 5, 2020 |
---|---|
0.1.2 | Aug 20, 2019 |
0.1.1 | Jul 19, 2019 |
0.1.0 | Jul 19, 2019 |
#1524 in Asynchronous
10KB
124 lines
Option Extensions for Futures
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);
});
Dependencies
~25KB