Skip to content

Commit 54dc8da

Browse files
authored
Replace a panic with an error branch (#544)
This panic is unreachable. Panicxing is heavyweight, so we're trying to reduce panics (issue #202). This PR replaces the panic with a branch that should be lighter-weight (if it is not optimized away entirely).
1 parent cd82ae7 commit 54dc8da

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3399,10 +3399,10 @@ pub unsafe trait IntoBytes {
33993399
Self: Immutable,
34003400
{
34013401
let start = bytes.len().checked_sub(mem::size_of_val(self))?;
3402-
bytes
3403-
.get_mut(start..)
3404-
.expect("`start` should be in-bounds of `bytes`")
3405-
.copy_from_slice(self.as_bytes());
3402+
// get_mut() should never return None here. We use ? rather than
3403+
// .unwrap() because in the event the branch is not optimized away,
3404+
// returning None is generally lighter-weight than panicking.
3405+
bytes.get_mut(start..)?.copy_from_slice(self.as_bytes());
34063406
Some(())
34073407
}
34083408

0 commit comments

Comments
 (0)