From 185668c1c182e0b7a33ca521c3cc0180118719ed Mon Sep 17 00:00:00 2001 From: mdmzfzl Date: Fri, 7 Aug 2026 16:33:28 -0700 Subject: [PATCH] fix(client): allow shared channel access --- examples/client/src/main.rs | 2 +- examples/perf/src/main.rs | 2 +- integration/tests/integration_test.rs | 2 +- rodbus-client/src/main.rs | 8 ++++---- rodbus/src/client/channel.rs | 18 +++++++++--------- rodbus/src/client/task.rs | 18 +++++++++--------- rodbus/src/lib.rs | 2 +- 7 files changed, 26 insertions(+), 26 deletions(-) diff --git a/examples/client/src/main.rs b/examples/client/src/main.rs index d917cac5..c334e2a9 100644 --- a/examples/client/src/main.rs +++ b/examples/client/src/main.rs @@ -176,7 +176,7 @@ fn print_write_result(result: Result) { } } -async fn run_channel(mut channel: Channel) -> Result<(), Box> { +async fn run_channel(channel: Channel) -> Result<(), Box> { channel.enable().await?; // ANCHOR: request_param diff --git a/examples/perf/src/main.rs b/examples/perf/src/main.rs index 48825a6e..e36c2a0c 100644 --- a/examples/perf/src/main.rs +++ b/examples/perf/src/main.rs @@ -126,7 +126,7 @@ async fn main() -> Result<(), Box> { let start = std::time::Instant::now(); // spawn tasks that make requests for the specified duration - for (mut channel, params) in channels { + for (channel, params) in channels { let handle: tokio::task::JoinHandle> = tokio::spawn(async move { let mut iterations = 0; diff --git a/integration/tests/integration_test.rs b/integration/tests/integration_test.rs index dc0cfeba..e1692c12 100644 --- a/integration/tests/integration_test.rs +++ b/integration/tests/integration_test.rs @@ -132,7 +132,7 @@ async fn test_requests_and_responses() { let (tx, mut rx) = tokio::sync::mpsc::channel(8); let listener = ClientStateListener { tx }; - let mut channel = spawn_tcp_client_task( + let channel = spawn_tcp_client_task( HostAddr::ip(addr.ip(), addr.port()), 10, default_retry_strategy(), diff --git a/rodbus-client/src/main.rs b/rodbus-client/src/main.rs index 9f38bdbf..f0e2d69f 100644 --- a/rodbus-client/src/main.rs +++ b/rodbus-client/src/main.rs @@ -275,16 +275,16 @@ async fn main() -> Result<(), Error> { async fn run() -> Result<(), Error> { let cli = Cli::parse(); - let (mut channel, command) = setup_channel(cli.mode).await?; + let (channel, command) = setup_channel(cli.mode).await?; let params = RequestParam::new(UnitId::new(cli.id), REQUEST_TIMEOUT); match cli.period { - None => run_command(&command, &mut channel, params).await, + None => run_command(&command, &channel, params).await, Some(period_ms) => { let period = Duration::from_millis(period_ms); loop { - run_command(&command, &mut channel, params).await?; + run_command(&command, &channel, params).await?; tokio::time::sleep(period).await } } @@ -367,7 +367,7 @@ async fn setup_serial(path: String, settings: ModeSerialSettings) -> Result Result<(), Error> { match command { diff --git a/rodbus/src/client/channel.rs b/rodbus/src/client/channel.rs index b11eae47..d66a5227 100644 --- a/rodbus/src/client/channel.rs +++ b/rodbus/src/client/channel.rs @@ -146,7 +146,7 @@ impl Channel { /// Read coils from the server pub async fn read_coils( - &mut self, + &self, param: RequestParam, range: AddressRange, ) -> Result>, RequestError> { @@ -161,7 +161,7 @@ impl Channel { /// Read discrete inputs from the server pub async fn read_discrete_inputs( - &mut self, + &self, param: RequestParam, range: AddressRange, ) -> Result>, RequestError> { @@ -176,7 +176,7 @@ impl Channel { /// Read holding registers from the server pub async fn read_holding_registers( - &mut self, + &self, param: RequestParam, range: AddressRange, ) -> Result>, RequestError> { @@ -194,7 +194,7 @@ impl Channel { /// Read input registers from the server pub async fn read_input_registers( - &mut self, + &self, param: RequestParam, range: AddressRange, ) -> Result>, RequestError> { @@ -212,7 +212,7 @@ impl Channel { /// Write a single coil on the server pub async fn write_single_coil( - &mut self, + &self, param: RequestParam, request: Indexed, ) -> Result, RequestError> { @@ -227,7 +227,7 @@ impl Channel { /// Write a single register on the server pub async fn write_single_register( - &mut self, + &self, param: RequestParam, request: Indexed, ) -> Result, RequestError> { @@ -242,7 +242,7 @@ impl Channel { /// Write multiple contiguous coils on the server pub async fn write_multiple_coils( - &mut self, + &self, param: RequestParam, request: WriteMultiple, ) -> Result { @@ -260,7 +260,7 @@ impl Channel { /// Write multiple contiguous registers on the server pub async fn write_multiple_registers( - &mut self, + &self, param: RequestParam, request: WriteMultiple, ) -> Result { @@ -277,7 +277,7 @@ impl Channel { } /// Dynamically change the protocol decoding level of the channel - pub async fn set_decode_level(&mut self, level: DecodeLevel) -> Result<(), Shutdown> { + pub async fn set_decode_level(&self, level: DecodeLevel) -> Result<(), Shutdown> { self.tx .send(Command::Setting(Setting::DecodeLevel(level))) .await?; diff --git a/rodbus/src/client/task.rs b/rodbus/src/client/task.rs index e0ea1fdb..4e0fbf93 100644 --- a/rodbus/src/client/task.rs +++ b/rodbus/src/client/task.rs @@ -432,7 +432,7 @@ mod tests { #[tokio::test] async fn returns_io_error_when_write_fails() { - let (mut channel, _task, mut io) = spawn_client_loop(); + let (channel, _task, mut io) = spawn_client_loop(); let error_kind = ErrorKind::ConnectionReset; @@ -452,7 +452,7 @@ mod tests { #[tokio::test] async fn returns_timeout_when_no_response() { - let (mut channel, _task, mut io) = spawn_client_loop(); + let (channel, _task, mut io) = spawn_client_loop(); // the expected request let range = AddressRange::try_from(7, 2).unwrap(); @@ -479,7 +479,7 @@ mod tests { #[tokio::test] async fn returns_shutdown_when_task_dropped() { - let (mut channel, task, mut io) = spawn_client_loop(); + let (channel, task, mut io) = spawn_client_loop(); // the expected request let range = AddressRange::try_from(7, 2).unwrap(); @@ -517,7 +517,7 @@ mod tests { #[tokio::test] async fn transmit_read_coils_when_requested() { - let (mut channel, _task, mut io) = spawn_client_loop(); + let (channel, _task, mut io) = spawn_client_loop(); let range = AddressRange::try_from(7, 2).unwrap(); let request = get_framed_adu(FunctionCode::ReadCoils, &range); @@ -558,7 +558,7 @@ mod tests { // spawn 3 requests that will all timeout for _ in 0..3 { - let mut ch = channel.clone(); + let ch = channel.clone(); tokio::spawn(async move { ch.read_coils( RequestParam::new(UnitId::new(1), Duration::from_secs(1)), @@ -588,7 +588,7 @@ mod tests { // send 10 requests that all timeout for _ in 0..10 { - let mut ch = channel.clone(); + let ch = channel.clone(); tokio::spawn(async move { ch.read_coils( RequestParam::new(UnitId::new(1), Duration::from_secs(1)), @@ -620,7 +620,7 @@ mod tests { // First two timeouts for _ in 0..2 { - let mut ch = channel.clone(); + let ch = channel.clone(); tokio::spawn(async move { ch.read_coils( RequestParam::new(UnitId::new(1), Duration::from_secs(1)), @@ -636,7 +636,7 @@ mod tests { // Successful request let success_task = tokio::spawn({ - let mut ch = channel.clone(); + let ch = channel.clone(); async move { ch.read_coils( RequestParam::new(UnitId::new(1), Duration::from_secs(1)), @@ -675,7 +675,7 @@ mod tests { // Two more timeouts - should NOT terminate since counter was reset for _ in 0..2 { - let mut ch = channel.clone(); + let ch = channel.clone(); tokio::spawn(async move { ch.read_coils( RequestParam::new(UnitId::new(1), Duration::from_secs(1)), diff --git a/rodbus/src/lib.rs b/rodbus/src/lib.rs index 692dda88..44dc144a 100644 --- a/rodbus/src/lib.rs +++ b/rodbus/src/lib.rs @@ -15,7 +15,7 @@ //!#[tokio::main(flavor = "multi_thread")] //!async fn main() -> Result<(), Box> { //! -//! let mut channel = spawn_tcp_client_task( +//! let channel = spawn_tcp_client_task( //! HostAddr::ip("127.0.0.1".parse()?, 502), //! 10, //! default_retry_strategy(),