From 233c978b8c601036d77b5ca3ee5c328d2a53f8cb Mon Sep 17 00:00:00 2001 From: Simon Chen Date: Sat, 21 Oct 2023 12:48:11 +0000 Subject: [PATCH] fix clippy warning --- amqp_serde/src/de.rs | 90 +++++++++++++++++++++++------------------ amqp_serde/src/types.rs | 58 ++++++++++++++------------ 2 files changed, 83 insertions(+), 65 deletions(-) diff --git a/amqp_serde/src/de.rs b/amqp_serde/src/de.rs index 9ad06d3..997208e 100644 --- a/amqp_serde/src/de.rs +++ b/amqp_serde/src/de.rs @@ -455,11 +455,19 @@ struct DataSequence<'a, 'de: 'a> { impl<'a, 'de> DataSequence<'a, 'de> { fn new(de: &'a mut Deserializer<'de>, len: usize) -> Self { - DataSequence { de, len, is_struct: false } + DataSequence { + de, + len, + is_struct: false, + } } fn new_struct(de: &'a mut Deserializer<'de>, len: usize) -> Self { - DataSequence { de, len, is_struct: true } + DataSequence { + de, + len, + is_struct: true, + } } } @@ -602,44 +610,46 @@ mod tests { #[test] fn test_array() { let input = vec![ - 0x41, 0x00, 0x00, 0x01, 0x7c, 0x53, 0x00, 0x00, 0x00, 0x32, 0x7b, 0x3c, 0x3c, 0x22, 0x63, 0x6f, - 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3e, 0x3e, - 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x30, 0x45, 0x30, 0x45, - 0x30, 0x45, 0x42, 0x30, 0x42, 0x30, 0x32, 0x30, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, - 0x26, 0x7b, 0x3c, 0x3c, 0x22, 0x63, 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x22, 0x3e, - 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x47, 0x61, 0x6c, - 0x67, 0x75, 0x73, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x25, 0x7b, 0x3c, 0x3c, 0x22, - 0x68, 0x6f, 0x73, 0x74, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, - 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x47, 0x61, 0x6c, 0x67, 0x75, 0x73, 0x22, 0x3e, 0x3e, - 0x7d, 0x53, 0x00, 0x00, 0x00, 0x21, 0x7b, 0x3c, 0x3c, 0x22, 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, - 0x74, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, - 0x43, 0x48, 0x54, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x38, 0x7b, 0x3c, 0x3c, 0x22, - 0x69, 0x6e, 0x66, 0x6f, 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, - 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, - 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x67, 0x61, 0x6c, 0x67, 0x75, 0x73, 0x2e, 0x6e, 0x65, 0x74, - 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x29, 0x7b, 0x3c, 0x3c, 0x22, 0x76, 0x65, 0x72, - 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, - 0x3c, 0x3c, 0x22, 0x4d, 0x6f, 0x63, 0x6b, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x3e, - 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x29, 0x7b, 0x3c, 0x3c, 0x22, 0x70, 0x6c, 0x61, 0x74, 0x66, - 0x6f, 0x72, 0x6d, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, - 0x3c, 0x22, 0x41, 0x58, 0x31, 0x38, 0x30, 0x30, 0x59, 0x43, 0x58, 0x54, 0x22, 0x3e, 0x3e, 0x7d, - 0x53, 0x00, 0x00, 0x00, 0x2c, 0x7b, 0x3c, 0x3c, 0x22, 0x65, 0x63, 0x6f, 0x76, 0x65, 0x72, 0x73, - 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, - 0x3c, 0x22, 0x4d, 0x6f, 0x63, 0x6b, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x3e, 0x3e, - 0x7d, + 0x41, 0x00, 0x00, 0x01, 0x7c, 0x53, 0x00, 0x00, 0x00, 0x32, 0x7b, 0x3c, 0x3c, 0x22, + 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x6e, 0x61, 0x6d, + 0x65, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, + 0x3c, 0x22, 0x30, 0x45, 0x30, 0x45, 0x30, 0x45, 0x42, 0x30, 0x42, 0x30, 0x32, 0x30, + 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x26, 0x7b, 0x3c, 0x3c, 0x22, 0x63, + 0x6f, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68, 0x74, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, + 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x47, 0x61, 0x6c, 0x67, 0x75, + 0x73, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x25, 0x7b, 0x3c, 0x3c, 0x22, + 0x68, 0x6f, 0x73, 0x74, 0x6e, 0x61, 0x6d, 0x65, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, + 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x47, 0x61, 0x6c, 0x67, 0x75, + 0x73, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x21, 0x7b, 0x3c, 0x3c, 0x22, + 0x70, 0x72, 0x6f, 0x64, 0x75, 0x63, 0x74, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, + 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x43, 0x48, 0x54, 0x22, 0x3e, 0x3e, + 0x7d, 0x53, 0x00, 0x00, 0x00, 0x38, 0x7b, 0x3c, 0x3c, 0x22, 0x69, 0x6e, 0x66, 0x6f, + 0x72, 0x6d, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, + 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, + 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x67, 0x61, 0x6c, 0x67, 0x75, 0x73, 0x2e, 0x6e, + 0x65, 0x74, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, 0x29, 0x7b, 0x3c, 0x3c, + 0x22, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, + 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x4d, 0x6f, 0x63, 0x6b, 0x53, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x22, 0x3e, 0x3e, 0x7d, 0x53, 0x00, 0x00, 0x00, + 0x29, 0x7b, 0x3c, 0x3c, 0x22, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x22, + 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, + 0x41, 0x58, 0x31, 0x38, 0x30, 0x30, 0x59, 0x43, 0x58, 0x54, 0x22, 0x3e, 0x3e, 0x7d, + 0x53, 0x00, 0x00, 0x00, 0x2c, 0x7b, 0x3c, 0x3c, 0x22, 0x65, 0x63, 0x6f, 0x76, 0x65, + 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x22, 0x3e, 0x3e, 0x2c, 0x6c, 0x6f, 0x6e, 0x67, 0x73, + 0x74, 0x72, 0x2c, 0x3c, 0x3c, 0x22, 0x4d, 0x6f, 0x63, 0x6b, 0x53, 0x65, 0x72, 0x76, + 0x69, 0x63, 0x65, 0x22, 0x3e, 0x3e, 0x7d, ]; - let f_array = FieldArray::try_from( - vec!( - FieldValue::from("{<<\"connection_name\">>,longstr,<<\"0E0E0EB0B020\">>}"), - FieldValue::from("{<<\"copyright\">>,longstr,<<\"Galgus\">>}"), - FieldValue::from("{<<\"hostname\">>,longstr,<<\"Galgus\">>}"), - FieldValue::from("{<<\"product\">>,longstr,<<\"CHT\">>}"), - FieldValue::from("{<<\"information\">>,longstr,<<\"https://www.galgus.net\">>}"), - FieldValue::from("{<<\"version\">>,longstr,<<\"MockService\">>}"), - FieldValue::from("{<<\"platform\">>,longstr,<<\"AX1800YCXT\">>}"), - FieldValue::from("{<<\"ecoversion\">>,longstr,<<\"MockService\">>}"), - ) - ).unwrap(); + let f_array = FieldArray::try_from(vec![ + FieldValue::from("{<<\"connection_name\">>,longstr,<<\"0E0E0EB0B020\">>}"), + FieldValue::from("{<<\"copyright\">>,longstr,<<\"Galgus\">>}"), + FieldValue::from("{<<\"hostname\">>,longstr,<<\"Galgus\">>}"), + FieldValue::from("{<<\"product\">>,longstr,<<\"CHT\">>}"), + FieldValue::from("{<<\"information\">>,longstr,<<\"https://www.galgus.net\">>}"), + FieldValue::from("{<<\"version\">>,longstr,<<\"MockService\">>}"), + FieldValue::from("{<<\"platform\">>,longstr,<<\"AX1800YCXT\">>}"), + FieldValue::from("{<<\"ecoversion\">>,longstr,<<\"MockService\">>}"), + ]) + .unwrap(); let expected = FieldValue::A(f_array); let result: FieldValue = from_bytes(&input).unwrap(); assert_eq!(expected, result); @@ -803,6 +813,6 @@ mod tests { assert_eq!('€', result.m_char.1); assert_eq!(b"beef".to_vec(), result.m_owned_bytes.1); assert_eq!(Some(b'o'), result.m_opt); - assert_eq!((), result.m_unit); + assert_eq!(0, std::mem::size_of_val(&result.m_unit)); } } diff --git a/amqp_serde/src/types.rs b/amqp_serde/src/types.rs index 665a3e9..9adcfc1 100644 --- a/amqp_serde/src/types.rs +++ b/amqp_serde/src/types.rs @@ -3,6 +3,7 @@ //! See [RabbitMQ's Definition](https://github.com/rabbitmq/rabbitmq-codegen/blob/main/amqp-rabbitmq-0.9.1.json). //! //! See [RabbitMQ errata](https://www.rabbitmq.com/amqp-0-9-1-errata.html) +use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, convert::TryFrom, @@ -10,7 +11,6 @@ use std::{ mem::{size_of, size_of_val}, num::TryFromIntError, }; -use serde::{Deserialize, Serialize}; /// DO NOT USE. No primitive rust type to represent single bit. /// @@ -209,7 +209,9 @@ impl TryFrom> for FieldArray { type Error = TryFromIntError; fn try_from(values: Vec) -> Result { - let total_bytes = values.iter().fold(0, |acc, v| acc + FieldValue::TAG_SIZE + v.len()); + let total_bytes = values + .iter() + .fold(0, |acc, v| acc + FieldValue::TAG_SIZE + v.len()); let len = LongUint::try_from(total_bytes)?; Ok(Self(len, values)) } @@ -282,23 +284,23 @@ impl FieldValue { fn len(&self) -> usize { match self { - Self::V => 0, // fixed size - Self::t(_) => size_of::(), // fixed size - Self::b(_) => size_of::(), // fixed size - Self::B(_) => size_of::(), // fixed size - Self::s(_) => size_of::(), // fixed size - Self::u(_) => size_of::(), // fixed size - Self::I(_) => size_of::(), // fixed size - Self::i(_) => size_of::(), // fixed size - Self::l(_) => size_of::(), // fixed size - Self::f(_) => size_of::(), // fixed size - Self::d(_) => size_of::(), // fixed size - Self::T(_) => size_of::(), // fixed size + Self::V => 0, // fixed size + Self::t(_) => size_of::(), // fixed size + Self::b(_) => size_of::(), // fixed size + Self::B(_) => size_of::(), // fixed size + Self::s(_) => size_of::(), // fixed size + Self::u(_) => size_of::(), // fixed size + Self::I(_) => size_of::(), // fixed size + Self::i(_) => size_of::(), // fixed size + Self::l(_) => size_of::(), // fixed size + Self::f(_) => size_of::(), // fixed size + Self::d(_) => size_of::(), // fixed size + Self::T(_) => size_of::(), // fixed size Self::D(v) => size_of_val(&v.0) + size_of_val(&v.1), // fixed size - Self::S(v) => size_of_val(&v.0) + v.0 as usize, // variable size - Self::A(v) => size_of_val(&v.0) + v.0 as usize, // variable size - Self::F(v) => size_of_val(&v.0) + v.0 as usize, // variable size - Self::x(v) => size_of_val(&v.0) + v.0 as usize, // variable size + Self::S(v) => size_of_val(&v.0) + v.0 as usize, // variable size + Self::A(v) => size_of_val(&v.0) + v.0 as usize, // variable size + Self::F(v) => size_of_val(&v.0) + v.0 as usize, // variable size + Self::x(v) => size_of_val(&v.0) + v.0 as usize, // variable size } } } @@ -384,7 +386,6 @@ impl<'a> TryInto<&'a str> for &'a FieldValue { } } - /// RabbitMQ's field value support only long string variant, so rust string type /// always converted to long string variant. impl From for FieldValue { @@ -446,13 +447,18 @@ impl FieldTable { } pub fn insert(&mut self, k: FieldName, v: FieldValue) -> Option { - self.0 += LongUint::try_from(size_of_val(&k.0) + k.0 as usize + FieldValue::TAG_SIZE + v.len()).unwrap(); + self.0 += + LongUint::try_from(size_of_val(&k.0) + k.0 as usize + FieldValue::TAG_SIZE + v.len()) + .unwrap(); self.1.insert(k, v) } pub fn remove(&mut self, k: &FieldName) -> Option { if let Some(v) = self.1.remove(k) { - self.0 -= LongUint::try_from(size_of_val(&k.0) + k.0 as usize + FieldValue::TAG_SIZE + v.len()).unwrap(); + self.0 -= LongUint::try_from( + size_of_val(&k.0) + k.0 as usize + FieldValue::TAG_SIZE + v.len(), + ) + .unwrap(); Some(v) } else { None @@ -588,8 +594,8 @@ pub type AmqpTimeStamp = TimeStamp; ///////////////////////////////////////////////////////////////////////////// #[cfg(test)] mod tests { - use std::mem::size_of; use crate::types::{ByteArray, DecimalValue, FieldArray, FieldValue, LongStr, LongUint}; + use std::mem::size_of; use super::{FieldTable, ShortStr}; #[test] @@ -657,7 +663,7 @@ mod tests { let exp = FieldValue::t(true); assert_eq!(exp, true.into()); let t: bool = exp.try_into().unwrap(); - assert_eq!(true, t); + assert!(t); let exp = FieldValue::F(FieldTable::default()); assert_eq!(exp, FieldTable::default().into()); @@ -699,7 +705,8 @@ mod tests { FieldValue::S(s4), ]; - v.iter().for_each(|_fv| total_length += FieldValue::TAG_SIZE + size_of::()); + v.iter() + .for_each(|_fv| total_length += FieldValue::TAG_SIZE + size_of::()); let a = FieldArray::try_from(v).unwrap(); @@ -723,7 +730,8 @@ mod tests { ]; let mut total_length = 0; v_kv.into_iter().for_each(|(k, v)| { - total_length += k.1.len() + v.1.len() + 2 * FieldValue::TAG_SIZE + size_of::(); + total_length += + k.1.len() + v.1.len() + 2 * FieldValue::TAG_SIZE + size_of::(); ft.insert(k, FieldValue::S(v)); });