Fix panic in BufferContents derive expansion and wrong safety docs (#2587)

This commit is contained in:
marc0246 2024-10-22 17:10:14 +02:00 committed by GitHub
parent 82acabc0fb
commit ba278ac193
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 2 additions and 10 deletions

View File

@ -95,8 +95,6 @@ pub fn derive_buffer_contents(crate_ident: &Ident, mut ast: DeriveInput) -> Resu
}; };
ptr_from_slice = quote! { ptr_from_slice = quote! {
debug_assert_eq!(slice.len(), ::std::mem::size_of::<Self>());
<*mut [u8]>::cast::<Self>(slice.as_ptr()) <*mut [u8]>::cast::<Self>(slice.as_ptr())
}; };
} }

View File

@ -839,9 +839,7 @@ pub unsafe trait BufferContents: Send + Sync + 'static {
/// ///
/// # Safety /// # Safety
/// ///
/// - If `Self` is sized, then `slice.len()` must match the size exactly. /// - `slice` must be a pointer that's valid for reads and writes of the entire slice.
/// - If `Self` is unsized, then `slice.len()` minus the size of the head (sized part) of the
/// DST must be evenly divisible by the size of the element type.
#[doc(hidden)] #[doc(hidden)]
unsafe fn ptr_from_slice(slice: NonNull<[u8]>) -> *mut Self; unsafe fn ptr_from_slice(slice: NonNull<[u8]>) -> *mut Self;
} }
@ -854,8 +852,6 @@ where
#[inline(always)] #[inline(always)]
unsafe fn ptr_from_slice(slice: NonNull<[u8]>) -> *mut Self { unsafe fn ptr_from_slice(slice: NonNull<[u8]>) -> *mut Self {
debug_assert_eq!(slice.len(), size_of::<T>());
<*mut [u8]>::cast::<T>(slice.as_ptr()) <*mut [u8]>::cast::<T>(slice.as_ptr())
} }
} }

View File

@ -8,7 +8,7 @@ use std::{
cmp::Ordering, cmp::Ordering,
fmt::{Debug, Display, Formatter, Result as FmtResult}, fmt::{Debug, Display, Formatter, Result as FmtResult},
hash::{Hash, Hasher}, hash::{Hash, Hasher},
mem::{size_of, MaybeUninit}, mem::MaybeUninit,
ops::{Deref, DerefMut}, ops::{Deref, DerefMut},
ptr::NonNull, ptr::NonNull,
}; };
@ -290,8 +290,6 @@ where
const LAYOUT: BufferContentsLayout = BufferContentsLayout::from_sized(Layout::new::<Self>()); const LAYOUT: BufferContentsLayout = BufferContentsLayout::from_sized(Layout::new::<Self>());
unsafe fn ptr_from_slice(slice: NonNull<[u8]>) -> *mut Self { unsafe fn ptr_from_slice(slice: NonNull<[u8]>) -> *mut Self {
debug_assert_eq!(slice.len(), size_of::<Padded<T, N>>());
<*mut [u8]>::cast::<Padded<T, N>>(slice.as_ptr()) <*mut [u8]>::cast::<Padded<T, N>>(slice.as_ptr())
} }
} }