From d63b7e9568575e4a76733a5ed8ec6f34dc6e26d8 Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Fri, 6 May 2022 19:15:24 +0000 Subject: [PATCH] some cleanup for `bevy_ptr` (#4668) 1. change `PtrMut::as_ptr(self)` and `OwnedPtr::as_ptr(self)` to take `&self`, otherwise printing the pointer will prevent doing anything else afterwards 2. make all `as_ptr` methods safe. There's nothing unsafe about obtaining a pointer, these kinds of methods are safe in std as well [str::as_ptr](https://doc.rust-lang.org/stable/std/primitive.str.html#method.as_ptr), [Rc::as_ptr](https://doc.rust-lang.org/stable/std/rc/struct.Rc.html#method.as_ptr) 3. rename `offset`/`add` to `byte_offset`/`byte_add`. The unprefixed methods in std add in increments of `std::mem::size_of::`, not in bytes. There's a PR for rust to add these byte_ methods https://github.com/rust-lang/rust/pull/95643 and at the call site it makes it much more clear that you need to do `.byte_add(i * layout_size)` instead of `.add(i)` --- crates/bevy_ecs/src/storage/blob_vec.rs | 6 +++--- crates/bevy_ptr/src/lib.rs | 22 +++++----------------- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/crates/bevy_ecs/src/storage/blob_vec.rs b/crates/bevy_ecs/src/storage/blob_vec.rs index c2902553e7..7b5d8fd801 100644 --- a/crates/bevy_ecs/src/storage/blob_vec.rs +++ b/crates/bevy_ecs/src/storage/blob_vec.rs @@ -212,7 +212,7 @@ impl BlobVec { #[inline] pub unsafe fn get_unchecked(&self, index: usize) -> Ptr<'_> { debug_assert!(index < self.len()); - self.get_ptr().add(index * self.item_layout.size()) + self.get_ptr().byte_add(index * self.item_layout.size()) } /// # Safety @@ -221,7 +221,7 @@ impl BlobVec { pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> PtrMut<'_> { debug_assert!(index < self.len()); let layout_size = self.item_layout.size(); - self.get_ptr_mut().add(index * layout_size) + self.get_ptr_mut().byte_add(index * layout_size) } /// Gets a [`Ptr`] to the start of the vec @@ -258,7 +258,7 @@ impl BlobVec { unsafe { // NOTE: this doesn't use self.get_unchecked(i) because the debug_assert on index // will panic here due to self.len being set to 0 - let ptr = self.get_ptr_mut().add(i * layout_size).promote(); + let ptr = self.get_ptr_mut().byte_add(i * layout_size).promote(); (drop)(ptr); } } diff --git a/crates/bevy_ptr/src/lib.rs b/crates/bevy_ptr/src/lib.rs index cb3cc2abb3..d590d0bfb1 100644 --- a/crates/bevy_ptr/src/lib.rs +++ b/crates/bevy_ptr/src/lib.rs @@ -55,7 +55,7 @@ macro_rules! impl_ptr { /// /// [ptr_offset]: https://doc.rust-lang.org/std/primitive.pointer.html#method.offset #[inline] - pub unsafe fn offset(self, count: isize) -> Self { + pub unsafe fn byte_offset(self, count: isize) -> Self { Self( NonNull::new_unchecked(self.as_ptr().offset(count)), PhantomData, @@ -73,7 +73,7 @@ macro_rules! impl_ptr { /// /// [ptr_add]: https://doc.rust-lang.org/std/primitive.pointer.html#method.add #[inline] - pub unsafe fn add(self, count: usize) -> Self { + pub unsafe fn byte_add(self, count: usize) -> Self { Self( NonNull::new_unchecked(self.as_ptr().add(count)), PhantomData, @@ -116,13 +116,9 @@ impl<'a> Ptr<'a> { /// /// If possible, it is strongly encouraged to use [`deref`](Self::deref) over this function, /// as it retains the lifetime. - /// - /// # Safety - /// All subsequent operations to the returned pointer must be valid inside the - /// associated lifetime. #[inline] #[allow(clippy::wrong_self_convention)] - pub unsafe fn as_ptr(self) -> *mut u8 { + pub fn as_ptr(self) -> *mut u8 { self.0.as_ptr() } } @@ -150,13 +146,9 @@ impl<'a> PtrMut<'a> { /// /// If possible, it is strongly encouraged to use [`deref_mut`](Self::deref_mut) over /// this function, as it retains the lifetime. - /// - /// # Safety - /// All subsequent operations to the returned pointer must be valid inside the - /// associated lifetime. #[inline] #[allow(clippy::wrong_self_convention)] - pub unsafe fn as_ptr(self) -> *mut u8 { + pub fn as_ptr(&self) -> *mut u8 { self.0.as_ptr() } } @@ -192,13 +184,9 @@ impl<'a> OwningPtr<'a> { /// /// If possible, it is strongly encouraged to use the other more type-safe functions /// over this function. - /// - /// # Safety - /// All subsequent operations to the returned pointer must be valid inside the - /// associated lifetime. #[inline] #[allow(clippy::wrong_self_convention)] - pub unsafe fn as_ptr(self) -> *mut u8 { + pub fn as_ptr(&self) -> *mut u8 { self.0.as_ptr() } }