Skip to content

Commit

Permalink
Improve visit_seq implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sgued committed Dec 16, 2022
1 parent b56f836 commit 8fcf21f
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/bytearray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,16 @@ impl<'de, const N: usize> Visitor<'de> for ByteArrayVisitor<N> {
write!(formatter, "a byte array of length {}", N)
}

fn visit_seq<V>(self, mut visitor: V) -> Result<ByteArray<N>, V::Error>
fn visit_seq<V>(self, mut seq: V) -> Result<ByteArray<N>, V::Error>
where
V: SeqAccess<'de>,
{
let mut bytes = [0; N];
let mut idx = 0;
while let Some(b) = visitor.next_element()? {
bytes[idx] = b;
idx += 1;
if idx == N {
break;
}
}

if idx != N {
return Err(V::Error::invalid_length(N, &self));
for (idx, byte) in bytes.iter_mut().enumerate() {
*byte = seq
.next_element()?
.ok_or_else(|| V::Error::invalid_length(idx, &self))?;
}

Ok(ByteArray::from(bytes))
Expand Down

0 comments on commit 8fcf21f

Please sign in to comment.