1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use futures::{Future, Async};
use futures::sync::oneshot;
use std::{thread, panic};
use void::Void;

/// Wraps a synchronous function into a future by running it in its own thread. Created using
/// `thread_future`.
pub struct ThreadFuture<R> {
    join_handle: Option<thread::JoinHandle<()>>,
    rx: oneshot::Receiver<R>,
}

/// Run a synchronous function in a separate thread and return its result as a `Future`.
///
/// # Note
///
/// If the given function panics then so will this future.
pub fn thread_future<F, R>(f: F) -> ThreadFuture<R>
where
    R: Send + 'static,
    F: FnOnce() -> R + Send + 'static
{
    let (tx, rx) = oneshot::channel();
    let join_handle = Some(thread::spawn(|| {
        let x = f();
        let _ = tx.send(x);
    }));
    ThreadFuture {
        rx,
        join_handle,
    }
}

impl<R> Future for ThreadFuture<R> {
    type Item = R;
    type Error = Void;

    fn poll(&mut self) -> Result<Async<R>, Void> {
        match self.rx.poll() {
            Ok(x) => Ok(x),
            Err(oneshot::Canceled) => {
                // The thread must have died. Get the error.
                match unwrap!(self.join_handle.take()).join() {
                    Ok(()) => unreachable!(),
                    Err(e) => panic::resume_unwind(e),
                }
            },
        }
    }
}