Skip to content

Commit

Permalink
server: Add bounds and docs for associated types of Service trait (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
uklotzde authored Jul 21, 2024
1 parent dd1f676 commit 528e797
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ mod error;
pub use self::error::{Error, ProtocolError};

mod frame;
#[cfg(feature = "server")]
pub use self::frame::SlaveRequest;
pub use self::frame::{
Address, Exception, ExceptionResponse, FunctionCode, Quantity, Request, Response,
};
Expand Down
10 changes: 1 addition & 9 deletions src/server/rtu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ use crate::{
rtu::{RequestAdu, ResponseAdu},
ExceptionResponse, OptionalResponsePdu,
},
server::service::Service,
Exception, Response,
};

use super::Terminated;
use super::{Service, Terminated};

#[derive(Debug)]
pub struct Server {
Expand All @@ -45,8 +43,6 @@ impl Server {
where
S: Service + Send + Sync + 'static,
S::Request: From<RequestAdu<'static>> + Send,
S::Response: Into<Option<Response>>,
S::Exception: Into<Exception>,
{
let framed = Framed::new(self.serial, ServerCodec::default());
process(framed, service).await
Expand All @@ -60,8 +56,6 @@ impl Server {
where
S: Service + Send + Sync + 'static,
S::Request: From<RequestAdu<'static>> + Send,
S::Response: Into<Option<Response>>,
S::Exception: Into<Exception>,
X: Future<Output = ()> + Sync + Send + Unpin + 'static,
{
let framed = Framed::new(self.serial, ServerCodec::default());
Expand All @@ -82,8 +76,6 @@ async fn process<S>(mut framed: Framed<SerialStream, ServerCodec>, service: S) -
where
S: Service + Send + Sync + 'static,
S::Request: From<RequestAdu<'static>> + Send,
S::Response: Into<Option<Response>>,
S::Exception: Into<Exception>,
{
loop {
let Some(request) = framed.next().await.transpose()? else {
Expand Down
10 changes: 1 addition & 9 deletions src/server/rtu_over_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ use crate::{
rtu::{RequestAdu, ResponseAdu},
ExceptionResponse, OptionalResponsePdu,
},
server::service::Service,
Exception, Response,
};

use super::Terminated;
use super::{Service, Terminated};

#[async_trait]
pub trait BindSocket {
Expand Down Expand Up @@ -76,8 +74,6 @@ impl Server {
where
S: Service + Send + Sync + 'static,
S::Request: From<RequestAdu<'static>> + Send,
S::Response: Into<Option<Response>>,
S::Exception: Into<Exception>,
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
OnConnected: Fn(TcpStream, SocketAddr) -> F,
F: Future<Output = io::Result<Option<(S, T)>>>,
Expand Down Expand Up @@ -118,8 +114,6 @@ impl Server {
where
S: Service + Send + Sync + 'static,
S::Request: From<RequestAdu<'static>> + Send,
S::Response: Into<Option<Response>>,
S::Exception: Into<Exception>,
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
X: Future<Output = ()> + Sync + Send + Unpin + 'static,
OnConnected: Fn(TcpStream, SocketAddr) -> F,
Expand All @@ -143,8 +137,6 @@ async fn process<S, T>(mut framed: Framed<T, ServerCodec>, service: S) -> io::Re
where
S: Service + Send + Sync + 'static,
S::Request: From<RequestAdu<'static>> + Send,
S::Response: Into<Option<Response>>,
S::Exception: Into<Exception>,
T: AsyncRead + AsyncWrite + Unpin,
{
loop {
Expand Down
15 changes: 13 additions & 2 deletions src/server/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@ use std::{future::Future, ops::Deref};
/// A Modbus server service.
pub trait Service {
/// Requests handled by the service.
///
/// Both [`tokio_modbus::Request`](crate::Request) and
/// [`tokio_modbus::SlaveRequest`](crate::SlaveRequest)
/// are possible choices.
type Request;

/// Responses sent by the service.
type Response;
///
/// Both [`tokio_modbus::Response`](crate::Response) and
/// `Option<tokio_modbus::Response>` are possible choices.
/// The latter allows to selectively ignore requests
/// by not sending a response.
type Response: Into<Option<crate::Response>>;

/// Exceptional responses sent by the service.
type Exception;
///
/// Use [`tokio_modbus::Exception`](crate::Exception) as default.
type Exception: Into<crate::Exception>;

/// The future response value.
type Future: Future<Output = Result<Self::Response, Self::Exception>> + Send;
Expand Down
10 changes: 1 addition & 9 deletions src/server/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ use crate::{
tcp::{RequestAdu, ResponseAdu},
ExceptionResponse, OptionalResponsePdu,
},
server::service::Service,
Exception, Response,
};

use super::Terminated;
use super::{Service, Terminated};

#[async_trait]
pub trait BindSocket {
Expand Down Expand Up @@ -76,8 +74,6 @@ impl Server {
where
S: Service + Send + Sync + 'static,
S::Request: From<RequestAdu<'static>> + Send,
S::Response: Into<Option<Response>>,
S::Exception: Into<Exception>,
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
OnConnected: Fn(TcpStream, SocketAddr) -> F,
F: Future<Output = io::Result<Option<(S, T)>>>,
Expand Down Expand Up @@ -117,8 +113,6 @@ impl Server {
where
S: Service + Send + Sync + 'static,
S::Request: From<RequestAdu<'static>> + Send,
S::Response: Into<Option<Response>>,
S::Exception: Into<Exception>,
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
X: Future<Output = ()> + Sync + Send + Unpin + 'static,
OnConnected: Fn(TcpStream, SocketAddr) -> F,
Expand All @@ -142,8 +136,6 @@ async fn process<S, T>(mut framed: Framed<T, ServerCodec>, service: S) -> io::Re
where
S: Service + Send + Sync + 'static,
S::Request: From<RequestAdu<'static>> + Send,
S::Response: Into<Option<Response>>,
S::Exception: Into<Exception>,
T: AsyncRead + AsyncWrite + Unpin,
{
loop {
Expand Down

0 comments on commit 528e797

Please sign in to comment.