Skip to content

Commit 38936c4

Browse files
committed
Remove an unwrap() from the MessagePack code
I thought it might be interesting to audit these and see if any were obvious candidates for removal. This first_chunk() method is from Rust 1.77, so only became available to me with the v0.20.0 MSRV bump. (Though maybe there was a way to do this before. Oh well.)
1 parent e398dd1 commit 38936c4

1 file changed

Lines changed: 6 additions & 7 deletions

File tree

src/msgpack.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,12 @@ fn try_read_length<const N: usize, T, F>(input: &[u8], convert: F) -> Result<T,
238238
where
239239
F: FnOnce([u8; N]) -> T,
240240
{
241-
Ok(convert(
242-
input
243-
.get(1..1 + N)
244-
.ok_or(ReadSizeError::Truncated)?
245-
.try_into()
246-
.unwrap(),
247-
))
241+
let length_be_bytes = input
242+
.get(1..)
243+
.and_then(|s| s.first_chunk::<N>())
244+
.ok_or(ReadSizeError::Truncated)?;
245+
246+
Ok(convert(*length_be_bytes))
248247
}
249248

250249
/// The error type returned by [`next_value_size`].

0 commit comments

Comments
 (0)