euphrates/
memo.rs

1//! Memos - simple messages sent from devices to the user
2//!
3//! Memos are useful for debugging.
4
5use std::fmt::Display;
6use std::marker::PhantomData;
7
8pub trait Inbox {
9    type Memo;
10
11    fn receive_impl(&mut self, memo: Self::Memo);
12
13    #[inline]
14    fn receive(&mut self, memo: Self::Memo) {
15        if self.active() {
16            self.receive_impl(memo);
17        }
18    }
19
20    #[inline(always)]
21    fn active(&self) -> bool {
22        true
23    }
24
25    #[inline(always)]
26    fn holding(&self) -> bool {
27        false
28    }
29}
30
31/// An Inbox that throws away its memos.
32#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
33pub struct NothingInbox<M>(PhantomData<M>);
34
35impl<M> Default for NothingInbox<M> {
36    #[inline]
37    fn default() -> Self {
38        NothingInbox(PhantomData)
39    }
40}
41
42impl<M> Inbox for NothingInbox<M> {
43    type Memo = M;
44    #[inline]
45    fn receive_impl(&mut self, _memo: M) {}
46    #[inline]
47    fn active(&self) -> bool {
48        false
49    }
50}
51
52#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
53pub struct PrintingInbox<M: ?Sized>(PhantomData<M>);
54
55impl<M> Default for PrintingInbox<M> {
56    #[inline]
57    fn default() -> Self {
58        PrintingInbox(PhantomData)
59    }
60}
61
62impl<M> Inbox for PrintingInbox<M>
63where
64    M: Display,
65{
66    type Memo = M;
67
68    #[inline]
69    fn receive_impl(&mut self, memo: M) {
70        println!("{}", memo);
71    }
72}