Skip to content

Commit 83f1cb7

Browse files
Simplify deserialize implementation
1 parent 2dd9b18 commit 83f1cb7

File tree

1 file changed

+2
-17
lines changed

1 file changed

+2
-17
lines changed

src/lib.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -602,29 +602,14 @@ impl<'de, const N: usize> Deserialize<'de> for Bytes<N> {
602602
type Value = Bytes<N>;
603603

604604
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
605-
formatter.write_str("a sequence")
605+
formatter.write_str("a sequence of bytes")
606606
}
607607

608608
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
609609
where
610610
E: serde::de::Error,
611611
{
612-
if v.len() > N {
613-
// hprintln!("error! own size: {}, data size: {}", N::to_usize(), v.len()).ok();
614-
// return Err(E::invalid_length(values.capacity() + 1, &self))?;
615-
return Err(E::invalid_length(v.len(), &self))?;
616-
}
617-
let mut buf: Vec<u8, N> = Vec::new();
618-
// avoid unwrapping even though redundant
619-
match buf.extend_from_slice(v) {
620-
Ok(()) => {}
621-
Err(()) => {
622-
// hprintln!("error! own size: {}, data size: {}", N::to_usize(), v.len()).ok();
623-
// return Err(E::invalid_length(values.capacity() + 1, &self))?;
624-
return Err(E::invalid_length(v.len(), &self))?;
625-
}
626-
}
627-
Ok(Bytes::<N>::from(buf))
612+
Bytes::try_from(v).map_err(|()| E::invalid_length(v.len(), &self))
628613
}
629614

630615
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>

0 commit comments

Comments
 (0)