Skip to content

Instantly share code, notes, and snippets.

@jamwt
Created August 19, 2016 18:41
Show Gist options
  • Save jamwt/8c3dfb2f577e5bd68cc048e759b28614 to your computer and use it in GitHub Desktop.
Save jamwt/8c3dfb2f577e5bd68cc048e759b28614 to your computer and use it in GitHub Desktop.
use std::sync::mpsc;
use futures::{
Future,
};
use futures::stream::{
Stream,
};
pub fn await_stream<T: Send + 'static, S: Stream<Item=T>>(stream: S) -> (Option<T>, S) {
let (s, r) = mpsc::channel();
let f = stream.into_future().and_then(move |(t, stream)| {
s.send((t, stream)).unwrap();
Ok(())
});
f.forget();
r.recv().unwrap()
}
pub fn await_future<E: Send + 'static, T: Send + 'static, F: Future<Item=T, Error=E>>(f: F) -> Result<T, E> {
let (s, r) = mpsc::channel();
let s2 = s.clone();
let f = f.and_then(move |t| {
s.send(Ok(t)).unwrap();
Ok(())
}).or_else(move |e| {
s2.send(Err(e)).unwrap();
Err(())
});
f.forget();
r.recv().unwrap()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment