From e70128725b774a2b642363aa9494e30677fe3afe Mon Sep 17 00:00:00 2001 From: Johnathan Van Why Date: Tue, 24 Oct 2023 14:06:37 -0700 Subject: [PATCH] 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). --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 84515b1837..3ad0d8c823 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(()) }