Skip to content

Commit 05e9eea

Browse files
jakobhellermannItsDoot
authored andcommitted
some cleanup for bevy_ptr (bevyengine#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::<T>`, not in bytes. There's a PR for rust to add these byte_ methods rust-lang/rust#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)`
1 parent dcffedc commit 05e9eea

File tree

2 files changed

+8
-20
lines changed

2 files changed

+8
-20
lines changed

crates/bevy_ecs/src/storage/blob_vec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl BlobVec {
212212
#[inline]
213213
pub unsafe fn get_unchecked(&self, index: usize) -> Ptr<'_> {
214214
debug_assert!(index < self.len());
215-
self.get_ptr().add(index * self.item_layout.size())
215+
self.get_ptr().byte_add(index * self.item_layout.size())
216216
}
217217

218218
/// # Safety
@@ -221,7 +221,7 @@ impl BlobVec {
221221
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> PtrMut<'_> {
222222
debug_assert!(index < self.len());
223223
let layout_size = self.item_layout.size();
224-
self.get_ptr_mut().add(index * layout_size)
224+
self.get_ptr_mut().byte_add(index * layout_size)
225225
}
226226

227227
/// Gets a [`Ptr`] to the start of the vec
@@ -258,7 +258,7 @@ impl BlobVec {
258258
unsafe {
259259
// NOTE: this doesn't use self.get_unchecked(i) because the debug_assert on index
260260
// will panic here due to self.len being set to 0
261-
let ptr = self.get_ptr_mut().add(i * layout_size).promote();
261+
let ptr = self.get_ptr_mut().byte_add(i * layout_size).promote();
262262
(drop)(ptr);
263263
}
264264
}

crates/bevy_ptr/src/lib.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ macro_rules! impl_ptr {
5555
///
5656
/// [ptr_offset]: https://doc.rust-lang.org/std/primitive.pointer.html#method.offset
5757
#[inline]
58-
pub unsafe fn offset(self, count: isize) -> Self {
58+
pub unsafe fn byte_offset(self, count: isize) -> Self {
5959
Self(
6060
NonNull::new_unchecked(self.as_ptr().offset(count)),
6161
PhantomData,
@@ -73,7 +73,7 @@ macro_rules! impl_ptr {
7373
///
7474
/// [ptr_add]: https://doc.rust-lang.org/std/primitive.pointer.html#method.add
7575
#[inline]
76-
pub unsafe fn add(self, count: usize) -> Self {
76+
pub unsafe fn byte_add(self, count: usize) -> Self {
7777
Self(
7878
NonNull::new_unchecked(self.as_ptr().add(count)),
7979
PhantomData,
@@ -116,13 +116,9 @@ impl<'a> Ptr<'a> {
116116
///
117117
/// If possible, it is strongly encouraged to use [`deref`](Self::deref) over this function,
118118
/// as it retains the lifetime.
119-
///
120-
/// # Safety
121-
/// All subsequent operations to the returned pointer must be valid inside the
122-
/// associated lifetime.
123119
#[inline]
124120
#[allow(clippy::wrong_self_convention)]
125-
pub unsafe fn as_ptr(self) -> *mut u8 {
121+
pub fn as_ptr(self) -> *mut u8 {
126122
self.0.as_ptr()
127123
}
128124
}
@@ -150,13 +146,9 @@ impl<'a> PtrMut<'a> {
150146
///
151147
/// If possible, it is strongly encouraged to use [`deref_mut`](Self::deref_mut) over
152148
/// this function, as it retains the lifetime.
153-
///
154-
/// # Safety
155-
/// All subsequent operations to the returned pointer must be valid inside the
156-
/// associated lifetime.
157149
#[inline]
158150
#[allow(clippy::wrong_self_convention)]
159-
pub unsafe fn as_ptr(self) -> *mut u8 {
151+
pub fn as_ptr(&self) -> *mut u8 {
160152
self.0.as_ptr()
161153
}
162154
}
@@ -192,13 +184,9 @@ impl<'a> OwningPtr<'a> {
192184
///
193185
/// If possible, it is strongly encouraged to use the other more type-safe functions
194186
/// over this function.
195-
///
196-
/// # Safety
197-
/// All subsequent operations to the returned pointer must be valid inside the
198-
/// associated lifetime.
199187
#[inline]
200188
#[allow(clippy::wrong_self_convention)]
201-
pub unsafe fn as_ptr(self) -> *mut u8 {
189+
pub fn as_ptr(&self) -> *mut u8 {
202190
self.0.as_ptr()
203191
}
204192
}

0 commit comments

Comments
 (0)