From b9842fcb1860eec68506b7956a3f5c18637b9606 Mon Sep 17 00:00:00 2001 From: hinto-janai Date: Wed, 18 Sep 2024 18:12:35 -0400 Subject: [PATCH] fixed-bytes: enable workspace lints (#293) --- net/fixed-bytes/Cargo.toml | 3 +++ net/fixed-bytes/src/lib.rs | 50 ++++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/net/fixed-bytes/Cargo.toml b/net/fixed-bytes/Cargo.toml index 4c5a1af..7844570 100644 --- a/net/fixed-bytes/Cargo.toml +++ b/net/fixed-bytes/Cargo.toml @@ -17,3 +17,6 @@ serde = { workspace = true, features = ["derive"], optional = true } [dev-dependencies] serde_json = { workspace = true, features = ["std"] } + +[lints] +workspace = true \ No newline at end of file diff --git a/net/fixed-bytes/src/lib.rs b/net/fixed-bytes/src/lib.rs index 2e8f1bc..b1b064b 100644 --- a/net/fixed-bytes/src/lib.rs +++ b/net/fixed-bytes/src/lib.rs @@ -22,17 +22,15 @@ pub enum FixedByteError { } impl FixedByteError { - fn field_name(&self) -> &'static str { + const fn field_name(&self) -> &'static str { match self { - FixedByteError::InvalidLength => "input", + Self::InvalidLength => "input", } } - fn field_data(&self) -> &'static str { + const fn field_data(&self) -> &'static str { match self { - FixedByteError::InvalidLength => { - "Cannot create fix byte array, input has invalid length." - } + Self::InvalidLength => "Cannot create fix byte array, input has invalid length.", } } } @@ -82,7 +80,7 @@ impl ByteArray { impl From<[u8; N]> for ByteArray { fn from(value: [u8; N]) -> Self { - ByteArray(Bytes::copy_from_slice(&value)) + Self(Bytes::copy_from_slice(&value)) } } @@ -101,7 +99,7 @@ impl TryFrom for ByteArray { if value.len() != N { return Err(FixedByteError::InvalidLength); } - Ok(ByteArray(value)) + Ok(Self(value)) } } @@ -112,7 +110,7 @@ impl TryFrom> for ByteArray { if value.len() != N { return Err(FixedByteError::InvalidLength); } - Ok(ByteArray(Bytes::from(value))) + Ok(Self(Bytes::from(value))) } } @@ -142,11 +140,11 @@ impl<'de, const N: usize> Deserialize<'de> for ByteArrayVec { } impl ByteArrayVec { - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.0.len() / N } - pub fn is_empty(&self) -> bool { + pub const fn is_empty(&self) -> bool { self.len() == 0 } @@ -162,6 +160,7 @@ impl ByteArrayVec { /// /// # Panics /// Panics if at > len. + #[must_use] pub fn split_off(&mut self, at: usize) -> Self { Self(self.0.split_off(at * N)) } @@ -169,9 +168,9 @@ impl ByteArrayVec { impl From<&ByteArrayVec> for Vec<[u8; N]> { fn from(value: &ByteArrayVec) -> Self { - let mut out = Vec::with_capacity(value.len()); + let mut out = Self::with_capacity(value.len()); for i in 0..value.len() { - out.push(value[i]) + out.push(value[i]); } out @@ -181,11 +180,11 @@ impl From<&ByteArrayVec> for Vec<[u8; N]> { impl From> for ByteArrayVec { fn from(value: Vec<[u8; N]>) -> Self { let mut bytes = BytesMut::with_capacity(N * value.len()); - for i in value.into_iter() { - bytes.extend_from_slice(&i) + for i in value { + bytes.extend_from_slice(&i); } - ByteArrayVec(bytes.freeze()) + Self(bytes.freeze()) } } @@ -197,13 +196,13 @@ impl TryFrom for ByteArrayVec { return Err(FixedByteError::InvalidLength); } - Ok(ByteArrayVec(value)) + Ok(Self(value)) } } impl From<[u8; N]> for ByteArrayVec { fn from(value: [u8; N]) -> Self { - ByteArrayVec(Bytes::copy_from_slice(value.as_slice())) + Self(Bytes::copy_from_slice(value.as_slice())) } } @@ -211,11 +210,11 @@ impl From<[[u8; N]; LEN]> for ByteArrayVec fn from(value: [[u8; N]; LEN]) -> Self { let mut bytes = BytesMut::with_capacity(N * LEN); - for val in value.into_iter() { + for val in value { bytes.put_slice(val.as_slice()); } - ByteArrayVec(bytes.freeze()) + Self(bytes.freeze()) } } @@ -227,7 +226,7 @@ impl TryFrom> for ByteArrayVec { return Err(FixedByteError::InvalidLength); } - Ok(ByteArrayVec(Bytes::from(value))) + Ok(Self(Bytes::from(value))) } } @@ -235,9 +234,12 @@ impl Index for ByteArrayVec { type Output = [u8; N]; fn index(&self, index: usize) -> &Self::Output { - if (index + 1) * N > self.0.len() { - panic!("Index out of range, idx: {}, length: {}", index, self.len()); - } + assert!( + (index + 1) * N <= self.0.len(), + "Index out of range, idx: {}, length: {}", + index, + self.len() + ); self.0[index * N..(index + 1) * N] .as_ref()