Created
August 19, 2016 18:41
-
-
Save jamwt/8c3dfb2f577e5bd68cc048e759b28614 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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