Skip to content

Commit 167c622

Browse files
committed
Improve visit_seq implementation
1 parent 4c24ac8 commit 167c622

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/bytearray.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,22 +181,16 @@ impl<'de, const N: usize> Visitor<'de> for ByteArrayVisitor<N> {
181181
write!(formatter, "a byte array of length {}", N)
182182
}
183183

184-
fn visit_seq<V>(self, mut visitor: V) -> Result<ByteArray<N>, V::Error>
184+
fn visit_seq<V>(self, mut seq: V) -> Result<ByteArray<N>, V::Error>
185185
where
186186
V: SeqAccess<'de>,
187187
{
188188
let mut bytes = [0; N];
189-
let mut idx = 0;
190-
while let Some(b) = visitor.next_element()? {
191-
bytes[idx] = b;
192-
idx += 1;
193-
if idx == N {
194-
break;
195-
}
196-
}
197189

198-
if idx != N {
199-
return Err(V::Error::invalid_length(N, &self));
190+
for (idx, byte) in bytes.iter_mut().enumerate() {
191+
*byte = seq
192+
.next_element()?
193+
.ok_or_else(|| V::Error::invalid_length(idx, &self))?;
200194
}
201195

202196
Ok(ByteArray::from(bytes))

0 commit comments

Comments
 (0)