utils_plugs/
plugin.rs

1use std::sync::{Arc, Mutex, MutexGuard};
2
3#[derive(Debug, Default)]
4pub struct Plugin<T> {
5    p: Option<Arc<Mutex<T>>>,
6}
7
8impl<T> Plugin<T> {
9    pub fn none() -> Self {
10        Self { p: None }
11    }
12    pub fn new(m: T) -> Self {
13        Self {
14            p: Some(Arc::new(Mutex::new(m))),
15        }
16    }
17    pub fn copy_from(&mut self, p: &Self) {
18        self.p = p.p.clone();
19    }
20    pub fn is_plugged_in(&self) -> bool {
21        self.p.is_some()
22    }
23    pub fn unwrap(&self) -> MutexGuard<T> {
24        match &self.p {
25            Some(v) => v.lock().unwrap(),
26            None => panic!("empty plugin."),
27        }
28    }
29}