diff --git a/mqtt-format/src/v5/bytes.rs b/mqtt-format/src/v5/bytes.rs index d347bb3a..49ef64b3 100644 --- a/mqtt-format/src/v5/bytes.rs +++ b/mqtt-format/src/v5/bytes.rs @@ -20,14 +20,14 @@ pub fn parse_binary_data<'i>(input: &mut &'i Bytes) -> MResult<&'i [u8]> { .parse_next(input) } -pub async fn write_binary_data(buffer: &mut W, slice: &[u8]) -> WResult { +pub fn write_binary_data(buffer: &mut W, slice: &[u8]) -> WResult { let slice_len = slice .len() .try_into() .map_err(|_| W::Error::from(super::write::MqttWriteError::Invariant))?; - buffer.write_u16(slice_len).await?; - buffer.write_slice(slice).await + buffer.write_u16(slice_len)?; + buffer.write_slice(slice) } #[inline] @@ -53,12 +53,12 @@ mod tests { ); } - #[tokio::test] - async fn test_write_binary_data() { + #[test] + fn test_write_binary_data() { let mut writer = TestWriter { buffer: Vec::new() }; let data = &[0xFF, 0xAB, 0x42, 0x13, 0x37, 0x69]; - write_binary_data(&mut writer, data).await.unwrap(); + write_binary_data(&mut writer, data).unwrap(); let out = parse_binary_data(&mut Bytes::new(&writer.buffer)).unwrap(); assert_eq!(out, data); diff --git a/mqtt-format/src/v5/fixed_header.rs b/mqtt-format/src/v5/fixed_header.rs index 31c4a23f..84830ce0 100644 --- a/mqtt-format/src/v5/fixed_header.rs +++ b/mqtt-format/src/v5/fixed_header.rs @@ -104,7 +104,7 @@ impl MFixedHeader { Ok(MFixedHeader { packet_type }) } - pub async fn write(&self, buffer: &mut W) -> WResult { + pub fn write(&self, buffer: &mut W) -> WResult { #[allow(clippy::identity_op)] let byte = match self.packet_type { PacketType::Connect => (1 << 4) | 0, @@ -135,7 +135,7 @@ impl MFixedHeader { PacketType::Auth => (15 << 4) | 0, }; - buffer.write_byte(byte).await + buffer.write_byte(byte) } } diff --git a/mqtt-format/src/v5/integers.rs b/mqtt-format/src/v5/integers.rs index 3228986e..42e7cc27 100644 --- a/mqtt-format/src/v5/integers.rs +++ b/mqtt-format/src/v5/integers.rs @@ -28,8 +28,8 @@ pub fn parse_u16(input: &mut &Bytes) -> MResult { .parse_next(input) } -pub async fn write_u16(buffer: &mut W, u: u16) -> WResult { - buffer.write_u16(u.to_be()).await?; +pub fn write_u16(buffer: &mut W, u: u16) -> WResult { + buffer.write_u16(u.to_be())?; Ok(()) } @@ -45,8 +45,8 @@ pub fn parse_u32(input: &mut &Bytes) -> MResult { .parse_next(input) } -pub async fn write_u32(buffer: &mut W, u: u32) -> WResult { - buffer.write_u32(u).await?; +pub fn write_u32(buffer: &mut W, u: u32) -> WResult { + buffer.write_u32(u)?; Ok(()) } @@ -86,25 +86,25 @@ pub const fn variable_u32_binary_size(u: u32) -> u32 { } } -pub async fn write_variable_u32(buffer: &mut W, u: u32) -> WResult { +pub fn write_variable_u32(buffer: &mut W, u: u32) -> WResult { match u { 0..=127 => { - buffer.write_byte(u as u8).await?; + buffer.write_byte(u as u8)?; } len @ 128..=16383 => { let first = (len % 128) | 0b1000_0000; let second = len / 128; - buffer.write_byte(first as u8).await?; - buffer.write_byte(second as u8).await?; + buffer.write_byte(first as u8)?; + buffer.write_byte(second as u8)?; } len @ 16384..=2_097_151 => { let first = (len % 128) | 0b1000_0000; let second = ((len / 128) % 128) | 0b1000_0000; let third = len / (128 * 128); - buffer.write_byte(first as u8).await?; - buffer.write_byte(second as u8).await?; - buffer.write_byte(third as u8).await?; + buffer.write_byte(first as u8)?; + buffer.write_byte(second as u8)?; + buffer.write_byte(third as u8)?; } len @ 2_097_152..=268_435_455 => { let first = (len % 128) | 0b1000_0000; @@ -112,10 +112,10 @@ pub async fn write_variable_u32(buffer: &mut W, u: u32) -> W let third = ((len / (128 * 128)) % 128) | 0b1000_0000; let fourth = len / (128 * 128 * 128); - buffer.write_byte(first as u8).await?; - buffer.write_byte(second as u8).await?; - buffer.write_byte(third as u8).await?; - buffer.write_byte(fourth as u8).await?; + buffer.write_byte(first as u8)?; + buffer.write_byte(second as u8)?; + buffer.write_byte(third as u8)?; + buffer.write_byte(fourth as u8)?; } _size => { return Err(super::write::MqttWriteError::Invariant.into()); @@ -183,53 +183,53 @@ mod tests { parse_variable_u32(&mut Bytes::new(&input)).unwrap_err(); } - #[tokio::test] - async fn test_write_byte() { + #[test] + fn test_write_byte() { let mut writer = TestWriter { buffer: Vec::new() }; - writer.write_byte(1).await.unwrap(); + writer.write_byte(1).unwrap(); assert_eq!(writer.buffer, &[1]); } - #[tokio::test] - async fn test_write_two_bytes() { + #[test] + fn test_write_two_bytes() { let mut writer = TestWriter { buffer: Vec::new() }; - writer.write_u16(1).await.unwrap(); + writer.write_u16(1).unwrap(); assert_eq!(writer.buffer, &[0x00, 0x01]); } - #[tokio::test] - async fn test_write_four_bytes() { + #[test] + fn test_write_four_bytes() { let mut writer = TestWriter { buffer: Vec::new() }; - writer.write_u32(1).await.unwrap(); + writer.write_u32(1).unwrap(); assert_eq!(writer.buffer, &[0x00, 0x00, 0x00, 0x01]); } - #[tokio::test] - async fn test_write_u32() { + #[test] + fn test_write_u32() { // step by some prime number for i in (0..268_435_455).step_by(271) { let mut writer = TestWriter { buffer: Vec::new() }; - writer.write_u32(i).await.unwrap(); + writer.write_u32(i).unwrap(); let out = parse_u32(&mut Bytes::new(&writer.buffer)).unwrap(); assert_eq!(out, i); } } - #[tokio::test] - async fn test_write_variable_u32() { + #[test] + fn test_write_variable_u32() { // step by some prime number for i in (0..268_435_455).step_by(271) { let mut writer = TestWriter { buffer: Vec::new() }; - write_variable_u32(&mut writer, i).await.unwrap(); + write_variable_u32(&mut writer, i).unwrap(); let out = parse_variable_u32(&mut Bytes::new(&writer.buffer)).unwrap(); assert_eq!(out, i); diff --git a/mqtt-format/src/v5/packets/auth.rs b/mqtt-format/src/v5/packets/auth.rs index f152086a..e5651d5a 100644 --- a/mqtt-format/src/v5/packets/auth.rs +++ b/mqtt-format/src/v5/packets/auth.rs @@ -64,9 +64,9 @@ impl<'i> MAuth<'i> { self.reason.binary_size() + self.properties.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { - self.reason.write(buffer).await?; - self.properties.write(buffer).await + pub fn write(&self, buffer: &mut W) -> WResult { + self.reason.write(buffer)?; + self.properties.write(buffer) } } @@ -80,8 +80,8 @@ mod test { use crate::v5::variable_header::ReasonString; use crate::v5::variable_header::UserProperties; - #[tokio::test] - async fn test_roundtrip_mauth_no_props() { + #[test] + fn test_roundtrip_mauth_no_props() { crate::v5::test::make_roundtrip_test!(MAuth { reason: AuthReasonCode::ContinueAuthentication, properties: AuthProperties { @@ -93,8 +93,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_mauth_props() { + #[test] + fn test_roundtrip_mauth_props() { crate::v5::test::make_roundtrip_test!(MAuth { reason: AuthReasonCode::ContinueAuthentication, properties: AuthProperties { diff --git a/mqtt-format/src/v5/packets/connack.rs b/mqtt-format/src/v5/packets/connack.rs index 7a67c3ca..ab98137e 100644 --- a/mqtt-format/src/v5/packets/connack.rs +++ b/mqtt-format/src/v5/packets/connack.rs @@ -155,11 +155,11 @@ impl<'i> MConnack<'i> { + self.properties.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { + pub fn write(&self, buffer: &mut W) -> WResult { let byte = (self.session_present as u8) << 7; - buffer.write_byte(byte).await?; - self.reason_code.write(buffer).await?; - self.properties.write(buffer).await + buffer.write_byte(byte)?; + self.reason_code.write(buffer)?; + self.properties.write(buffer) } } @@ -186,8 +186,8 @@ mod test { use crate::v5::variable_header::UserProperties; use crate::v5::variable_header::WildcardSubscriptionAvailable; - #[tokio::test] - async fn test_roundtrip_connack_no_props() { + #[test] + fn test_roundtrip_connack_no_props() { crate::v5::test::make_roundtrip_test!(MConnack { session_present: true, reason_code: ConnackReasonCode::Success, @@ -213,8 +213,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_connack_with_props() { + #[test] + fn test_roundtrip_connack_with_props() { crate::v5::test::make_roundtrip_test!(MConnack { session_present: true, reason_code: ConnackReasonCode::Success, diff --git a/mqtt-format/src/v5/packets/connect.rs b/mqtt-format/src/v5/packets/connect.rs index 86931989..a767b5bd 100644 --- a/mqtt-format/src/v5/packets/connect.rs +++ b/mqtt-format/src/v5/packets/connect.rs @@ -177,9 +177,9 @@ impl<'i> MConnect<'i> { + self.password.as_ref().map(|p| crate::v5::bytes::binary_data_binary_size(p)).unwrap_or(0) } - pub async fn write(&self, buffer: &mut W) -> WResult { - crate::v5::strings::write_string(buffer, "MQTT").await?; - ProtocolLevel::V5.write(buffer).await?; + pub fn write(&self, buffer: &mut W) -> WResult { + crate::v5::strings::write_string(buffer, "MQTT")?; + ProtocolLevel::V5.write(buffer)?; let flags = { let reserved = 0; @@ -203,18 +203,18 @@ impl<'i> MConnect<'i> { reserved | clean_start | will | password | username }; - buffer.write_byte(flags).await?; - buffer.write_u16(self.keep_alive).await?; - self.properties.write(buffer).await?; - crate::v5::strings::write_string(buffer, self.client_identifier).await?; + buffer.write_byte(flags)?; + buffer.write_u16(self.keep_alive)?; + self.properties.write(buffer)?; + crate::v5::strings::write_string(buffer, self.client_identifier)?; if let Some(will) = self.will.as_ref() { - will.write(buffer).await?; + will.write(buffer)?; } if let Some(username) = self.username.as_ref() { - crate::v5::strings::write_string(buffer, username).await?; + crate::v5::strings::write_string(buffer, username)?; } if let Some(password) = self.password.as_ref() { - crate::v5::bytes::write_binary_data(buffer, password).await?; + crate::v5::bytes::write_binary_data(buffer, password)?; } Ok(()) @@ -231,10 +231,10 @@ pub struct Will<'i> { } impl<'i> Will<'i> { - pub async fn write(&self, buffer: &mut W) -> WResult { - self.properties.write(buffer).await?; - crate::v5::strings::write_string(buffer, self.topic).await?; - crate::v5::bytes::write_binary_data(buffer, self.payload).await?; + pub fn write(&self, buffer: &mut W) -> WResult { + self.properties.write(buffer)?; + crate::v5::strings::write_string(buffer, self.topic)?; + crate::v5::bytes::write_binary_data(buffer, self.payload)?; Ok(()) } @@ -274,10 +274,10 @@ impl ProtocolLevel { } } - pub async fn write(&self, buffer: &mut W) -> WResult { + pub fn write(&self, buffer: &mut W) -> WResult { match self { - ProtocolLevel::V3 => buffer.write_byte(3).await, - ProtocolLevel::V5 => buffer.write_byte(5).await, + ProtocolLevel::V3 => buffer.write_byte(3), + ProtocolLevel::V5 => buffer.write_byte(5), } } } @@ -304,8 +304,8 @@ mod test { use crate::v5::variable_header::UserProperties; use crate::v5::variable_header::WillDelayInterval; - #[tokio::test] - async fn test_roundtrip_connect_empty() { + #[test] + fn test_roundtrip_connect_empty() { crate::v5::test::make_roundtrip_test!(MConnect { client_identifier: "i am so cool", username: None, @@ -327,8 +327,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_connect_no_props() { + #[test] + fn test_roundtrip_connect_no_props() { crate::v5::test::make_roundtrip_test!(MConnect { client_identifier: "i am so cool", username: Some("user"), @@ -363,8 +363,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_connect_with_props() { + #[test] + fn test_roundtrip_connect_with_props() { crate::v5::test::make_roundtrip_test!(MConnect { client_identifier: "i am so cool", username: Some("user"), diff --git a/mqtt-format/src/v5/packets/disconnect.rs b/mqtt-format/src/v5/packets/disconnect.rs index 376e99f7..0a585456 100644 --- a/mqtt-format/src/v5/packets/disconnect.rs +++ b/mqtt-format/src/v5/packets/disconnect.rs @@ -95,9 +95,9 @@ impl<'i> MDisconnect<'i> { self.reason_code.binary_size() + self.properties.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { - self.reason_code.write(buffer).await?; - self.properties.write(buffer).await + pub fn write(&self, buffer: &mut W) -> WResult { + self.reason_code.write(buffer)?; + self.properties.write(buffer) } } @@ -111,8 +111,8 @@ mod test { use crate::v5::variable_header::SessionExpiryInterval; use crate::v5::variable_header::UserProperties; - #[tokio::test] - async fn test_roundtrip_disconnect_no_props() { + #[test] + fn test_roundtrip_disconnect_no_props() { crate::v5::test::make_roundtrip_test!(MDisconnect { reason_code: DisconnectReasonCode::NormalDisconnection, properties: DisconnectProperties { @@ -124,8 +124,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_disconnect_with_props() { + #[test] + fn test_roundtrip_disconnect_with_props() { crate::v5::test::make_roundtrip_test!(MDisconnect { reason_code: DisconnectReasonCode::NormalDisconnection, properties: DisconnectProperties { diff --git a/mqtt-format/src/v5/packets/mod.rs b/mqtt-format/src/v5/packets/mod.rs index 8bb23458..bd4be9ac 100644 --- a/mqtt-format/src/v5/packets/mod.rs +++ b/mqtt-format/src/v5/packets/mod.rs @@ -107,71 +107,71 @@ impl<'i> MqttPacket<'i> { Self::parse(&mut Bytes::new(input)) } - pub async fn write(&self, buffer: &mut W) -> WResult { + pub fn write(&self, buffer: &mut W) -> WResult { match self { MqttPacket::Auth(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Auth, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Connack(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Connack, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Connect(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Connect, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Disconnect(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Disconnect, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Pingreq(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Pingreq, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Pingresp(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Pingresp, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Puback(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Puback, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Pubcomp(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Pubcomp, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Publish(p) => { let fixed_header = MFixedHeader { @@ -181,57 +181,57 @@ impl<'i> MqttPacket<'i> { retain: p.retain, }, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Pubrec(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Pubrec, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Pubrel(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Pubrel, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Suback(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Suback, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Subscribe(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Subscribe, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Unsuback(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Unsuback, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } MqttPacket::Unsubscribe(p) => { let fixed_header = MFixedHeader { packet_type: PacketType::Unsubscribe, }; - fixed_header.write(buffer).await?; - crate::v5::integers::write_variable_u32(buffer, p.binary_size()).await?; - p.write(buffer).await?; + fixed_header.write(buffer)?; + crate::v5::integers::write_variable_u32(buffer, p.binary_size())?; + p.write(buffer)?; } } diff --git a/mqtt-format/src/v5/packets/pingreq.rs b/mqtt-format/src/v5/packets/pingreq.rs index 346b42cc..08c97fc6 100644 --- a/mqtt-format/src/v5/packets/pingreq.rs +++ b/mqtt-format/src/v5/packets/pingreq.rs @@ -26,7 +26,7 @@ impl MPingreq { 0 } - pub async fn write(&self, _buffer: &mut W) -> WResult { + pub fn write(&self, _buffer: &mut W) -> WResult { Ok(()) } } @@ -35,11 +35,11 @@ impl MPingreq { mod test { use crate::v5::packets::pingreq::MPingreq; - #[tokio::test] - async fn test_roundtrip_pingreq() { + #[test] + fn test_roundtrip_pingreq() { let mut writer = crate::v5::test::TestWriter { buffer: Vec::new() }; let instance = MPingreq; - instance.write(&mut writer).await.unwrap(); + instance.write(&mut writer).unwrap(); let output = MPingreq::parse(&mut winnow::Bytes::new(&writer.buffer)).unwrap(); assert_eq!(instance, output); } diff --git a/mqtt-format/src/v5/packets/pingresp.rs b/mqtt-format/src/v5/packets/pingresp.rs index fcede20e..5f950edd 100644 --- a/mqtt-format/src/v5/packets/pingresp.rs +++ b/mqtt-format/src/v5/packets/pingresp.rs @@ -26,7 +26,7 @@ impl MPingresp { 0 } - pub async fn write(&self, _buffer: &mut W) -> WResult { + pub fn write(&self, _buffer: &mut W) -> WResult { Ok(()) } } @@ -35,11 +35,11 @@ impl MPingresp { mod test { use crate::v5::packets::pingresp::MPingresp; - #[tokio::test] - async fn test_roundtrip_pingresp() { + #[test] + fn test_roundtrip_pingresp() { let mut writer = crate::v5::test::TestWriter { buffer: Vec::new() }; let instance = MPingresp; - instance.write(&mut writer).await.unwrap(); + instance.write(&mut writer).unwrap(); let output = MPingresp::parse(&mut winnow::Bytes::new(&writer.buffer)).unwrap(); assert_eq!(instance, output); } diff --git a/mqtt-format/src/v5/packets/puback.rs b/mqtt-format/src/v5/packets/puback.rs index d1f28d79..3234a37b 100644 --- a/mqtt-format/src/v5/packets/puback.rs +++ b/mqtt-format/src/v5/packets/puback.rs @@ -80,10 +80,10 @@ impl<'i> MPuback<'i> { + self.properties.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { - self.packet_identifier.write(buffer).await?; - self.reason.write(buffer).await?; - self.properties.write(buffer).await + pub fn write(&self, buffer: &mut W) -> WResult { + self.packet_identifier.write(buffer)?; + self.reason.write(buffer)?; + self.properties.write(buffer) } } @@ -96,8 +96,8 @@ mod test { use crate::v5::variable_header::ReasonString; use crate::v5::variable_header::UserProperties; - #[tokio::test] - async fn test_roundtrip_puback_no_props() { + #[test] + fn test_roundtrip_puback_no_props() { crate::v5::test::make_roundtrip_test!(MPuback { packet_identifier: PacketIdentifier(123), reason: PubackReasonCode::Success, @@ -108,8 +108,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_puback_with_props() { + #[test] + fn test_roundtrip_puback_with_props() { crate::v5::test::make_roundtrip_test!(MPuback { packet_identifier: PacketIdentifier(123), reason: PubackReasonCode::Success, diff --git a/mqtt-format/src/v5/packets/pubcomp.rs b/mqtt-format/src/v5/packets/pubcomp.rs index 403b12e7..e8c37821 100644 --- a/mqtt-format/src/v5/packets/pubcomp.rs +++ b/mqtt-format/src/v5/packets/pubcomp.rs @@ -63,10 +63,10 @@ impl<'i> MPubcomp<'i> { + self.properties.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { - self.packet_identifier.write(buffer).await?; - self.reason.write(buffer).await?; - self.properties.write(buffer).await + pub fn write(&self, buffer: &mut W) -> WResult { + self.packet_identifier.write(buffer)?; + self.reason.write(buffer)?; + self.properties.write(buffer) } } @@ -79,8 +79,8 @@ mod test { use crate::v5::variable_header::ReasonString; use crate::v5::variable_header::UserProperties; - #[tokio::test] - async fn test_roundtrip_pubcomp_no_props() { + #[test] + fn test_roundtrip_pubcomp_no_props() { crate::v5::test::make_roundtrip_test!(MPubcomp { packet_identifier: PacketIdentifier(123), reason: PubcompReasonCode::Success, @@ -91,8 +91,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_puback_with_props() { + #[test] + fn test_roundtrip_puback_with_props() { crate::v5::test::make_roundtrip_test!(MPubcomp { packet_identifier: PacketIdentifier(123), reason: PubcompReasonCode::Success, diff --git a/mqtt-format/src/v5/packets/publish.rs b/mqtt-format/src/v5/packets/publish.rs index e2ba1a32..205dede7 100644 --- a/mqtt-format/src/v5/packets/publish.rs +++ b/mqtt-format/src/v5/packets/publish.rs @@ -108,12 +108,12 @@ impl<'i> MPublish<'i> { + crate::v5::bytes::binary_data_binary_size(self.payload) } - pub async fn write(&self, buffer: &mut W) -> WResult { - write_string(buffer, self.topic_name).await?; - self.packet_identifier.write(buffer).await?; - self.properties.write(buffer).await?; + pub fn write(&self, buffer: &mut W) -> WResult { + write_string(buffer, self.topic_name)?; + self.packet_identifier.write(buffer)?; + self.properties.write(buffer)?; - buffer.write_slice(self.payload).await + buffer.write_slice(self.payload) } } @@ -128,8 +128,8 @@ mod test { use crate::v5::packets::publish::PublishProperties; use crate::v5::variable_header::PacketIdentifier; - #[tokio::test] - async fn test_roundtrip_pubcomp_no_props() { + #[test] + fn test_roundtrip_pubcomp_no_props() { let mut writer = crate::v5::test::TestWriter { buffer: Vec::new() }; let duplicate = true; @@ -154,7 +154,7 @@ mod test { }, payload: &[0x12, 0x34], }; - instance.write(&mut writer).await.unwrap(); + instance.write(&mut writer).unwrap(); let output = MPublish::parse( duplicate, quality_of_service, @@ -165,8 +165,8 @@ mod test { assert_eq!(instance, output); } - #[tokio::test] - async fn test_roundtrip_puback_with_props() { + #[test] + fn test_roundtrip_puback_with_props() { let mut writer = crate::v5::test::TestWriter { buffer: Vec::new() }; let duplicate = true; let quality_of_service = QualityOfService::ExactlyOnce; @@ -190,7 +190,7 @@ mod test { }, payload: &[0x12, 0x34], }; - instance.write(&mut writer).await.unwrap(); + instance.write(&mut writer).unwrap(); let output = MPublish::parse( duplicate, quality_of_service, diff --git a/mqtt-format/src/v5/packets/pubrec.rs b/mqtt-format/src/v5/packets/pubrec.rs index c02f21ab..00cd8903 100644 --- a/mqtt-format/src/v5/packets/pubrec.rs +++ b/mqtt-format/src/v5/packets/pubrec.rs @@ -71,10 +71,10 @@ impl<'i> MPubrec<'i> { + self.properties.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { - self.packet_identifier.write(buffer).await?; - self.reason.write(buffer).await?; - self.properties.write(buffer).await + pub fn write(&self, buffer: &mut W) -> WResult { + self.packet_identifier.write(buffer)?; + self.reason.write(buffer)?; + self.properties.write(buffer) } } @@ -87,8 +87,8 @@ mod test { use crate::v5::variable_header::ReasonString; use crate::v5::variable_header::UserProperties; - #[tokio::test] - async fn test_roundtrip_mauth_no_props() { + #[test] + fn test_roundtrip_mauth_no_props() { crate::v5::test::make_roundtrip_test!(MPubrec { packet_identifier: PacketIdentifier(123), reason: PubrecReasonCode::Success, @@ -99,8 +99,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_mauth_props() { + #[test] + fn test_roundtrip_mauth_props() { crate::v5::test::make_roundtrip_test!(MPubrec { packet_identifier: PacketIdentifier(123), reason: PubrecReasonCode::Success, diff --git a/mqtt-format/src/v5/packets/pubrel.rs b/mqtt-format/src/v5/packets/pubrel.rs index fb96068b..4fd54424 100644 --- a/mqtt-format/src/v5/packets/pubrel.rs +++ b/mqtt-format/src/v5/packets/pubrel.rs @@ -73,10 +73,10 @@ impl<'i> MPubrel<'i> { + self.properties.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { - self.packet_identifier.write(buffer).await?; - self.reason.write(buffer).await?; - self.properties.write(buffer).await + pub fn write(&self, buffer: &mut W) -> WResult { + self.packet_identifier.write(buffer)?; + self.reason.write(buffer)?; + self.properties.write(buffer) } } @@ -89,8 +89,8 @@ mod test { use crate::v5::variable_header::ReasonString; use crate::v5::variable_header::UserProperties; - #[tokio::test] - async fn test_roundtrip_mauth_no_props() { + #[test] + fn test_roundtrip_mauth_no_props() { crate::v5::test::make_roundtrip_test!(MPubrel { packet_identifier: PacketIdentifier(13), reason: PubrelReasonCode::Success, @@ -101,8 +101,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_mauth_props() { + #[test] + fn test_roundtrip_mauth_props() { crate::v5::test::make_roundtrip_test!(MPubrel { packet_identifier: PacketIdentifier(13), reason: PubrelReasonCode::Success, diff --git a/mqtt-format/src/v5/packets/suback.rs b/mqtt-format/src/v5/packets/suback.rs index 44329bf8..e8561851 100644 --- a/mqtt-format/src/v5/packets/suback.rs +++ b/mqtt-format/src/v5/packets/suback.rs @@ -86,15 +86,15 @@ impl<'i> MSuback<'i> { + self.properties.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { - self.packet_identifier.write(buffer).await?; - self.properties.write(buffer).await?; + pub fn write(&self, buffer: &mut W) -> WResult { + self.packet_identifier.write(buffer)?; + self.properties.write(buffer)?; // SAFETY: We know SubackReasonCode is a valid u8 let reasons: &[u8] = unsafe { core::mem::transmute::<&[SubackReasonCode], &[u8]>(self.reasons) }; - buffer.write_slice(reasons).await + buffer.write_slice(reasons) } } @@ -107,8 +107,8 @@ mod test { use crate::v5::variable_header::ReasonString; use crate::v5::variable_header::UserProperties; - #[tokio::test] - async fn test_roundtrip_suback_no_props() { + #[test] + fn test_roundtrip_suback_no_props() { crate::v5::test::make_roundtrip_test!(MSuback { packet_identifier: PacketIdentifier(17), reasons: &[SubackReasonCode::GrantedQoS0], @@ -119,8 +119,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_suback_props() { + #[test] + fn test_roundtrip_suback_props() { crate::v5::test::make_roundtrip_test!(MSuback { packet_identifier: PacketIdentifier(17), reasons: &[SubackReasonCode::GrantedQoS0], diff --git a/mqtt-format/src/v5/packets/subscribe.rs b/mqtt-format/src/v5/packets/subscribe.rs index 991d5213..80dc2dda 100644 --- a/mqtt-format/src/v5/packets/subscribe.rs +++ b/mqtt-format/src/v5/packets/subscribe.rs @@ -74,7 +74,7 @@ impl SubscriptionOptions { .parse_next(input) } - pub async fn write(&self, buffer: &mut W) -> WResult { + pub fn write(&self, buffer: &mut W) -> WResult { let qos = self.quality_of_service as u8; let no_local = (self.no_local as u8) << 2; let retain_as_published = (self.retain_as_published as u8) << 3; @@ -82,7 +82,7 @@ impl SubscriptionOptions { let sub_opts = qos | no_local | retain_as_published | retain_handling; - buffer.write_byte(sub_opts).await + buffer.write_byte(sub_opts) } } @@ -107,9 +107,9 @@ impl<'i> Subscription<'i> { .parse_next(input) } - pub async fn write(&self, buffer: &mut W) -> WResult { - write_string(buffer, self.topic_filter).await?; - self.options.write(buffer).await + pub fn write(&self, buffer: &mut W) -> WResult { + write_string(buffer, self.topic_filter)?; + self.options.write(buffer) } } @@ -149,9 +149,9 @@ impl<'i> Subscriptions<'i> { self.start.len() as u32 } - pub async fn write(&self, buffer: &mut W) -> WResult { + pub fn write(&self, buffer: &mut W) -> WResult { for sub in self.iter() { - sub.write(buffer).await?; + sub.write(buffer)?; } Ok(()) @@ -215,10 +215,10 @@ impl<'i> MSubscribe<'i> { + self.subscriptions.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { - self.packet_identifier.write(buffer).await?; - self.properties.write(buffer).await?; - self.subscriptions.write(buffer).await + pub fn write(&self, buffer: &mut W) -> WResult { + self.packet_identifier.write(buffer)?; + self.properties.write(buffer)?; + self.subscriptions.write(buffer) } } @@ -236,8 +236,8 @@ mod test { use crate::v5::variable_header::SubscriptionIdentifier; use crate::v5::variable_header::UserProperties; - #[tokio::test] - async fn test_roundtrip_subscription() { + #[test] + fn test_roundtrip_subscription() { crate::v5::test::make_roundtrip_test!(Subscription { topic_filter: "foo", options: SubscriptionOptions { @@ -249,8 +249,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_subscribe_no_props() { + #[test] + fn test_roundtrip_subscribe_no_props() { let mut sub_writer = TestWriter { buffer: Vec::new() }; let subscription = Subscription { @@ -263,7 +263,7 @@ mod test { }, }; - subscription.write(&mut sub_writer).await.unwrap(); + subscription.write(&mut sub_writer).unwrap(); crate::v5::test::make_roundtrip_test!(MSubscribe { packet_identifier: PacketIdentifier(88), @@ -277,8 +277,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_subscribe_with_props() { + #[test] + fn test_roundtrip_subscribe_with_props() { let mut sub_writer = TestWriter { buffer: Vec::new() }; let subscription = Subscription { @@ -291,7 +291,7 @@ mod test { }, }; - subscription.write(&mut sub_writer).await.unwrap(); + subscription.write(&mut sub_writer).unwrap(); crate::v5::test::make_roundtrip_test!(MSubscribe { packet_identifier: PacketIdentifier(88), diff --git a/mqtt-format/src/v5/packets/unsuback.rs b/mqtt-format/src/v5/packets/unsuback.rs index 94f8a981..cb231edd 100644 --- a/mqtt-format/src/v5/packets/unsuback.rs +++ b/mqtt-format/src/v5/packets/unsuback.rs @@ -76,14 +76,14 @@ impl<'i> MUnsuback<'i> { + self.properties.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { - self.packet_identifier.write(buffer).await?; - self.properties.write(buffer).await?; + pub fn write(&self, buffer: &mut W) -> WResult { + self.packet_identifier.write(buffer)?; + self.properties.write(buffer)?; // SAFETY: We know that UnsubackReasonCode is a valid u8 let reasons: &[u8] = unsafe { core::mem::transmute(self.reasons) }; - buffer.write_slice(reasons).await?; + buffer.write_slice(reasons)?; Ok(()) } @@ -98,8 +98,8 @@ mod test { use crate::v5::variable_header::ReasonString; use crate::v5::variable_header::UserProperties; - #[tokio::test] - async fn test_roundtrip_unsuback_no_props() { + #[test] + fn test_roundtrip_unsuback_no_props() { crate::v5::test::make_roundtrip_test!(MUnsuback { packet_identifier: PacketIdentifier(89), properties: UnsubackProperties { @@ -110,8 +110,8 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_unsuback_props() { + #[test] + fn test_roundtrip_unsuback_props() { crate::v5::test::make_roundtrip_test!(MUnsuback { packet_identifier: PacketIdentifier(89), properties: UnsubackProperties { diff --git a/mqtt-format/src/v5/packets/unsubscribe.rs b/mqtt-format/src/v5/packets/unsubscribe.rs index eb608d42..44c312bf 100644 --- a/mqtt-format/src/v5/packets/unsubscribe.rs +++ b/mqtt-format/src/v5/packets/unsubscribe.rs @@ -66,9 +66,9 @@ impl<'i> Unsubscriptions<'i> { self.start.len() as u32 } - pub async fn write(&self, buffer: &mut W) -> WResult { + pub fn write(&self, buffer: &mut W) -> WResult { for unsub in self.iter() { - unsub.write(buffer).await?; + unsub.write(buffer)?; } Ok(()) @@ -116,8 +116,8 @@ impl<'i> Unsubscription<'i> { .parse_next(input) } - pub async fn write(&self, buffer: &mut W) -> WResult { - write_string(buffer, self.topic_filter).await + pub fn write(&self, buffer: &mut W) -> WResult { + write_string(buffer, self.topic_filter) } } @@ -155,10 +155,10 @@ impl<'i> MUnsubscribe<'i> { + self.unsubscriptions.binary_size() } - pub async fn write(&self, buffer: &mut W) -> WResult { - self.packet_identifier.write(buffer).await?; - self.properties.write(buffer).await?; - self.unsubscriptions.write(buffer).await + pub fn write(&self, buffer: &mut W) -> WResult { + self.packet_identifier.write(buffer)?; + self.properties.write(buffer)?; + self.unsubscriptions.write(buffer) } } @@ -173,22 +173,22 @@ mod test { use crate::v5::variable_header::SubscriptionIdentifier; use crate::v5::variable_header::UserProperties; - #[tokio::test] - async fn test_roundtrip_unsubscription() { + #[test] + fn test_roundtrip_unsubscription() { crate::v5::test::make_roundtrip_test!(Unsubscription { topic_filter: "foo", }); } - #[tokio::test] - async fn test_roundtrip_unsubscribe_no_props() { + #[test] + fn test_roundtrip_unsubscribe_no_props() { let mut sub_writer = TestWriter { buffer: Vec::new() }; let subscription = Unsubscription { topic_filter: "foo", }; - subscription.write(&mut sub_writer).await.unwrap(); + subscription.write(&mut sub_writer).unwrap(); crate::v5::test::make_roundtrip_test!(MUnsubscribe { packet_identifier: PacketIdentifier(88), @@ -202,15 +202,15 @@ mod test { }); } - #[tokio::test] - async fn test_roundtrip_unsubscribe_with_props() { + #[test] + fn test_roundtrip_unsubscribe_with_props() { let mut sub_writer = TestWriter { buffer: Vec::new() }; let subscription = Unsubscription { topic_filter: "foo", }; - subscription.write(&mut sub_writer).await.unwrap(); + subscription.write(&mut sub_writer).unwrap(); crate::v5::test::make_roundtrip_test!(MUnsubscribe { packet_identifier: PacketIdentifier(88), diff --git a/mqtt-format/src/v5/properties.rs b/mqtt-format/src/v5/properties.rs index d16e855e..ce4491df 100644 --- a/mqtt-format/src/v5/properties.rs +++ b/mqtt-format/src/v5/properties.rs @@ -136,7 +136,7 @@ macro_rules! define_properties { $crate::v5::integers::variable_u32_binary_size(prop_size) + prop_size } - pub async fn write(&self, buffer: &mut W) -> crate::v5::write::WResult { + pub fn write(&self, buffer: &mut W) -> crate::v5::write::WResult { use crate::v5::variable_header::MqttProperties; #[cfg(test)] @@ -147,12 +147,12 @@ macro_rules! define_properties { + self.$prop_name.as_ref().map(|p| $crate::v5::integers::variable_u32_binary_size(<$prop>::IDENTIFIER) + p.binary_size()).unwrap_or(0) )* ; - $crate::v5::integers::write_variable_u32(buffer, size).await?; + $crate::v5::integers::write_variable_u32(buffer, size)?; $( if let Some(prop) = self.$prop_name.as_ref() { - $crate::v5::integers::write_variable_u32(buffer, <$prop>::IDENTIFIER).await?; - prop.write(buffer).await?; + $crate::v5::integers::write_variable_u32(buffer, <$prop>::IDENTIFIER)?; + prop.write(buffer)?; } )* diff --git a/mqtt-format/src/v5/reason_code.rs b/mqtt-format/src/v5/reason_code.rs index c3c0f146..83a786f0 100644 --- a/mqtt-format/src/v5/reason_code.rs +++ b/mqtt-format/src/v5/reason_code.rs @@ -30,10 +30,10 @@ macro_rules! make_combined_reason_code { 1 } - pub async fn write(&self, buffer: &mut W) -> $crate::v5::write::WResult { + pub fn write(&self, buffer: &mut W) -> $crate::v5::write::WResult { match self { $( - Self::$reason_code_name => buffer.write_byte(<$reason_code_type>::CODE).await?, + Self::$reason_code_name => buffer.write_byte(<$reason_code_type>::CODE)?, )* } diff --git a/mqtt-format/src/v5/strings.rs b/mqtt-format/src/v5/strings.rs index c34dcd41..24e5ee95 100644 --- a/mqtt-format/src/v5/strings.rs +++ b/mqtt-format/src/v5/strings.rs @@ -37,11 +37,11 @@ pub fn string_binary_size(s: &str) -> u32 { (2 + s.len()) as u32 } -pub async fn write_string(buffer: &mut W, s: &str) -> WResult { +pub fn write_string(buffer: &mut W, s: &str) -> WResult { let len = s.len().try_into().map_err(|_| MqttWriteError::Invariant)?; - buffer.write_u16(len).await?; - buffer.write_slice(s.as_bytes()).await + buffer.write_u16(len)?; + buffer.write_slice(s.as_bytes()) } #[inline] @@ -79,13 +79,13 @@ mod tests { assert_eq!(parse_string(&mut Bytes::new(&input)).unwrap(), "A𪛔"); } - #[tokio::test] - async fn test_write_string() { + #[test] + fn test_write_string() { let mut writer = TestWriter { buffer: Vec::new() }; let s = "foo bar baz"; - write_string(&mut writer, s).await.unwrap(); + write_string(&mut writer, s).unwrap(); let out = parse_string(&mut Bytes::new(&writer.buffer)).unwrap(); assert_eq!(out, s) } diff --git a/mqtt-format/src/v5/test.rs b/mqtt-format/src/v5/test.rs index 90fa1aee..79413928 100644 --- a/mqtt-format/src/v5/test.rs +++ b/mqtt-format/src/v5/test.rs @@ -16,12 +16,12 @@ pub struct TestWriter { impl WriteMqttPacket for TestWriter { type Error = MqttWriteError; - async fn write_byte(&mut self, u: u8) -> WResult { + fn write_byte(&mut self, u: u8) -> WResult { self.buffer.push(u); Ok(()) } - async fn write_slice(&mut self, u: &[u8]) -> WResult { + fn write_slice(&mut self, u: &[u8]) -> WResult { self.buffer.extend(u); Ok(()) } @@ -35,7 +35,7 @@ macro_rules! make_roundtrip_test { ($name:ident $def:tt) => { let mut writer = $crate::v5::test::TestWriter { buffer: Vec::new() }; let instance = $name $def; - instance.write(&mut writer).await.unwrap(); + instance.write(&mut writer).unwrap(); let output = $name::parse(&mut winnow::Bytes::new(&writer.buffer)).unwrap(); assert_eq!(instance, output); } diff --git a/mqtt-format/src/v5/variable_header.rs b/mqtt-format/src/v5/variable_header.rs index ff46aec6..82aee704 100644 --- a/mqtt-format/src/v5/variable_header.rs +++ b/mqtt-format/src/v5/variable_header.rs @@ -30,8 +30,8 @@ impl PacketIdentifier { 2 } - pub async fn write(&self, buffer: &mut W) -> WResult { - buffer.write_u16(self.0).await + pub fn write(&self, buffer: &mut W) -> WResult { + buffer.write_u16(self.0) } } @@ -45,10 +45,7 @@ pub trait MqttProperties<'lt>: Sized { fn binary_size(&self) -> u32; - fn write( - &self, - buffer: &mut W, - ) -> impl core::future::Future> + Send; + fn write(&self, buffer: &mut W) -> WResult; } macro_rules! define_properties { @@ -86,10 +83,10 @@ macro_rules! define_properties { fun(&self.0) } - async fn write(&self, buffer: &mut W) + fn write(&self, buffer: &mut W) -> $crate::v5::write::WResult { - $writer(buffer, self.0).await?; + $writer(buffer, self.0)?; Ok(()) } } @@ -127,14 +124,14 @@ macro_rules! define_properties { #[cfg(test)] mod property_tests { $( - #[tokio::test] - async fn $testfnname () { + #[test] + fn $testfnname () { use super::MqttProperties; use super::$name; $( let mut writer = $crate::v5::test::TestWriter { buffer: Vec::new() }; let instance = $name ($testvalue); - instance.write(&mut writer).await.unwrap(); + instance.write(&mut writer).unwrap(); let output = $name::parse(&mut winnow::Bytes::new(&writer.buffer)).unwrap(); let expected = $name ( $testvalue ); @@ -147,8 +144,8 @@ macro_rules! define_properties { } #[inline] -async fn write_u8(buffer: &mut W, u: u8) -> WResult { - buffer.write_byte(u).await +fn write_u8(buffer: &mut W, u: u8) -> WResult { + buffer.write_byte(u) } define_properties! {[ @@ -393,17 +390,17 @@ impl<'i> MqttProperties<'i> for UserProperties<'i> { .sum() } - async fn write(&self, buffer: &mut W) -> WResult { + fn write(&self, buffer: &mut W) -> WResult { let mut iter = self.iter(); let first = iter .next() .expect("There is always at least one UserProperty available"); - first.write(buffer).await?; + first.write(buffer)?; for up in iter { - write_variable_u32(buffer, UserProperties::IDENTIFIER).await?; - up.write(buffer).await?; + write_variable_u32(buffer, UserProperties::IDENTIFIER)?; + up.write(buffer)?; } Ok(()) @@ -448,9 +445,9 @@ impl<'i> UserProperty<'i> { crate::v5::strings::string_pair_binary_size(self.key, self.value) } - pub async fn write(&self, buffer: &mut W) -> WResult { - crate::v5::strings::write_string(buffer, self.key).await?; - crate::v5::strings::write_string(buffer, self.value).await + pub fn write(&self, buffer: &mut W) -> WResult { + crate::v5::strings::write_string(buffer, self.key)?; + crate::v5::strings::write_string(buffer, self.value) } } @@ -538,8 +535,8 @@ mod tests { ); } - #[tokio::test] - async fn test_write_properties() { + #[test] + fn test_write_properties() { #[rustfmt::skip] let input = &[ // First the string pair of the UserProp @@ -558,10 +555,8 @@ mod tests { let prop = UserProperties(input); let mut writer = TestWriter { buffer: Vec::new() }; - write_variable_u32(&mut writer, UserProperties::IDENTIFIER) - .await - .unwrap(); - prop.write(&mut writer).await.unwrap(); + write_variable_u32(&mut writer, UserProperties::IDENTIFIER).unwrap(); + prop.write(&mut writer).unwrap(); let out = Property::parse(&mut Bytes::new(&writer.buffer)).unwrap(); diff --git a/mqtt-format/src/v5/write.rs b/mqtt-format/src/v5/write.rs index d93ea9e6..dbd9de93 100644 --- a/mqtt-format/src/v5/write.rs +++ b/mqtt-format/src/v5/write.rs @@ -14,27 +14,22 @@ pub enum MqttWriteError { pub trait WriteMqttPacket: Send { type Error: From; - fn write_byte(&mut self, u: u8) -> impl core::future::Future> + Send; - fn write_slice(&mut self, u: &[u8]) - -> impl core::future::Future> + Send; + fn write_byte(&mut self, u: u8) -> WResult; + fn write_slice(&mut self, u: &[u8]) -> WResult; #[inline] - fn write_u16(&mut self, u: u16) -> impl core::future::Future> + Send { - async move { - self.write_byte((u >> 8) as u8).await?; - self.write_byte(u as u8).await - } + fn write_u16(&mut self, u: u16) -> WResult { + self.write_byte((u >> 8) as u8)?; + self.write_byte(u as u8) } #[inline] - fn write_u32(&mut self, u: u32) -> impl core::future::Future> + Send { - async move { - let bytes = u.to_be_bytes(); - self.write_byte(bytes[0]).await?; - self.write_byte(bytes[1]).await?; - self.write_byte(bytes[2]).await?; - self.write_byte(bytes[3]).await - } + fn write_u32(&mut self, u: u32) -> WResult { + let bytes = u.to_be_bytes(); + self.write_byte(bytes[0])?; + self.write_byte(bytes[1])?; + self.write_byte(bytes[2])?; + self.write_byte(bytes[3]) } #[cfg(test)] @@ -46,39 +41,39 @@ mod test { use super::WriteMqttPacket; use crate::v5::test::TestWriter; - #[tokio::test] - async fn test_write_u16() { + #[test] + fn test_write_u16() { for i in (0..=u16::MAX).step_by(991) { let mut writer = TestWriter { buffer: Vec::new() }; - writer.write_u16(i).await.unwrap(); + writer.write_u16(i).unwrap(); assert_eq!(&writer.buffer, &i.to_be_bytes()); } } - #[tokio::test] - async fn test_write_u32_1() { + #[test] + fn test_write_u32_1() { let num = 1; let mut writer = TestWriter { buffer: Vec::new() }; - writer.write_u32(num).await.unwrap(); + writer.write_u32(num).unwrap(); assert_eq!(&writer.buffer, &[0x00, 0x00, 0x00, 0x01]); assert_eq!(&writer.buffer, &num.to_be_bytes()); } - #[tokio::test] - async fn test_write_u32_12() { + #[test] + fn test_write_u32_12() { let num = 12; let mut writer = TestWriter { buffer: Vec::new() }; - writer.write_u32(num).await.unwrap(); + writer.write_u32(num).unwrap(); assert_eq!(&writer.buffer, &[0x00, 0x00, 0x00, 12u8.to_be()]); assert_eq!(&writer.buffer, &num.to_be_bytes()); } - #[tokio::test] - async fn test_write_u32_range() { + #[test] + fn test_write_u32_range() { // step by some prime number for i in (0..268_435_455).step_by(991) { let mut writer = TestWriter { buffer: Vec::new() }; - writer.write_u32(i).await.unwrap(); + writer.write_u32(i).unwrap(); assert_eq!(&writer.buffer, &i.to_be_bytes()); } }