From 52878cc7a01346a9ca3c6d01f71cb6fed86eed40 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. 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). --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 97a19ab319..52df74c318 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(()) }