Skip to content

Commit

Permalink
Replace a panic with an error branch (#544)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jrvanwhy committed Apr 24, 2024
1 parent cd82ae7 commit 54dc8da
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3399,10 +3399,10 @@ pub unsafe trait IntoBytes {
Self: Immutable,
{
let start = bytes.len().checked_sub(mem::size_of_val(self))?;
bytes
.get_mut(start..)
.expect("`start` should be in-bounds of `bytes`")
.copy_from_slice(self.as_bytes());
// get_mut() should never return None here. We use ? rather than
// .unwrap() because in the event the branch is not optimized away,
// returning None is generally lighter-weight than panicking.
bytes.get_mut(start..)?.copy_from_slice(self.as_bytes());
Some(())
}

Expand Down

0 comments on commit 54dc8da

Please sign in to comment.