-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[Merged by Bors] - Pointerfication followup: Type safety and cleanup #4621
Conversation
Adding the Controversial tag due to the pointery trickery involved, to make sure we get Cart to review this before merging. I don't expect much pushback on these changes though; they're very nice. |
crates/bevy_ecs/src/ptr.rs
Outdated
#[inline] | ||
pub fn inner(&self) -> NonNull<u8> { | ||
self.0 | ||
pub fn cast<T>(&self) -> NonNull<T> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh this is dramatically nicer. I love this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quite straightforward. I really like the increased clarity and type safety we get here.
All the use sites you changed seem to immediately |
Main reason I chose Ideally, moving the pointer casts and conversions into |
If you know the underlying type you should be calling Now that ptrification is merged we should add debug checks to those 3 methods, storing Put another way- |
@BoxyUwU made the changes requested, ended up adding
|
IMO we should revert |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do somewhat think we ought to rename the offset
methods to byte_offset
to make it clear at use sites whats going on but that could be done in a separate PR
/// associated lifetime. | ||
#[inline] | ||
#[allow(clippy::wrong_self_convention)] | ||
pub unsafe fn as_ptr(self) -> *mut u8 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this doesn't really need to be an unsafe fn it could be fn as_ptr(&self) -> *mut u8
to be maximally flexible but it probably doesn't matter
@@ -186,7 +186,7 @@ impl std::fmt::Debug for ComponentDescriptor { | |||
impl ComponentDescriptor { | |||
// SAFETY: The pointer points to a valid value of type `T` and it is safe to drop this value. | |||
unsafe fn drop_ptr<T>(x: OwningPtr<'_>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure we really need ComponentDescriptor::drop_ptr
anymore considering you can just do OwningPtr::drop_as
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually tried removing this and there was a lifetime mismatch. Using OwningPtr::drop_as::<T>
doesn't satisfy for<'a> unsafe fn...
. Is there a way to make this work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh... that makes sense I guess, oh well ._.
bors r+ |
# Objective The `Ptr` types gives free access to the underlying `NonNull<u8>`, which adds more publicly visible pointer wrangling than there needs to be. There are also a few edge cases where Ptr types could be more readily utilized for properly validating the soundness of ECS operations. ## Solution - Replace `*Ptr(Mut)::inner` with `cast` which requires a concrete type to give the pointer. This function could also have a `debug_assert` with an alignment check to ensure that the pointer is aligned properly, but is currently not included. - Use `OwningPtr::read` in ECS macros over casting the inner pointer around.
bors r- |
Canceled. |
bors r+ |
# Objective The `Ptr` types gives free access to the underlying `NonNull<u8>`, which adds more publicly visible pointer wrangling than there needs to be. There are also a few edge cases where Ptr types could be more readily utilized for properly validating the soundness of ECS operations. ## Solution - Replace `*Ptr(Mut)::inner` with `cast` which requires a concrete type to give the pointer. This function could also have a `debug_assert` with an alignment check to ensure that the pointer is aligned properly, but is currently not included. - Use `OwningPtr::read` in ECS macros over casting the inner pointer around.
# Objective The `Ptr` types gives free access to the underlying `NonNull<u8>`, which adds more publicly visible pointer wrangling than there needs to be. There are also a few edge cases where Ptr types could be more readily utilized for properly validating the soundness of ECS operations. ## Solution - Replace `*Ptr(Mut)::inner` with `cast` which requires a concrete type to give the pointer. This function could also have a `debug_assert` with an alignment check to ensure that the pointer is aligned properly, but is currently not included. - Use `OwningPtr::read` in ECS macros over casting the inner pointer around.
# Objective The `Ptr` types gives free access to the underlying `NonNull<u8>`, which adds more publicly visible pointer wrangling than there needs to be. There are also a few edge cases where Ptr types could be more readily utilized for properly validating the soundness of ECS operations. ## Solution - Replace `*Ptr(Mut)::inner` with `cast` which requires a concrete type to give the pointer. This function could also have a `debug_assert` with an alignment check to ensure that the pointer is aligned properly, but is currently not included. - Use `OwningPtr::read` in ECS macros over casting the inner pointer around.
Objective
The
Ptr
types gives free access to the underlyingNonNull<u8>
, which adds more publicly visible pointer wrangling than there needs to be. There are also a few edge cases where Ptr types could be more readily utilized for properly validating the soundness of ECS operations.Solution
*Ptr(Mut)::inner
withcast
which requires a concrete type to give the pointer. This function could also have adebug_assert
with an alignment check to ensure that the pointer is aligned properly, but is currently not included.OwningPtr::read
in ECS macros over casting the inner pointer around.