promising_future/
spawner.rs

1use std::thread;
2
3#[cfg(feature = "threadpool")]
4use threadpool;
5
6/// A trait for spawning threads.
7pub trait Spawner {
8    /// Spawn a thread to run function `f`.
9    fn spawn<F>(&self, f: F) where F: FnOnce() + Send + 'static;
10}
11
12/// An implementation of `Spawner` that creates normal `std::thread` threads.
13pub struct ThreadSpawner;
14
15impl Spawner for ThreadSpawner {
16    fn spawn<F>(&self, f: F)
17        where F: FnOnce() + Send + 'static
18    {
19        let _ = thread::spawn(f);
20    }
21}
22
23#[cfg(feature = "threadpool")]
24impl Spawner for threadpool::ThreadPool {
25    fn spawn<F>(&self, f: F)
26        where F: FnOnce() + Send + 'static
27    {
28        self.execute(f)
29    }
30}