Skip to content

Commit ecc8e22

Browse files
committed
Use a try_from instead of a cast
This is part of #101.
1 parent 525f90c commit ecc8e22

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/protocol/frame.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ where
4242
let len = Int32::read(&mut Cursor::new(len_buf))
4343
.expect("Reading Int32 from in-mem buffer should always work");
4444

45-
// convert to usize
46-
if len.0 < 0 {
47-
return Err(ReadError::NegativeMessageSize { size: len.0 });
48-
}
49-
let len = len.0 as usize;
45+
let len = match usize::try_from(len.0) {
46+
Ok(n) => n,
47+
Err(_) => return Err(ReadError::NegativeMessageSize { size: len.0 }),
48+
};
5049

5150
// check max message size to not blow up memory
5251
if len > max_message_size {

0 commit comments

Comments
 (0)