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
//! An abstraction for worker threads.
//!
//! The abstraction provided some consistency on how worker threads
//! are implemented and used, as well as logging. A worker thread does
//! what it does. The abstraction is not meant to constrain that.
use std::thread::{JoinHandle, spawn};
use crate::logger;
/// Start a new thread. Caller must catch the thread handle and
/// join it to wait for thread to end.
pub fn start_thread<W: Worker>(mut o: W) -> JoinHandle<Result<(), W::Error>> {
let name = o.name();
spawn(move || {
logger::worker_start(&name);
let result = o.work();
logger::worker_end(&name, &result);
result
})
}
/// A worker thread.
pub trait Worker: Send + 'static {
/// Name of thread, or kind of thread. Used for logging only.
const NAME: &str;
/// Type of error from this worker.
type Error: std::error::Error + Send;
/// Do the work the thread is supposed to do.
fn work(&mut self) -> Result<(), Self::Error>;
/// Return name of thread as an owned string.
fn name(&self) -> String {
Self::NAME.to_string()
}
}