Skip to content

Commit

Permalink
Replace a panic in IntoBytes::write_to_suffix 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 google#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 Mar 29, 2024
1 parent b56c01b commit aab9374
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 @@ -3313,10 +3313,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 aab9374

Please sign in to comment.