1use std::{
2 cell::RefCell,
3 rc::Rc,
4 sync::{Arc, Mutex, RwLock},
5};
6
7pub mod pass_receiver;
8
9pub trait Receive<E> {
10 type Output;
11
12 fn send(&mut self, event: E) -> ReceiverResult<E, Self::Output>;
13}
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
16pub enum ReceiverResult<E, T> {
17 Continue(T),
18 Stop,
19 Delete(E),
20}
21impl<E, T> ReceiverResult<E, T> {
22 pub fn is_continue(&self) -> bool {
23 matches!(self, ReceiverResult::Continue(_))
24 }
25 pub fn is_stop(&self) -> bool {
26 matches!(self, ReceiverResult::Stop)
27 }
28 pub fn is_delete(&self) -> bool {
29 matches!(self, ReceiverResult::Delete(_))
30 }
31
32 pub fn unwrap_continue(self) -> T {
33 match self {
34 ReceiverResult::Continue(t) => t,
35 _ => panic!(),
36 }
37 }
38 pub fn unwrap_delete(self) -> E {
39 match self {
40 ReceiverResult::Delete(e) => e,
41 _ => panic!(),
42 }
43 }
44}
45
46impl<E, R: Receive<E>> Receive<E> for Rc<RefCell<R>> {
48 type Output = R::Output;
49
50 fn send(&mut self, event: E) -> ReceiverResult<E, Self::Output> {
51 self.borrow_mut().send(event)
52 }
53}
54
55impl<E, R: Receive<E>> Receive<E> for Rc<Mutex<R>> {
57 type Output = R::Output;
58
59 fn send(&mut self, event: E) -> ReceiverResult<E, Self::Output> {
60 self.lock().unwrap().send(event)
61 }
62}
63
64impl<E, R: Receive<E>> Receive<E> for Rc<RwLock<R>> {
66 type Output = R::Output;
67
68 fn send(&mut self, event: E) -> ReceiverResult<E, Self::Output> {
69 self.write().unwrap().send(event)
70 }
71}
72
73impl<E, R: Receive<E>> Receive<E> for Arc<Mutex<R>> {
75 type Output = R::Output;
76
77 fn send(&mut self, event: E) -> ReceiverResult<E, Self::Output> {
78 self.lock().unwrap().send(event)
79 }
80}
81
82impl<E, R: Receive<E>> Receive<E> for Arc<RwLock<R>> {
84 type Output = R::Output;
85
86 fn send(&mut self, event: E) -> ReceiverResult<E, Self::Output> {
87 self.write().unwrap().send(event)
88 }
89}