Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some ZeroVec stacked borrows fixes #3513

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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