Skip to content

Commit d563ad0

Browse files
committed
trait Service: Make server-side response optional again
1 parent 8fb50bc commit d563ad0

File tree

10 files changed

+92
-86
lines changed

10 files changed

+92
-86
lines changed

examples/rtu-over-tcp-server.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,35 @@ struct ExampleService {
2828

2929
impl tokio_modbus::server::Service for ExampleService {
3030
type Request = SlaveRequest<'static>;
31-
type Future = future::Ready<Result<Response, Exception>>;
31+
type Future = future::Ready<Result<Option<Response>, Exception>>;
3232

3333
fn call(&self, req: Self::Request) -> Self::Future {
3434
println!("{}", req.slave);
35-
match req.request {
36-
Request::ReadInputRegisters(addr, cnt) => future::ready(
35+
let res = match req.request {
36+
Request::ReadInputRegisters(addr, cnt) => {
3737
register_read(&self.input_registers.lock().unwrap(), addr, cnt)
38-
.map(Response::ReadInputRegisters),
39-
),
40-
Request::ReadHoldingRegisters(addr, cnt) => future::ready(
38+
.map(Response::ReadInputRegisters)
39+
}
40+
Request::ReadHoldingRegisters(addr, cnt) => {
4141
register_read(&self.holding_registers.lock().unwrap(), addr, cnt)
42-
.map(Response::ReadHoldingRegisters),
43-
),
44-
Request::WriteMultipleRegisters(addr, values) => future::ready(
42+
.map(Response::ReadHoldingRegisters)
43+
}
44+
Request::WriteMultipleRegisters(addr, values) => {
4545
register_write(&mut self.holding_registers.lock().unwrap(), addr, &values)
46-
.map(|_| Response::WriteMultipleRegisters(addr, values.len() as u16)),
47-
),
48-
Request::WriteSingleRegister(addr, value) => future::ready(
49-
register_write(
50-
&mut self.holding_registers.lock().unwrap(),
51-
addr,
52-
std::slice::from_ref(&value),
53-
)
54-
.map(|_| Response::WriteSingleRegister(addr, value)),
55-
),
46+
.map(|_| Response::WriteMultipleRegisters(addr, values.len() as u16))
47+
}
48+
Request::WriteSingleRegister(addr, value) => register_write(
49+
&mut self.holding_registers.lock().unwrap(),
50+
addr,
51+
std::slice::from_ref(&value),
52+
)
53+
.map(|_| Response::WriteSingleRegister(addr, value)),
5654
_ => {
5755
println!("SERVER: Exception::IllegalFunction - Unimplemented function code in request: {req:?}");
58-
future::ready(Err(Exception::IllegalFunction))
56+
Err(Exception::IllegalFunction)
5957
}
60-
}
58+
};
59+
future::ready(res.map(Some))
6160
}
6261
}
6362

examples/rtu-server-address.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Service {
1313

1414
impl tokio_modbus::server::Service for Service {
1515
type Request = SlaveRequest<'static>;
16-
type Future = future::Ready<Result<Response, Exception>>;
16+
type Future = future::Ready<Result<Option<Response>, Exception>>;
1717

1818
fn call(&self, req: Self::Request) -> Self::Future {
1919
if req.slave != self.slave.into() {
@@ -23,7 +23,7 @@ impl tokio_modbus::server::Service for Service {
2323
Request::ReadInputRegisters(_addr, cnt) => {
2424
let mut registers = vec![0; cnt.into()];
2525
registers[2] = 0x77;
26-
future::ready(Ok(Response::ReadInputRegisters(registers)))
26+
future::ready(Ok(Some(Response::ReadInputRegisters(registers))))
2727
}
2828
_ => future::ready(Err(Exception::IllegalFunction)),
2929
}

examples/rtu-server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ struct Service;
1111

1212
impl tokio_modbus::server::Service for Service {
1313
type Request = SlaveRequest<'static>;
14-
type Future = future::Ready<Result<Response, Exception>>;
14+
type Future = future::Ready<Result<Option<Response>, Exception>>;
1515

1616
fn call(&self, req: Self::Request) -> Self::Future {
1717
match req.request {
1818
Request::ReadInputRegisters(_addr, cnt) => {
1919
let mut registers = vec![0; cnt.into()];
2020
registers[2] = 0x77;
21-
future::ready(Ok(Response::ReadInputRegisters(registers)))
21+
future::ready(Ok(Some(Response::ReadInputRegisters(registers))))
2222
}
2323
Request::ReadHoldingRegisters(_, _) => {
2424
future::ready(Err(Exception::IllegalDataAddress))

examples/tcp-server.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,34 @@ struct ExampleService {
2828

2929
impl tokio_modbus::server::Service for ExampleService {
3030
type Request = Request<'static>;
31-
type Future = future::Ready<Result<Response, Exception>>;
31+
type Future = future::Ready<Result<Option<Response>, Exception>>;
3232

3333
fn call(&self, req: Self::Request) -> Self::Future {
34-
match req {
35-
Request::ReadInputRegisters(addr, cnt) => future::ready(
34+
let res = match req {
35+
Request::ReadInputRegisters(addr, cnt) => {
3636
register_read(&self.input_registers.lock().unwrap(), addr, cnt)
37-
.map(Response::ReadInputRegisters),
38-
),
39-
Request::ReadHoldingRegisters(addr, cnt) => future::ready(
37+
.map(Response::ReadInputRegisters)
38+
}
39+
Request::ReadHoldingRegisters(addr, cnt) => {
4040
register_read(&self.holding_registers.lock().unwrap(), addr, cnt)
41-
.map(Response::ReadHoldingRegisters),
42-
),
43-
Request::WriteMultipleRegisters(addr, values) => future::ready(
41+
.map(Response::ReadHoldingRegisters)
42+
}
43+
Request::WriteMultipleRegisters(addr, values) => {
4444
register_write(&mut self.holding_registers.lock().unwrap(), addr, &values)
45-
.map(|_| Response::WriteMultipleRegisters(addr, values.len() as u16)),
46-
),
47-
Request::WriteSingleRegister(addr, value) => future::ready(
48-
register_write(
49-
&mut self.holding_registers.lock().unwrap(),
50-
addr,
51-
std::slice::from_ref(&value),
52-
)
53-
.map(|_| Response::WriteSingleRegister(addr, value)),
54-
),
45+
.map(|_| Response::WriteMultipleRegisters(addr, values.len() as u16))
46+
}
47+
Request::WriteSingleRegister(addr, value) => register_write(
48+
&mut self.holding_registers.lock().unwrap(),
49+
addr,
50+
std::slice::from_ref(&value),
51+
)
52+
.map(|_| Response::WriteSingleRegister(addr, value)),
5553
_ => {
5654
println!("SERVER: Exception::IllegalFunction - Unimplemented function code in request: {req:?}");
57-
future::ready(Err(Exception::IllegalFunction))
55+
Err(Exception::IllegalFunction)
5856
}
59-
}
57+
};
58+
future::ready(res.map(Some))
6059
}
6160
}
6261

examples/tls-server.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,35 +90,34 @@ struct ExampleService {
9090

9191
impl tokio_modbus::server::Service for ExampleService {
9292
type Request = Request<'static>;
93-
type Future = future::Ready<Result<Response, Exception>>;
93+
type Future = future::Ready<Result<Option<Response>, Exception>>;
9494

9595
fn call(&self, req: Self::Request) -> Self::Future {
96-
match req {
97-
Request::ReadInputRegisters(addr, cnt) => future::ready(
96+
let res = match req {
97+
Request::ReadInputRegisters(addr, cnt) => {
9898
register_read(&self.input_registers.lock().unwrap(), addr, cnt)
99-
.map(Response::ReadInputRegisters),
100-
),
101-
Request::ReadHoldingRegisters(addr, cnt) => future::ready(
99+
.map(Response::ReadInputRegisters)
100+
}
101+
Request::ReadHoldingRegisters(addr, cnt) => {
102102
register_read(&self.holding_registers.lock().unwrap(), addr, cnt)
103-
.map(Response::ReadHoldingRegisters),
104-
),
105-
Request::WriteMultipleRegisters(addr, values) => future::ready(
103+
.map(Response::ReadHoldingRegisters)
104+
}
105+
Request::WriteMultipleRegisters(addr, values) => {
106106
register_write(&mut self.holding_registers.lock().unwrap(), addr, &values)
107-
.map(|_| Response::WriteMultipleRegisters(addr, values.len() as u16)),
108-
),
109-
Request::WriteSingleRegister(addr, value) => future::ready(
110-
register_write(
111-
&mut self.holding_registers.lock().unwrap(),
112-
addr,
113-
std::slice::from_ref(&value),
114-
)
115-
.map(|_| Response::WriteSingleRegister(addr, value)),
116-
),
107+
.map(|_| Response::WriteMultipleRegisters(addr, values.len() as u16))
108+
}
109+
Request::WriteSingleRegister(addr, value) => register_write(
110+
&mut self.holding_registers.lock().unwrap(),
111+
addr,
112+
std::slice::from_ref(&value),
113+
)
114+
.map(|_| Response::WriteSingleRegister(addr, value)),
117115
_ => {
118116
println!("SERVER: Exception::IllegalFunction - Unimplemented function code in request: {req:?}");
119-
future::ready(Err(Exception::IllegalFunction))
117+
Err(Exception::IllegalFunction)
120118
}
121-
}
119+
};
120+
future::ready(res.map(Some))
122121
}
123122
}
124123

src/frame/mod.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,12 +444,6 @@ impl From<ExceptionResponse> for ResponsePdu {
444444
}
445445
}
446446

447-
impl From<Result<Response, ExceptionResponse>> for ResponsePdu {
448-
fn from(from: Result<Response, ExceptionResponse>) -> Self {
449-
ResponsePdu(from.map(Into::into).map_err(Into::into))
450-
}
451-
}
452-
453447
#[cfg(any(
454448
feature = "rtu-over-tcp-server",
455449
feature = "rtu-server",
@@ -458,6 +452,21 @@ impl From<Result<Response, ExceptionResponse>> for ResponsePdu {
458452
#[derive(Debug, Clone, PartialEq, Eq)]
459453
pub(crate) struct OptionalResponsePdu(pub(crate) Option<ResponsePdu>);
460454

455+
#[cfg(any(
456+
feature = "rtu-over-tcp-server",
457+
feature = "rtu-server",
458+
feature = "tcp-server"
459+
))]
460+
impl From<Result<Option<Response>, ExceptionResponse>> for OptionalResponsePdu {
461+
fn from(from: Result<Option<Response>, ExceptionResponse>) -> Self {
462+
match from {
463+
Ok(None) => Self(None),
464+
Ok(Some(response)) => Self(Some(response.into())),
465+
Err(exception) => Self(Some(exception.into())),
466+
}
467+
}
468+
}
469+
461470
#[cfg(any(
462471
feature = "rtu-over-tcp-server",
463472
feature = "rtu-server",

src/server/rtu_over_tcp.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,10 @@ mod tests {
220220

221221
impl Service for DummyService {
222222
type Request = Request<'static>;
223-
type Future = future::Ready<Result<Response, Exception>>;
223+
type Future = future::Ready<Result<Option<Response>, Exception>>;
224224

225225
fn call(&self, _: Self::Request) -> Self::Future {
226-
future::ready(Ok(self.response.clone()))
226+
future::ready(Ok(Some(self.response.clone())))
227227
}
228228
}
229229

@@ -253,10 +253,10 @@ mod tests {
253253

254254
impl Service for DummyService {
255255
type Request = Request<'static>;
256-
type Future = future::Ready<Result<Response, Exception>>;
256+
type Future = future::Ready<Result<Option<Response>, Exception>>;
257257

258258
fn call(&self, _: Self::Request) -> Self::Future {
259-
future::ready(Ok(self.response.clone()))
259+
future::ready(Ok(Some(self.response.clone())))
260260
}
261261
}
262262

@@ -265,7 +265,7 @@ mod tests {
265265
};
266266

267267
let pdu = Request::ReadInputRegisters(0, 1);
268-
let rsp_adu = service.call(pdu).await.unwrap();
268+
let rsp_adu = service.call(pdu).await.unwrap().unwrap();
269269

270270
assert_eq!(rsp_adu, service.response);
271271
}

src/server/service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub trait Service {
99
type Request;
1010

1111
/// The future response value.
12-
type Future: Future<Output = Result<crate::Response, crate::Exception>> + Send;
12+
type Future: Future<Output = Result<Option<crate::Response>, crate::Exception>> + Send;
1313

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

src/server/tcp.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ mod tests {
217217

218218
impl Service for DummyService {
219219
type Request = Request<'static>;
220-
type Future = future::Ready<Result<Response, Exception>>;
220+
type Future = future::Ready<Result<Option<Response>, Exception>>;
221221

222222
fn call(&self, _: Self::Request) -> Self::Future {
223-
future::ready(Ok(self.response.clone()))
223+
future::ready(Ok(Some(self.response.clone())))
224224
}
225225
}
226226

@@ -250,10 +250,10 @@ mod tests {
250250

251251
impl Service for DummyService {
252252
type Request = Request<'static>;
253-
type Future = future::Ready<Result<Response, Exception>>;
253+
type Future = future::Ready<Result<Option<Response>, Exception>>;
254254

255255
fn call(&self, _: Self::Request) -> Self::Future {
256-
future::ready(Ok(self.response.clone()))
256+
future::ready(Ok(Some(self.response.clone())))
257257
}
258258
}
259259

@@ -262,7 +262,7 @@ mod tests {
262262
};
263263

264264
let pdu = Request::ReadInputRegisters(0, 1);
265-
let rsp_adu = service.call(pdu).await.unwrap();
265+
let rsp_adu = service.call(pdu).await.unwrap().unwrap();
266266

267267
assert_eq!(rsp_adu, service.response);
268268
}

tests/exception/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tokio_modbus::{
1212
pub struct TestService {}
1313

1414
impl TestService {
15-
fn handle(&self, req: Request<'static>) -> Result<Response, Exception> {
15+
fn handle(&self, req: Request<'static>) -> Result<Option<Response>, Exception> {
1616
use Request::*;
1717

1818
match req {
@@ -33,7 +33,7 @@ impl TestService {
3333
impl Service for TestService {
3434
type Request = Request<'static>;
3535

36-
type Future = future::Ready<Result<Response, Exception>>;
36+
type Future = future::Ready<Result<Option<Response>, Exception>>;
3737

3838
fn call(&self, req: Self::Request) -> Self::Future {
3939
future::ready(self.handle(req))

0 commit comments

Comments
 (0)