From 61e022b51633288c56aafdb4f4ca8797c5fedc4b Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Wed, 19 Jul 2023 15:15:31 -0700 Subject: [PATCH] Fix FZS stacked borrow error (needs Rust 1.64) (#3513) --- utils/zerovec/src/flexzerovec/slice.rs | 14 +++++++------- utils/zerovec/src/ule/option.rs | 3 +-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/utils/zerovec/src/flexzerovec/slice.rs b/utils/zerovec/src/flexzerovec/slice.rs index 1499c2e032e..41cb7116f90 100644 --- a/utils/zerovec/src/flexzerovec/slice.rs +++ b/utils/zerovec/src/flexzerovec/slice.rs @@ -134,18 +134,18 @@ impl FlexZeroSlice { // equal to the length of the `data` field, which will be one less than the length of the // overall array. #[allow(clippy::panic)] // panic is documented in function contract - let (_, remainder) = match bytes.split_last() { - Some(v) => v, - None => panic!("slice should be non-empty"), - }; - &*(remainder as *const [u8] as *const Self) + if bytes.is_empty() { + panic!("from_byte_slice_unchecked called with empty slice") + } + let slice = core::ptr::slice_from_raw_parts(bytes.as_ptr(), bytes.len() - 1); + &*(slice as *const Self) } #[inline] pub(crate) unsafe fn from_byte_slice_mut_unchecked(bytes: &mut [u8]) -> &mut Self { // Safety: See comments in `from_byte_slice_unchecked` - let remainder = core::slice::from_raw_parts_mut(bytes.as_mut_ptr(), bytes.len() - 1); - &mut *(remainder as *mut [u8] as *mut Self) + let remainder = core::ptr::slice_from_raw_parts_mut(bytes.as_mut_ptr(), bytes.len() - 1); + &mut *(remainder as *mut Self) } /// Returns this slice as its underlying `&[u8]` byte buffer representation. diff --git a/utils/zerovec/src/ule/option.rs b/utils/zerovec/src/ule/option.rs index 50b193aac17..9b0dc5b28a1 100644 --- a/utils/zerovec/src/ule/option.rs +++ b/utils/zerovec/src/ule/option.rs @@ -197,9 +197,8 @@ unsafe impl VarULE for OptionVarULE { #[inline] unsafe fn from_byte_slice_unchecked(bytes: &[u8]) -> &Self { - let metadata = bytes.len() - 1; let entire_struct_as_slice: *const [u8] = - ::core::slice::from_raw_parts(bytes.as_ptr(), metadata); + ::core::ptr::slice_from_raw_parts(bytes.as_ptr(), bytes.len() - 1); &*(entire_struct_as_slice as *const Self) } }