From 54dc8dac3c01f838e48e24db4cc69be250e9be87 Mon Sep 17 00:00:00 2001 From: Johnathan Van Why Date: Wed, 24 Apr 2024 11:05:10 -0700 Subject: [PATCH] Replace a panic with an error branch (#544) 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(()) }