virtual_mio/
arc.rs

1use std::sync::Arc;
2use std::sync::Mutex;
3
4use crate::{InterestHandler, InterestType};
5
6#[derive(Debug)]
7struct ArcInterestHandlerState {
8    handler: Box<dyn InterestHandler + Send + Sync>,
9}
10
11#[derive(Debug, Clone)]
12pub struct ArcInterestHandler {
13    state: Arc<Mutex<ArcInterestHandlerState>>,
14}
15
16impl ArcInterestHandler {
17    pub fn new(handler: Box<dyn InterestHandler + Send + Sync>) -> Self {
18        Self {
19            state: Arc::new(Mutex::new(ArcInterestHandlerState { handler })),
20        }
21    }
22}
23
24impl InterestHandler for ArcInterestHandler {
25    fn push_interest(&mut self, interest: InterestType) {
26        let mut state = self.state.lock().unwrap();
27        state.handler.push_interest(interest)
28    }
29
30    fn pop_interest(&mut self, interest: InterestType) -> bool {
31        let mut state = self.state.lock().unwrap();
32        state.handler.pop_interest(interest)
33    }
34
35    fn has_interest(&self, interest: InterestType) -> bool {
36        let state = self.state.lock().unwrap();
37        state.handler.has_interest(interest)
38    }
39}