Skip to main content

libp2p_pubsub_common/service/
context_handles.rs

1use std::collections::VecDeque;
2
3/// A service context inbox handle.
4pub struct InCtx<'a, InEvent> {
5    inbox: &'a mut VecDeque<InEvent>,
6}
7
8impl<'a, InEvent> InCtx<'a, InEvent> {
9    /// Create a new service context inbox handle.
10    pub(super) fn new(inbox: &'a mut VecDeque<InEvent>) -> Self {
11        Self { inbox }
12    }
13
14    /// Get the number of events in the input mailbox.
15    pub fn len(&self) -> usize {
16        self.inbox.len()
17    }
18
19    /// Check if the input mailbox is empty.
20    pub fn is_empty(&self) -> bool {
21        self.inbox.is_empty()
22    }
23
24    /// Pop the next event from the input mailbox.
25    pub fn pop_next(&mut self) -> Option<InEvent> {
26        self.inbox.pop_front()
27    }
28}
29
30/// A service context outbox handle.
31pub struct OutCtx<'a, OutEvent> {
32    outbox: &'a mut VecDeque<OutEvent>,
33}
34
35impl<'a, OutEvent> OutCtx<'a, OutEvent> {
36    /// Create a new service context outbox handle.
37    pub(super) fn new(outbox: &'a mut VecDeque<OutEvent>) -> Self {
38        Self { outbox }
39    }
40
41    /// Emit an event to the output mailbox.
42    pub fn emit(&mut self, ev: OutEvent) {
43        self.outbox.push_back(ev);
44    }
45
46    /// Emit a batch of events to the output mailbox.
47    pub fn emit_batch(&mut self, evs: impl IntoIterator<Item = OutEvent>) {
48        self.outbox.extend(evs);
49    }
50}
51
52/// A service context mailbox handle for the [`Service::on_event`](super::Service::on_event) method.
53pub struct OnEventCtx<'a, OutEvent> {
54    outbox: OutCtx<'a, OutEvent>,
55}
56
57impl<'a, OutEvent> OnEventCtx<'a, OutEvent> {
58    /// Emit an event to the output mailbox.
59    pub fn emit(&mut self, ev: OutEvent) {
60        self.outbox.emit(ev);
61    }
62
63    /// Emit a batch of events to the output mailbox.
64    pub fn emit_batch(&mut self, ev: impl IntoIterator<Item = OutEvent>) {
65        self.outbox.emit_batch(ev);
66    }
67}
68
69impl<'a, OutEvent> From<OutCtx<'a, OutEvent>> for OnEventCtx<'a, OutEvent> {
70    fn from(value: OutCtx<'a, OutEvent>) -> Self {
71        Self { outbox: value }
72    }
73}
74
75/// A service context mailbox handle for the [`Service::poll`](super::Service::poll) method.
76pub struct PollCtx<'a, InEvent, OutEvent> {
77    inbox: InCtx<'a, InEvent>,
78    outbox: OutCtx<'a, OutEvent>,
79}
80
81impl<'a, InEvent, OutEvent> PollCtx<'a, InEvent, OutEvent> {
82    /// Create a new service context mailbox handle.
83    pub(super) fn new(
84        inbox: &'a mut VecDeque<InEvent>,
85        outbox: &'a mut VecDeque<OutEvent>,
86    ) -> Self {
87        Self {
88            inbox: InCtx::new(inbox),
89            outbox: OutCtx::new(outbox),
90        }
91    }
92
93    /// Split this context into its input and output mailbox context handles.
94    pub fn split(self) -> (InCtx<'a, InEvent>, OutCtx<'a, OutEvent>) {
95        (self.inbox, self.outbox)
96    }
97
98    /// Get the number of events in the input mailbox.
99    pub fn len(&self) -> usize {
100        self.inbox.len()
101    }
102
103    /// Check if the input mailbox is empty.
104    pub fn is_empty(&self) -> bool {
105        self.inbox.is_empty()
106    }
107
108    /// Pop the next event from the input mailbox.
109    pub fn pop_next(&mut self) -> Option<InEvent> {
110        self.inbox.pop_next()
111    }
112
113    /// Emit an event to the output mailbox.
114    pub fn emit(&mut self, ev: OutEvent) {
115        self.outbox.emit(ev);
116    }
117
118    /// Emit a batch of events to the output mailbox.
119    pub fn emit_batch(&mut self, evs: impl IntoIterator<Item = OutEvent>) {
120        self.outbox.emit_batch(evs);
121    }
122}