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
use std::sync::{Arc, Mutex};

use pros_simulator_interface::SimulatorEvent;

#[derive(Clone)]
pub struct SimulatorInterface {
    callback: Arc<Mutex<dyn FnMut(SimulatorEvent) + Send>>,
}

impl<T> From<T> for SimulatorInterface
where
    T: FnMut(SimulatorEvent) + Send + 'static,
{
    fn from(callback: T) -> Self {
        Self {
            callback: Arc::new(Mutex::new(callback)),
        }
    }
}

impl SimulatorInterface {
    pub(crate) fn send(&self, event: SimulatorEvent) {
        let mut callback = self.callback.lock().unwrap();
        callback(event);
    }
}