Skip to content

Commit b9842fc

Browse files
authored
fixed-bytes: enable workspace lints (#293)
1 parent 8b4b403 commit b9842fc

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

net/fixed-bytes/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ serde = { workspace = true, features = ["derive"], optional = true }
1717

1818
[dev-dependencies]
1919
serde_json = { workspace = true, features = ["std"] }
20+
21+
[lints]
22+
workspace = true

net/fixed-bytes/src/lib.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ pub enum FixedByteError {
2222
}
2323

2424
impl FixedByteError {
25-
fn field_name(&self) -> &'static str {
25+
const fn field_name(&self) -> &'static str {
2626
match self {
27-
FixedByteError::InvalidLength => "input",
27+
Self::InvalidLength => "input",
2828
}
2929
}
3030

31-
fn field_data(&self) -> &'static str {
31+
const fn field_data(&self) -> &'static str {
3232
match self {
33-
FixedByteError::InvalidLength => {
34-
"Cannot create fix byte array, input has invalid length."
35-
}
33+
Self::InvalidLength => "Cannot create fix byte array, input has invalid length.",
3634
}
3735
}
3836
}
@@ -82,7 +80,7 @@ impl<const N: usize> ByteArray<N> {
8280

8381
impl<const N: usize> From<[u8; N]> for ByteArray<N> {
8482
fn from(value: [u8; N]) -> Self {
85-
ByteArray(Bytes::copy_from_slice(&value))
83+
Self(Bytes::copy_from_slice(&value))
8684
}
8785
}
8886

@@ -101,7 +99,7 @@ impl<const N: usize> TryFrom<Bytes> for ByteArray<N> {
10199
if value.len() != N {
102100
return Err(FixedByteError::InvalidLength);
103101
}
104-
Ok(ByteArray(value))
102+
Ok(Self(value))
105103
}
106104
}
107105

@@ -112,7 +110,7 @@ impl<const N: usize> TryFrom<Vec<u8>> for ByteArray<N> {
112110
if value.len() != N {
113111
return Err(FixedByteError::InvalidLength);
114112
}
115-
Ok(ByteArray(Bytes::from(value)))
113+
Ok(Self(Bytes::from(value)))
116114
}
117115
}
118116

@@ -142,11 +140,11 @@ impl<'de, const N: usize> Deserialize<'de> for ByteArrayVec<N> {
142140
}
143141

144142
impl<const N: usize> ByteArrayVec<N> {
145-
pub fn len(&self) -> usize {
143+
pub const fn len(&self) -> usize {
146144
self.0.len() / N
147145
}
148146

149-
pub fn is_empty(&self) -> bool {
147+
pub const fn is_empty(&self) -> bool {
150148
self.len() == 0
151149
}
152150

@@ -162,16 +160,17 @@ impl<const N: usize> ByteArrayVec<N> {
162160
///
163161
/// # Panics
164162
/// Panics if at > len.
163+
#[must_use]
165164
pub fn split_off(&mut self, at: usize) -> Self {
166165
Self(self.0.split_off(at * N))
167166
}
168167
}
169168

170169
impl<const N: usize> From<&ByteArrayVec<N>> for Vec<[u8; N]> {
171170
fn from(value: &ByteArrayVec<N>) -> Self {
172-
let mut out = Vec::with_capacity(value.len());
171+
let mut out = Self::with_capacity(value.len());
173172
for i in 0..value.len() {
174-
out.push(value[i])
173+
out.push(value[i]);
175174
}
176175

177176
out
@@ -181,11 +180,11 @@ impl<const N: usize> From<&ByteArrayVec<N>> for Vec<[u8; N]> {
181180
impl<const N: usize> From<Vec<[u8; N]>> for ByteArrayVec<N> {
182181
fn from(value: Vec<[u8; N]>) -> Self {
183182
let mut bytes = BytesMut::with_capacity(N * value.len());
184-
for i in value.into_iter() {
185-
bytes.extend_from_slice(&i)
183+
for i in value {
184+
bytes.extend_from_slice(&i);
186185
}
187186

188-
ByteArrayVec(bytes.freeze())
187+
Self(bytes.freeze())
189188
}
190189
}
191190

@@ -197,25 +196,25 @@ impl<const N: usize> TryFrom<Bytes> for ByteArrayVec<N> {
197196
return Err(FixedByteError::InvalidLength);
198197
}
199198

200-
Ok(ByteArrayVec(value))
199+
Ok(Self(value))
201200
}
202201
}
203202

204203
impl<const N: usize> From<[u8; N]> for ByteArrayVec<N> {
205204
fn from(value: [u8; N]) -> Self {
206-
ByteArrayVec(Bytes::copy_from_slice(value.as_slice()))
205+
Self(Bytes::copy_from_slice(value.as_slice()))
207206
}
208207
}
209208

210209
impl<const N: usize, const LEN: usize> From<[[u8; N]; LEN]> for ByteArrayVec<N> {
211210
fn from(value: [[u8; N]; LEN]) -> Self {
212211
let mut bytes = BytesMut::with_capacity(N * LEN);
213212

214-
for val in value.into_iter() {
213+
for val in value {
215214
bytes.put_slice(val.as_slice());
216215
}
217216

218-
ByteArrayVec(bytes.freeze())
217+
Self(bytes.freeze())
219218
}
220219
}
221220

@@ -227,17 +226,20 @@ impl<const N: usize> TryFrom<Vec<u8>> for ByteArrayVec<N> {
227226
return Err(FixedByteError::InvalidLength);
228227
}
229228

230-
Ok(ByteArrayVec(Bytes::from(value)))
229+
Ok(Self(Bytes::from(value)))
231230
}
232231
}
233232

234233
impl<const N: usize> Index<usize> for ByteArrayVec<N> {
235234
type Output = [u8; N];
236235

237236
fn index(&self, index: usize) -> &Self::Output {
238-
if (index + 1) * N > self.0.len() {
239-
panic!("Index out of range, idx: {}, length: {}", index, self.len());
240-
}
237+
assert!(
238+
(index + 1) * N <= self.0.len(),
239+
"Index out of range, idx: {}, length: {}",
240+
index,
241+
self.len()
242+
);
241243

242244
self.0[index * N..(index + 1) * N]
243245
.as_ref()

0 commit comments

Comments
 (0)