From 0f05dc2b75a842618965da005890106e733e71e3 Mon Sep 17 00:00:00 2001 From: andber1 <82754113+andber1@users.noreply.github.com> Date: Tue, 13 Aug 2024 21:22:17 +0200 Subject: [PATCH] Fix some clippy issues --- framed/src/bytes.rs | 84 +++++++++++++++++++++--------------------- framed/src/checksum.rs | 11 ++---- framed/src/error.rs | 13 +++---- framed/src/lib.rs | 8 ++-- framed/src/typed.rs | 24 ++++++------ 5 files changed, 66 insertions(+), 74 deletions(-) diff --git a/framed/src/bytes.rs b/framed/src/bytes.rs index 050fa67..02c6c8e 100644 --- a/framed/src/bytes.rs +++ b/framed/src/bytes.rs @@ -32,7 +32,7 @@ //! assert_eq!(payload, *decoded); //! ``` //! -//! ## Example usage from a no_std crate +//! ## Example usage from a `no_std` crate //! //! The `encode_to_slice` and `decode_from_slice` functions offer an //! API for `no_std` crates that do not have a heap allocator @@ -125,7 +125,7 @@ impl Config { pub fn to_receiver(&mut self, r: R) -> Receiver { Receiver:: { codec: self.to_codec(), - r: r, + r, } } @@ -134,7 +134,7 @@ impl Config { pub fn to_sender(&mut self, w: W) -> Sender { Sender:: { codec: self.to_codec(), - w: w, + w, } } @@ -145,7 +145,7 @@ impl Config { } /// Get the current checksum configuration. - pub fn checksum(&self) -> &Checksum { + #[must_use] pub fn checksum(&self) -> &Checksum { &self.checksum } @@ -164,8 +164,7 @@ impl Codec { } fn min_frame_len(&self) -> usize { - 0 // payload length - + self.checksum().len() + self.checksum().len() + 1 // sentinel length } @@ -194,13 +193,13 @@ impl Codec { let cobs_len = { let mut cobs_enc = cobs::CobsEncoder::new(dest); cobs_enc.push(p) - .map_err(|_| Error::CobsEncodeFailed)?; + .map_err(|()| Error::CobsEncodeFailed)?; if checksum_value.len() > 0 { - cobs_enc.push(&*checksum_value) - .map_err(|_| Error::CobsEncodeFailed)?; + cobs_enc.push(&checksum_value) + .map_err(|()| Error::CobsEncodeFailed)?; } let cobs_len = cobs_enc.finalize() - .map_err(|_| Error::CobsEncodeFailed)?; + .map_err(|()| Error::CobsEncodeFailed)?; // make sure sentinel is not in output buffer for x in &mut dest[..cobs_len] { *x ^= FRAME_END_SYMBOL; @@ -221,7 +220,7 @@ impl Codec { #[cfg(feature = "use_std")] pub fn encode_to_box(&mut self, p: &Payload) -> Result { let mut buf = vec![0; max_encoded_len(p.len())]; - let len = self.encode_to_slice(p, &mut *buf)?; + let len = self.encode_to_slice(p, &mut buf)?; buf.truncate(len); Ok(BoxEncoded::from(buf)) } @@ -237,7 +236,7 @@ impl Codec { pub fn encode_to_writer(&mut self, p: &Payload, w: &mut W) -> Result { let b = self.encode_to_box(p)?; - w.write_all(&*b.0)?; + w.write_all(&b.0)?; Ok(b.len()) } @@ -249,7 +248,7 @@ impl Codec { /// whole buffer including `FRAME_END_SYMBOL` to this function for /// decoding. /// - /// If there is more than 1 FRAME_END_SYMBOL within `e`, the result + /// If there is more than 1 `FRAME_END_SYMBOL` within `e`, the result /// is undefined. Make sure you only pass 1 frame at a time. /// /// Returns the length of the payload it has decoded. @@ -272,7 +271,7 @@ impl Codec { println!("framed::decode: Encoded = {:?}", e); } - if e.len() == 0 { + if e.is_empty() { return Err(Error::EofBeforeFrame); } @@ -289,11 +288,11 @@ impl Codec { let cobs_payload = &e[0..e.len() - 1]; let cobs_decoded_len = - if cobs_payload.len() == 0 { + if cobs_payload.is_empty() { 0 } else { cobs::decode_with_sentinel(cobs_payload, dest, FRAME_END_SYMBOL) - .map_err(|_| Error::CobsDecodeFailed)? + .map_err(|()| Error::CobsDecodeFailed)? }; let cobs_decoded = &dest[0..cobs_decoded_len]; if cobs_decoded_len < self.checksum().len() { @@ -328,7 +327,7 @@ impl Codec { /// Decode the supplied encoded frame, returning the payload on the heap. #[cfg(feature = "use_std")] pub fn decode_to_box(&mut self, e: &Encoded) -> Result { - if e.len() == 0 { + if e.is_empty() { return Err(Error::EofBeforeFrame); } let mut buf = vec![0; max_decoded_len(e.len())]; @@ -354,9 +353,9 @@ impl Codec { // In the 2 EOF cases defer to decode_to_box to return the // correct error (EofBeforeFrame or EofDuringFrame). Err(ref e) if e.kind() == io::ErrorKind::UnexpectedEof => - return self.decode_to_box(&*next_frame), + return self.decode_to_box(&next_frame), Ok(0) => - return self.decode_to_box(&*next_frame), + return self.decode_to_box(&next_frame), Err(e) => return Err(Error::from(e)), Ok(1) => (), @@ -373,7 +372,7 @@ impl Codec { } assert_eq!(next_frame[next_frame.len()-1], FRAME_END_SYMBOL); - self.decode_to_box(&*next_frame) + self.decode_to_box(&next_frame) } } // End of impl Codec. @@ -535,9 +534,8 @@ mod tests { fn assert_payload_eq(encoded: &Encoded, payload: &Payload) { #[cfg(feature = "use_std")] { println!("assert_payload_eq \n\ - - encoded = {:?}\n\ - - payload = {:?}", - encoded, payload); + - encoded = {encoded:?}\n\ + - payload = {payload:?}"); } let mut decoded_buf = [0; 100]; let len = codec().decode_to_slice(encoded, &mut decoded_buf).unwrap(); @@ -556,7 +554,7 @@ mod tests { #[cfg(feature = "use_std")] fn encode_to_slice_ok_dynamic_dest() { let mut encoded_buf = vec![0u8; max_encoded_len(PAYLOAD.len())]; - let len = codec().encode_to_slice(&PAYLOAD, &mut *encoded_buf).unwrap(); + let len = codec().encode_to_slice(&PAYLOAD, &mut encoded_buf).unwrap(); let encoded = &encoded_buf[0..len]; assert_payload_eq(encoded, &PAYLOAD); @@ -578,7 +576,7 @@ mod tests { fn encode_to_writer_ok() { let mut encoded = vec![]; codec().encode_to_writer(&PAYLOAD, &mut encoded).unwrap(); - assert_payload_eq(&*encoded, &PAYLOAD); + assert_payload_eq(&encoded, &PAYLOAD); } #[test] @@ -587,7 +585,7 @@ mod tests { let mut buf = [0; ENCODED_LEN]; let encoded = encoded_payload(&mut buf); let mut decoded_buf = [0u8; PAYLOAD_LEN - 1]; - let _ = codec().decode_to_slice(&*encoded, &mut decoded_buf); + let _ = codec().decode_to_slice(encoded, &mut decoded_buf); } #[test] @@ -595,7 +593,7 @@ mod tests { fn decode_to_slice_ok_dynamic_dest() { let encoded = codec().encode_to_box(&PAYLOAD).unwrap(); let mut decoded_buf = vec![0u8; max_decoded_len(encoded.len())]; - let len = codec().decode_to_slice(&*encoded, &mut decoded_buf).unwrap(); + let len = codec().decode_to_slice(&encoded, &mut decoded_buf).unwrap(); let decoded = &decoded_buf[0..len]; assert_eq!(&PAYLOAD, decoded); @@ -606,7 +604,7 @@ mod tests { fn decode_to_slice_no_end_symbol() { let encoded = vec![FRAME_END_SYMBOL + 1; max_encoded_len(0)]; let mut decoded_buf = []; - let res = codec().decode_to_slice(&*encoded, &mut decoded_buf); + let res = codec().decode_to_slice(&encoded, &mut decoded_buf); match res { Err(Error::EofDuringFrame) => (), @@ -620,7 +618,7 @@ mod tests { let mut c = codec(); let encoded = vec![FRAME_END_SYMBOL; c.min_frame_len() - 1]; let mut decoded_buf = []; - let res = c.decode_to_slice(&*encoded, &mut decoded_buf); + let res = c.decode_to_slice(&encoded, &mut decoded_buf); match res { Err(Error::EofDuringFrame) => (), @@ -633,7 +631,7 @@ mod tests { fn decode_to_slice_encoded_empty() { let encoded = vec![]; let mut decoded_buf = []; - let res = codec().decode_to_slice(&*encoded, &mut decoded_buf); + let res = codec().decode_to_slice(&encoded, &mut decoded_buf); match res { Err(Error::EofBeforeFrame) => (), @@ -658,7 +656,7 @@ mod tests { } let mut decoded_buf = vec![0u8; max_decoded_len(encoded.len())]; - let res = codec().decode_to_slice(&*encoded, &mut decoded_buf); + let res = codec().decode_to_slice(&encoded, &mut decoded_buf); match res { Err(Error::ChecksumError) => (), @@ -673,7 +671,7 @@ mod tests { let encoded = &encoded[1..encoded.len()]; let mut decoded_buf = vec![0u8; max_decoded_len(encoded.len())]; - let res = codec().decode_to_slice(&*encoded, &mut decoded_buf); + let res = codec().decode_to_slice(encoded, &mut decoded_buf); match res { Err(Error::ChecksumError) => (), @@ -685,7 +683,7 @@ mod tests { #[cfg(feature = "use_std")] fn decode_to_box_ok() { let encoded = codec().encode_to_box(&PAYLOAD).unwrap(); - let decoded = codec().decode_to_box(&*encoded).unwrap(); + let decoded = codec().decode_to_box(&encoded).unwrap(); assert_eq!(&PAYLOAD, &*decoded); } @@ -705,7 +703,7 @@ mod tests { fn roundtrip_default_config() { roundtrip_case(&mut Config::default() .to_codec(), - &PAYLOAD) + &PAYLOAD); } #[test] @@ -714,7 +712,7 @@ mod tests { roundtrip_case(&mut Config::default() .set_checksum(Checksum::None) .to_codec(), - &PAYLOAD) + &PAYLOAD); } #[test] @@ -722,16 +720,16 @@ mod tests { fn roundtrip_empty_payload() { roundtrip_case(&mut Config::default() .to_codec(), - &[]) + &[]); } #[cfg(feature = "use_std")] fn roundtrip_case(c: &mut Codec, payload: &Payload) { let encoded = c.encode_to_box(payload); - println!("encoded: {:?}", encoded); + println!("encoded: {encoded:?}"); let encoded = encoded.unwrap(); - let decoded = c.decode_to_box(&*encoded); - println!("decoded: {:?}", decoded); + let decoded = c.decode_to_box(&encoded); + println!("decoded: {decoded:?}"); let decoded = decoded.unwrap(); assert_eq!(&*decoded, payload); } @@ -782,8 +780,8 @@ mod rw_tests { let r1 = rx.recv().unwrap(); let r2 = rx.recv().unwrap(); - println!("r1: {:?}\n\ - r2: {:?}", r1, r2); + println!("r1: {r1:?}\n\ + r2: {r2:?}"); assert_eq!(*r1, s1); assert_eq!(*r2, s2); @@ -794,7 +792,7 @@ mod rw_tests { let (mut _tx, mut rx) = pair(); match rx.recv() { Err(Error::EofBeforeFrame) => (), - e @ _ => panic!("Bad value: {:?}", e) + e => panic!("Bad value: {:?}", e) } } @@ -806,7 +804,7 @@ mod rw_tests { tx_raw.write(&[0x01]).unwrap(); match rx.recv() { Err(Error::EofDuringFrame) => (), - e @ _ => panic!("Bad value: {:?}", e) + e => panic!("Bad value: {:?}", e) } } diff --git a/framed/src/checksum.rs b/framed/src/checksum.rs index dd2f620..caf636b 100644 --- a/framed/src/checksum.rs +++ b/framed/src/checksum.rs @@ -7,6 +7,7 @@ use crc16; /// A checksum algorithm configuration to use when encoding data. #[derive(Clone, Debug)] +#[derive(Default)] pub enum Checksum { /// Use no checksum. None, @@ -15,14 +16,10 @@ pub enum Checksum { /// This is the default checksum. /// /// [impl]: https://docs.rs/crc16/0.3.4/crc16/enum.CDMA2000.html + #[default] Crc16Cdma2000, } -impl Default for Checksum { - fn default() -> Checksum { - Checksum::Crc16Cdma2000 - } -} pub(crate) const MAX_CHECKSUM_LEN: usize = 2; @@ -67,12 +64,12 @@ impl Deref for ChecksumValue { impl PartialEq for ChecksumValue { fn eq(&self, other: &ChecksumValue) -> bool { - self.deref() == other.deref() + **self == **other } } impl Debug for ChecksumValue { fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { - write!(f, "{:?}", self.deref()) + write!(f, "{:?}", &**self) } } diff --git a/framed/src/error.rs b/framed/src/error.rs index 6de17e4..1f16bc4 100644 --- a/framed/src/error.rs +++ b/framed/src/error.rs @@ -32,11 +32,11 @@ pub enum Error { /// End of data before a frame started; we received none of a frame. EofBeforeFrame, - /// Forwarded io::Error. + /// Forwarded `io::Error`. #[cfg(feature = "use_std")] Io(io::Error), - /// Forwarded ssmarshal::Error. + /// Forwarded `ssmarshal::Error`. Ssmarshal(ssmarshal::Error), } @@ -44,14 +44,11 @@ impl Error { /// Returns true if the error represents a corrupted frame. Data /// may have been lost but the decoder should decode the next /// frame correctly. - pub fn is_corrupt_frame(&self) -> bool { - match *self { + #[must_use] pub fn is_corrupt_frame(&self) -> bool { + matches!(*self, Error::ChecksumError | Error::CobsDecodeFailed | - Error::EofDuringFrame - => true, - _ => false, - } + Error::EofDuringFrame) } } diff --git a/framed/src/lib.rs b/framed/src/lib.rs index 728ad5a..9fdffd5 100644 --- a/framed/src/lib.rs +++ b/framed/src/lib.rs @@ -40,10 +40,10 @@ //! //! ## Cargo feature flags //! -//! `use_std`: Use standard library. Enabled by default, disable for no_std. +//! `use_std`: Use standard library. Enabled by default, disable for `no_std`. //! //! `use_nightly`: Enables unstable features that only work on nightly rust. -//! Required for practical no_std use. +//! Required for practical `no_std` use. //! //! `trace`: Enable to print all data to stdout for testing. //! @@ -247,7 +247,7 @@ impl Deref for BoxPayload { type Target = [u8]; fn deref(&self) -> &[u8] { - &*self.0 + &self.0 } } @@ -268,7 +268,7 @@ impl Deref for BoxEncoded { type Target = [u8]; fn deref(&self) -> &[u8] { - &*self.0 + &self.0 } } diff --git a/framed/src/typed.rs b/framed/src/typed.rs index b44016f..fac1a9d 100644 --- a/framed/src/typed.rs +++ b/framed/src/typed.rs @@ -129,7 +129,7 @@ impl Clone for Config { fn clone(&self) -> Config { Config:: { bytes_config: self.bytes_config.clone(), - _phantom: PhantomData::::default(), + _phantom: PhantomData::, } } } @@ -138,7 +138,7 @@ impl Config { pub(crate) fn new(bytes_config: &bytes::Config) -> Config { Config:: { bytes_config: bytes_config.clone(), - _phantom: PhantomData::::default(), + _phantom: PhantomData::, } } @@ -146,7 +146,7 @@ impl Config { pub fn to_codec(&mut self) -> Codec { Codec:: { bytes_codec: self.bytes_config.to_codec(), - _phantom: PhantomData::::default(), + _phantom: PhantomData::, } } @@ -155,7 +155,7 @@ impl Config { pub fn to_receiver(&mut self, r: R) -> Receiver { Receiver:: { codec: self.to_codec(), - r: r, + r, } } @@ -164,7 +164,7 @@ impl Config { pub fn to_sender(&mut self, w: W) -> Sender { Sender:: { codec: self.to_codec(), - w: w, + w, } } } @@ -186,7 +186,7 @@ impl Codec { /// /// ## Examples /// - /// See the [no_std usage example](index.html#example-usage-from-a-no_std-crate) + /// See the [`no_std` usage example](index.html#example-usage-from-a-no_std-crate) /// in the `typed` module documentation. pub fn encode_to_slice( &mut self, @@ -217,7 +217,7 @@ impl Codec { /// /// ## Examples /// - /// See the [no_std usage example](index.html#example-usage-from-a-no_std-crate) + /// See the [`no_std` usage example](index.html#example-usage-from-a-no_std-crate) /// in the `typed` module documentation. pub fn decode_from_slice( &mut self, @@ -306,7 +306,7 @@ impl Sender { let ser_len = ssmarshal::serialize(&mut ser_buf, v)?; let ser = &ser_buf[0..ser_len]; - self.codec.bytes_codec.encode_to_writer(&ser, &mut self.w) + self.codec.bytes_codec.encode_to_writer(ser, &mut self.w) } /// Encode the supplied payload as a frame, write it to the @@ -349,7 +349,7 @@ impl Receiver { pub fn recv(&mut self) -> Result { let payload = self.codec.bytes_codec.decode_from_reader::(&mut self.r)?; - let (v, _len) = ssmarshal::deserialize(&*payload)?; + let (v, _len) = ssmarshal::deserialize(&payload)?; Ok(v) } } @@ -394,7 +394,7 @@ mod tests { a: [1, 2, 3], }; #[cfg(feature = "use_std")] { - println!("Test value: {:#?}", v); + println!("Test value: {v:#?}"); } v } @@ -475,7 +475,7 @@ mod tests { let v = test_val(); tx.send(&v).unwrap(); let r = rx.recv().unwrap(); - println!("r: {:#?}", r); + println!("r: {r:#?}"); assert_eq!(v, r); } @@ -484,7 +484,7 @@ mod tests { let (mut _tx, mut rx) = pair(); match rx.recv() { Err(Error::EofBeforeFrame) => (), - e @ _ => panic!("Bad value: {:?}", e) + e => panic!("Bad value: {:?}", e) } }