From 8a849ce0094ba81b5454da126c7cc0460d7ec4b1 Mon Sep 17 00:00:00 2001 From: Claas Date: Tue, 22 Oct 2024 00:58:08 +0200 Subject: [PATCH] Fix function name not matching trait and potential ambiguity because of that --- fan-controller/src/main.rs | 6 +++--- fan-controller/src/mqtt/client.rs | 2 +- fan-controller/src/mqtt/mod.rs | 8 ++++---- fan-controller/src/mqtt/packet/connect.rs | 2 +- fan-controller/src/mqtt/packet/disconnect.rs | 2 +- fan-controller/src/mqtt/packet/mod.rs | 2 +- fan-controller/src/mqtt/packet/publish.rs | 2 +- fan-controller/src/mqtt/packet/subscribe.rs | 2 +- fan-controller/src/mqtt/task.rs | 4 ++-- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/fan-controller/src/main.rs b/fan-controller/src/main.rs index f99df10..29cb7ee 100644 --- a/fan-controller/src/main.rs +++ b/fan-controller/src/main.rs @@ -488,7 +488,7 @@ async fn mqtt_task( } PingResponse::TYPE => { info!("Received ping response"); - let ping_response = match PingResponse::decode( + let ping_response = match PingResponse::try_decode( &parts.variable_header_and_payload, ) { Ok(response) => response, @@ -503,7 +503,7 @@ async fn mqtt_task( Disconnect::TYPE => { info!("Received disconnect"); - let disconnect = Disconnect::decode(&parts.variable_header_and_payload); + let disconnect = Disconnect::try_decode(&parts.variable_header_and_payload); info!("Disconnect {:?}", disconnect); //TODO disconnect TCP connection } @@ -706,7 +706,7 @@ async fn send_discovery_and_keep_alive( loop { keep_alive.next().await; // Send keep alive ping request - let _ = PingRequest.encode(&mut send_buffer, &mut offset); + let _ = PingRequest.try_encode(&mut send_buffer, &mut offset); writer .write_all(&send_buffer[..offset]) .await diff --git a/fan-controller/src/mqtt/client.rs b/fan-controller/src/mqtt/client.rs index 58d748d..e7da899 100644 --- a/fan-controller/src/mqtt/client.rs +++ b/fan-controller/src/mqtt/client.rs @@ -357,7 +357,7 @@ pub(crate) mod runner { let mut offset = 0; if let Err(error) = - packet.encode(&mut self.send_buffer, &mut offset) + packet.try_encode(&mut self.send_buffer, &mut offset) { //TODO handle error warn!("Error encoding subscribe packet: {:?}", error); diff --git a/fan-controller/src/mqtt/mod.rs b/fan-controller/src/mqtt/mod.rs index 9c6de26..5aecff1 100644 --- a/fan-controller/src/mqtt/mod.rs +++ b/fan-controller/src/mqtt/mod.rs @@ -68,13 +68,13 @@ pub(crate) trait Encode { pub(crate) trait TryEncode { type Error; - fn encode(&self, buffer: &mut [u8], offset: &mut usize) -> Result<(), Self::Error>; + fn try_encode(&self, buffer: &mut [u8], offset: &mut usize) -> Result<(), Self::Error>; } impl TryEncode for T { type Error = Infallible; - fn encode(&self, buffer: &mut [u8], offset: &mut usize) -> Result<(), Self::Error> { + fn try_encode(&self, buffer: &mut [u8], offset: &mut usize) -> Result<(), Self::Error> { self.encode(buffer, offset); Ok(()) } @@ -88,7 +88,7 @@ pub(crate) trait Decode { pub(crate) trait TryDecode { type Error; - fn decode(variable_header_and_payload: &[u8]) -> Result + fn try_decode(variable_header_and_payload: &[u8]) -> Result where Self: Sized; } @@ -96,7 +96,7 @@ pub(crate) trait TryDecode { impl TryDecode for T { type Error = Infallible; - fn decode(variable_header_and_payload: &[u8]) -> Result + fn try_decode(variable_header_and_payload: &[u8]) -> Result where Self: Sized, { diff --git a/fan-controller/src/mqtt/packet/connect.rs b/fan-controller/src/mqtt/packet/connect.rs index 71189df..86520f7 100644 --- a/fan-controller/src/mqtt/packet/connect.rs +++ b/fan-controller/src/mqtt/packet/connect.rs @@ -113,7 +113,7 @@ impl<'a> Connect<'a> { impl TryEncode for Connect<'_> { type Error = EncodeError; - fn encode(&self, buffer: &mut [u8], offset: &mut usize) -> Result<(), Self::Error> { + fn try_encode(&self, buffer: &mut [u8], offset: &mut usize) -> Result<(), Self::Error> { let remaining_length = 11 + size_of::() + self.client_identifier.len() diff --git a/fan-controller/src/mqtt/packet/disconnect.rs b/fan-controller/src/mqtt/packet/disconnect.rs index b08292e..e0d3f80 100644 --- a/fan-controller/src/mqtt/packet/disconnect.rs +++ b/fan-controller/src/mqtt/packet/disconnect.rs @@ -93,7 +93,7 @@ pub(crate) enum DecodeDisconnectError { impl TryDecode for Disconnect { type Error = DecodeDisconnectError; - fn decode(variable_header_and_payload: &[u8]) -> Result { + fn try_decode(variable_header_and_payload: &[u8]) -> Result { // Variable header // Disconnect reason code let reason_code = variable_header_and_payload[0]; diff --git a/fan-controller/src/mqtt/packet/mod.rs b/fan-controller/src/mqtt/packet/mod.rs index 59897d8..c4efb00 100644 --- a/fan-controller/src/mqtt/packet/mod.rs +++ b/fan-controller/src/mqtt/packet/mod.rs @@ -98,7 +98,7 @@ where Connect::TYPE => Err(ReadError::UnsupportedPacketType(parts.r#type)), ConnectAcknowledgement::TYPE => { let connect_acknowledgement = - ConnectAcknowledgement::decode(parts.variable_header_and_payload) + ConnectAcknowledgement::try_decode(parts.variable_header_and_payload) .map_err(ReadError::ConnectAcknowledgementError)?; Ok(Packet::ConnectAcknowledgement(connect_acknowledgement)) diff --git a/fan-controller/src/mqtt/packet/publish.rs b/fan-controller/src/mqtt/packet/publish.rs index 1db0b64..2b7b64d 100644 --- a/fan-controller/src/mqtt/packet/publish.rs +++ b/fan-controller/src/mqtt/packet/publish.rs @@ -142,7 +142,7 @@ impl<'a> Publish<'a> { impl TryEncode for Publish<'_> { type Error = EncodeError; - fn encode(&self, buffer: &mut [u8], offset: &mut usize) -> Result<(), Self::Error> { + fn try_encode(&self, buffer: &mut [u8], offset: &mut usize) -> Result<(), Self::Error> { // Fixed header //TODO set flags buffer[*offset] = Self::TYPE << 4; diff --git a/fan-controller/src/mqtt/packet/subscribe.rs b/fan-controller/src/mqtt/packet/subscribe.rs index 2ce5b2e..f9b9c25 100644 --- a/fan-controller/src/mqtt/packet/subscribe.rs +++ b/fan-controller/src/mqtt/packet/subscribe.rs @@ -92,7 +92,7 @@ impl TryEncode for Subscribe<'_> { type Error = EncodeError; // https://www.emqx.com/en/blog/mqtt-5-0-control-packets-03-subscribe-unsubscribe - fn encode(&self, buffer: &mut [u8], offset: &mut usize) -> Result<(), Self::Error> { + fn try_encode(&self, buffer: &mut [u8], offset: &mut usize) -> Result<(), Self::Error> { // 82 0a 05 be 00 00 04 64 65 6d 6f 02 // let test_packet = &[ // 0x82, 0x0a, 0x05, 0xbe, 0x00, 0x00, 0x04, 0x64, 0x65, 0x6d, 0x6f, 0x02, diff --git a/fan-controller/src/mqtt/task.rs b/fan-controller/src/mqtt/task.rs index bda6b98..571745e 100644 --- a/fan-controller/src/mqtt/task.rs +++ b/fan-controller/src/mqtt/task.rs @@ -30,7 +30,7 @@ where let mut offset = 0; let mut send_buffer = [0; 256]; packet - .encode(&mut send_buffer, &mut offset) + .try_encode(&mut send_buffer, &mut offset) .map_err(SendError::EncodeError)?; socket @@ -81,7 +81,7 @@ pub(crate) async fn connect<'a, 'b>( info!("Connect acknowledgement packet received"); - let acknowledgement = ConnectAcknowledgement::decode(parts.variable_header_and_payload) + let acknowledgement = ConnectAcknowledgement::try_decode(parts.variable_header_and_payload) .map_err(ConnectError::DecodeAcknowledgementError)?; info!("Connect acknowledgement read"); -- 2.51.2