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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
pub use super::*;
use std::cmp::PartialEq;
use std::fmt;

impl<P> Clone for ThreadPool<P>
where P: SyncThreadPool + Sized + Clone
{
    fn clone(&self) -> Self {
        Self {
            inner: Mutex::new(self.inner.lock().clone())
        }
    }
}

impl<P, Rhs> PartialEq<Rhs> for  ThreadPool<P>
where P: SyncThreadPool + Sized + PartialEq<Rhs>,
      Rhs: ?Sized
{
    fn eq(&self, other: &Rhs) -> bool {
        self.inner.lock().eq(other)
    }
}


impl<P> Eq for ThreadPool<P>
where Self: PartialEq,
      P: SyncThreadPool + Sized + Eq {}

impl<P> fmt::Debug for ThreadPool<P>
where P: SyncThreadPool + Sized + fmt::Debug
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ThreadPool")
         .field("inner", &self.inner.lock())
         .finish()
    }
}

impl<P> From<P> for ThreadPool<P>
where P: SyncThreadPool + Sized + Eq {
    fn from(p: P) -> Self {
        Self::from_threadpool(p)
    }
}

#[cfg(feature = "threadpool")]
impl SyncThreadPool for threadpool::ThreadPool {
    fn new() -> Self {
        Self::default()
    }
    fn with_thread_count(num_threads: usize) -> Self {
        Self::new(num_threads)
    }
    fn execute<F>(&self, fun: F)
    where
        F: FnOnce() + Send + 'static
    {
        self.execute(fun);
    }
}

#[cfg(feature = "uvth")]
impl SyncThreadPool for uvth::ThreadPool {
    fn new() -> Self {
        uvth::ThreadPoolBuilder::new().build()
    }
    fn with_thread_count(num_threads: usize) -> Self {
        uvth::ThreadPoolBuilder::new().num_threads(num_threads).build()
    }
    fn execute<F>(&self, fun: F)
    where
        F: FnOnce() + Send + 'static
    {
        self.execute(fun);
    }
}