Skip to content

Commit

Permalink
Replace a panic with an error branch
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
jrvanwhy committed Apr 23, 2024
1 parent 0b8b529 commit 5d6c8ce
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 @@ -3373,10 +3373,10 @@ pub unsafe trait IntoBytes {
Self: NoCell,
{
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 panicing.
bytes.get_mut(start..)?.copy_from_slice(self.as_bytes());
Some(())
}

Expand Down

0 comments on commit 5d6c8ce

Please sign in to comment.