1use crossbeam_channel::{unbounded, Receiver, Sender};
2use std::thread::{self, JoinHandle};
3
4pub struct WorkerThread {
7 handle: JoinHandle<()>,
8 done: Sender<()>,
9}
10
11impl WorkerThread {
12 pub fn spawn<F>(fun: F) -> Self
14 where
15 F: FnOnce(Receiver<()>) + Send + 'static,
16 {
17 let (tx, rx) = unbounded::<()>();
18 let handle = thread::spawn(move || fun(rx));
19
20 Self { handle, done: tx }
21 }
22
23 pub fn close(self) {
26 self.done.send(()).unwrap();
27 self.handle.join().unwrap();
28 }
29}