From ba278ac193f2fa0eb706e73af4f7b36555e715ed Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:10:14 +0200 Subject: [PATCH] Fix panic in `BufferContents` derive expansion and wrong safety docs (#2587) --- vulkano-macros/src/derive_buffer_contents.rs | 2 -- vulkano/src/buffer/subbuffer.rs | 6 +----- vulkano/src/padded.rs | 4 +--- 3 files changed, 2 insertions(+), 10 deletions(-) diff --git a/vulkano-macros/src/derive_buffer_contents.rs b/vulkano-macros/src/derive_buffer_contents.rs index 378cf660..e27dc7b9 100644 --- a/vulkano-macros/src/derive_buffer_contents.rs +++ b/vulkano-macros/src/derive_buffer_contents.rs @@ -95,8 +95,6 @@ pub fn derive_buffer_contents(crate_ident: &Ident, mut ast: DeriveInput) -> Resu }; ptr_from_slice = quote! { - debug_assert_eq!(slice.len(), ::std::mem::size_of::()); - <*mut [u8]>::cast::(slice.as_ptr()) }; } diff --git a/vulkano/src/buffer/subbuffer.rs b/vulkano/src/buffer/subbuffer.rs index 3ed53344..e122af42 100644 --- a/vulkano/src/buffer/subbuffer.rs +++ b/vulkano/src/buffer/subbuffer.rs @@ -839,9 +839,7 @@ pub unsafe trait BufferContents: Send + Sync + 'static { /// /// # Safety /// - /// - If `Self` is sized, then `slice.len()` must match the size exactly. - /// - 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. + /// - `slice` must be a pointer that's valid for reads and writes of the entire slice. #[doc(hidden)] unsafe fn ptr_from_slice(slice: NonNull<[u8]>) -> *mut Self; } @@ -854,8 +852,6 @@ where #[inline(always)] unsafe fn ptr_from_slice(slice: NonNull<[u8]>) -> *mut Self { - debug_assert_eq!(slice.len(), size_of::()); - <*mut [u8]>::cast::(slice.as_ptr()) } } diff --git a/vulkano/src/padded.rs b/vulkano/src/padded.rs index 4f3d3493..e8f8b7f1 100644 --- a/vulkano/src/padded.rs +++ b/vulkano/src/padded.rs @@ -8,7 +8,7 @@ use std::{ cmp::Ordering, fmt::{Debug, Display, Formatter, Result as FmtResult}, hash::{Hash, Hasher}, - mem::{size_of, MaybeUninit}, + mem::MaybeUninit, ops::{Deref, DerefMut}, ptr::NonNull, }; @@ -290,8 +290,6 @@ where const LAYOUT: BufferContentsLayout = BufferContentsLayout::from_sized(Layout::new::()); unsafe fn ptr_from_slice(slice: NonNull<[u8]>) -> *mut Self { - debug_assert_eq!(slice.len(), size_of::>()); - <*mut [u8]>::cast::>(slice.as_ptr()) } }