rat_salsa/poll/
thread_pool.rs

1use crate::poll::PollEvents;
2use crate::thread_pool::ThreadPool;
3use crate::Control;
4use crossbeam::channel::TryRecvError;
5use std::any::Any;
6use std::rc::Rc;
7
8/// Processes results from background tasks.
9#[derive(Debug)]
10pub struct PollTasks<Event, Error>
11where
12    Event: 'static,
13    Error: 'static,
14{
15    tasks: Rc<ThreadPool<Event, Error>>,
16}
17
18impl<Event, Error> Default for PollTasks<Event, Error>
19where
20    Event: 'static + Send,
21    Error: 'static + Send,
22{
23    fn default() -> Self {
24        Self::new(1)
25    }
26}
27
28impl<Event, Error> PollTasks<Event, Error>
29where
30    Event: 'static + Send,
31    Error: 'static + Send,
32{
33    pub fn new(num_workers: usize) -> Self {
34        Self {
35            tasks: Rc::new(ThreadPool::new(num_workers)),
36        }
37    }
38}
39
40impl<Event, Error> PollTasks<Event, Error>
41where
42    Event: 'static,
43    Error: 'static,
44{
45    pub(crate) fn get_tasks(&self) -> Rc<ThreadPool<Event, Error>> {
46        self.tasks.clone()
47    }
48}
49
50impl<Event, Error> PollEvents<Event, Error> for PollTasks<Event, Error>
51where
52    Event: 'static + Send,
53    Error: 'static + Send + From<TryRecvError>,
54{
55    fn as_any(&self) -> &dyn Any {
56        self
57    }
58
59    fn poll(&mut self) -> Result<bool, Error> {
60        Ok(!self.tasks.is_empty())
61    }
62
63    fn read(&mut self) -> Result<Control<Event>, Error> {
64        self.tasks.try_recv()
65    }
66}