data_router/
view.rs

1use std::{
2    cell::RefCell,
3    rc::Rc,
4    sync::{Arc, Mutex, RwLock},
5};
6
7pub trait View<E> {
8    fn view(&mut self, event: &E) -> Option<DeleteView>;
9}
10
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
12pub struct DeleteView;
13
14// Rc + RefCell
15impl<E, R: View<E>> View<E> for Rc<RefCell<R>> {
16    fn view(&mut self, event: &E) -> Option<DeleteView> {
17        self.borrow_mut().view(event)
18    }
19}
20
21// Rc + Mutex
22impl<E, R: View<E>> View<E> for Rc<Mutex<R>> {
23    fn view(&mut self, event: &E) -> Option<DeleteView> {
24        self.lock().unwrap().view(event)
25    }
26}
27
28// Rc + RwLock
29impl<E, R: View<E>> View<E> for Rc<RwLock<R>> {
30    fn view(&mut self, event: &E) -> Option<DeleteView> {
31        self.write().unwrap().view(event)
32    }
33}
34
35// Arc + Mutex
36impl<E, R: View<E>> View<E> for Arc<Mutex<R>> {
37    fn view(&mut self, event: &E) -> Option<DeleteView> {
38        self.lock().unwrap().view(event)
39    }
40}
41
42// Arc + RwLock
43impl<E, R: View<E>> View<E> for Arc<RwLock<R>> {
44    fn view(&mut self, event: &E) -> Option<DeleteView> {
45        self.write().unwrap().view(event)
46    }
47}