pub trait Service:
Send
+ Sync
+ 'static {
// Required method
fn call<'life0, 'life1, 'async_trait>(
&'life0 self,
request: Request<'life1>,
) -> Pin<Box<dyn Future<Output = Result<Response, Exception>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
}Expand description
A Modbus server service — handles incoming requests and produces responses.
Implement this trait to create a custom Modbus server. The transport loops
(TCP, RTU, ASCII) call call for each decoded request.
For hooking into the request/response lifecycle without reimplementing the
entire trait, see ServerHook and HookedService.
§Example
use async_trait::async_trait;
use oms_modbus::frame::{Request, Response, Exception};
use oms_modbus::server::Service;
/// A fixed-value service — always returns the same register value.
struct FixedService { value: u16 }
#[async_trait]
impl Service for FixedService {
async fn call(&self, request: Request<'_>) -> Result<Response, Exception> {
match request {
Request::ReadHoldingRegisters(_addr, qty) =>
Ok(Response::ReadHoldingRegisters(vec![self.value; qty as usize])),
_ => Err(Exception::IllegalFunction),
}
}
}Required Methods§
fn call<'life0, 'life1, 'async_trait>(
&'life0 self,
request: Request<'life1>,
) -> Pin<Box<dyn Future<Output = Result<Response, Exception>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".