relay_system/service/
simple.rs

1use crate::{Interface, Service};
2
3/// A service that handles messages one at a time.
4///
5/// When the message handler cannot keep up with incoming messages,
6/// it applies backpressure into the input queue.
7pub trait SimpleService {
8    /// The message interface (see [`Service::Interface`]).
9    type Interface: Interface;
10
11    /// The asynchronous message handler for this service.
12    fn handle_message(&self, message: Self::Interface) -> impl Future<Output = ()> + Send;
13}
14
15impl<T: SimpleService + Send + 'static> Service for T {
16    type Interface = T::Interface;
17
18    async fn run(self, mut rx: super::Receiver<Self::Interface>) {
19        while let Some(message) = rx.recv().await {
20            self.handle_message(message).await;
21        }
22    }
23}