Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: Add DynamicService trait and adapter #245

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@

# Changelog

## v0.10.1 (Unreleased)

- Server: Remove `Sync` and `Unpin` trait bounds from `Service::call()` future
result.
- Server: Add `DynamicService`/`DynamicServiceAdapter` opt-in for dynamic
dispatch.

## v0.10.0 (2024-01-03)

- Feature: Retrieve the `FunctionCode` of a `Request`/`Response`.
- Feature: Added `rtu-over-tcp-server` for _RTU over TCP_ server skeleton.
- Feature: Add `rtu-over-tcp-server` for _RTU over TCP_ server skeleton.

### Breaking Changes

Expand Down
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod tcp;
pub mod rtu_over_tcp;

mod service;
pub use self::service::Service;
pub use self::service::{DynamicService, DynamicServiceAdapter, Service};

/// Cause for termination
#[derive(Debug, Clone)]
Expand Down
64 changes: 60 additions & 4 deletions src/server/service.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// SPDX-FileCopyrightText: Copyright (c) 2017-2024 slowtec GmbH <[email protected]>
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::future::Future;
use std::ops::Deref;
use std::{future::Future, ops::Deref, pin::Pin};

/// A Modbus server service.
pub trait Service {
Expand All @@ -16,15 +15,15 @@ pub trait Service {
type Error;

/// The future response value.
type Future: Future<Output = Result<Self::Response, Self::Error>> + Send + Sync + Unpin;
type Future: Future<Output = Result<Self::Response, Self::Error>> + Send;

/// Process the request and return the response asynchronously.
fn call(&self, req: Self::Request) -> Self::Future;
}

impl<D> Service for D
where
D: Deref,
D: Deref + ?Sized,
D::Target: Service,
{
type Request = <D::Target as Service>::Request;
Expand All @@ -37,3 +36,60 @@ where
self.deref().call(req)
}
}

/// A Modbus server service that uses dynamic dispatch.
pub trait DynamicService {
/// Requests handled by the service.
type Request;

/// Responses given by the service.
type Response;

/// Errors produced by the service.
type Error;

/// Process the request and return the response asynchronously.
#[allow(clippy::type_complexity)]
fn call(
&self,
req: Self::Request,
) -> Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
}

/// An adapter that allows to use a [`DynamicService`] as a [`Service`].
#[derive(Debug, Clone)]
pub struct DynamicServiceAdapter<T> {
delegate: T,
}

impl<T> Service for DynamicServiceAdapter<T>
where
T: DynamicService,
{
type Request = T::Request;
type Response = T::Response;
type Error = T::Error;
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

fn call(&self, req: Self::Request) -> Self::Future {
self.delegate.call(req)
}
}

impl<D> DynamicService for D
where
D: Deref + ?Sized,
D::Target: DynamicService,
{
type Request = <D::Target as DynamicService>::Request;
type Response = <D::Target as DynamicService>::Response;
type Error = <D::Target as DynamicService>::Error;

/// A forwarding blanket impl to support smart pointers around [`DynamicService`].
fn call(
&self,
req: Self::Request,
) -> Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>> {
self.deref().call(req)
}
}