Skip to content

Commit

Permalink
Fix FZS stacked borrow error (needs Rust 1.64) (#3513)
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth authored Jul 19, 2023
1 parent 3b23abe commit 61e022b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
14 changes: 7 additions & 7 deletions utils/zerovec/src/flexzerovec/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions utils/zerovec/src/ule/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,8 @@ unsafe impl<U: VarULE + ?Sized> VarULE for OptionVarULE<U> {

#[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)
}
}
Expand Down

0 comments on commit 61e022b

Please sign in to comment.