Trait service_io::interface::Service
source · [−]pub trait Service {
fn run<'async_trait>(
self: Box<Self>,
input: Receiver,
output: Sender
) -> Pin<Box<dyn Future<Output = Result<(), ClosedChannel>> + Send + 'async_trait>>
where
Self: 'async_trait;
}
Expand description
Implement a service. A Service is an entity that processes input messages asynchronously and send output messages asynchronously.
If both, sender or receiver return a ClosedChannel
error,
it is expected to propagate this error.
See default implementations in services
Do not forget to add the async_trait
crate when implement this trait
Example
use service_io::interface::{Service};
use service_io::channel::{ClosedChannel, Receiver, Sender};
use async_trait::async_trait;
struct MyService;
#[async_trait]
impl Service for MyService {
async fn run(self: Box<Self>, mut input: Receiver, output: Sender) -> Result<(), ClosedChannel> {
// Load phase
// ...
loop {
// Get the message from the input connector.
let message = input.recv().await?;
// Do whatever your service impl must do.
// ...
// Send the message to the output connector.
output.send(message).await?;
}
}
}