Skip to content

Commit d13395c

Browse files
committed
Replace a panic with an error branch
This panic is unreachable. Panicing 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 b56c01b commit d13395c

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
@@ -3313,10 +3313,10 @@ pub unsafe trait IntoBytes {
33133313
Self: NoCell,
33143314
{
33153315
let start = bytes.len().checked_sub(mem::size_of_val(self))?;
3316-
bytes
3317-
.get_mut(start..)
3318-
.expect("`start` should be in-bounds of `bytes`")
3319-
.copy_from_slice(self.as_bytes());
3316+
// get_mut() should never return None here. We use ? rather than
3317+
// .unwrap() because in the event the branch is not optimized away,
3318+
// returning None is generally lighter-weight than panicing.
3319+
bytes.get_mut(start..)?.copy_from_slice(self.as_bytes());
33203320
Some(())
33213321
}
33223322

0 commit comments

Comments
 (0)