-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b74ab7d
Followup: inner -> cast.
james7132 600f60a
Fix tests
james7132 46ca843
Revert inner -> cast.
james7132 2510881
as_ptr and drop_as
james7132 8693468
Formatting
james7132 7416fd3
as_ptr -> to_ptr
james7132 6e939e1
Inline
james7132 afdd684
Shut up clippy
james7132 62da3fd
Revert back to as_ptr, make pub again.
james7132 114bc59
Update docs
james7132 405fb89
Fix CI
james7132 c62d2e5
More docs fixes
james7132 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,57 +43,86 @@ pub struct OwningPtr<'a>(NonNull<u8>, PhantomData<&'a mut u8>); | |
macro_rules! impl_ptr { | ||
($ptr:ident) => { | ||
impl $ptr<'_> { | ||
/// Calculates the offset from a pointer. | ||
/// As the pointer is type-erased, there is no size information available. The provided | ||
/// `count` parameter is in raw bytes. | ||
/// | ||
/// *See also: [`ptr::offset`][ptr_offset]* | ||
/// | ||
/// # Safety | ||
/// the offset cannot make the existing ptr null, or take it out of bounds for its allocation. | ||
/// | ||
/// [ptr_offset]: https://doc.rust-lang.org/std/primitive.pointer.html#method.offset | ||
#[inline] | ||
pub unsafe fn offset(self, count: isize) -> Self { | ||
Self( | ||
NonNull::new_unchecked(self.0.as_ptr().offset(count)), | ||
NonNull::new_unchecked(self.as_ptr().offset(count)), | ||
PhantomData, | ||
) | ||
} | ||
|
||
/// Calculates the offset from a pointer (convenience for `.offset(count as isize)`). | ||
/// As the pointer is type-erased, there is no size information available. The provided | ||
/// `count` parameter is in raw bytes. | ||
/// | ||
/// *See also: [`ptr::add`][ptr_add]* | ||
/// | ||
/// # Safety | ||
/// the offset cannot make the existing ptr null, or take it out of bounds for its allocation. | ||
/// | ||
/// [ptr_add]: https://doc.rust-lang.org/std/primitive.pointer.html#method.add | ||
#[inline] | ||
pub unsafe fn add(self, count: usize) -> Self { | ||
Self( | ||
NonNull::new_unchecked(self.0.as_ptr().add(count)), | ||
NonNull::new_unchecked(self.as_ptr().add(count)), | ||
PhantomData, | ||
) | ||
} | ||
|
||
/// # Safety | ||
/// Creates a new instance from a raw pointer. | ||
/// | ||
/// # Safety | ||
/// The lifetime for the returned item must not exceed the lifetime `inner` is valid for | ||
#[inline] | ||
pub unsafe fn new(inner: NonNull<u8>) -> Self { | ||
Self(inner, PhantomData) | ||
} | ||
|
||
#[inline] | ||
pub fn inner(&self) -> NonNull<u8> { | ||
self.0 | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_ptr!(Ptr); | ||
impl<'a> Ptr<'a> { | ||
/// # Safety | ||
/// Transforms this [`Ptr`] into an [`PtrMut`] | ||
/// | ||
/// # Safety | ||
/// Another [`PtrMut`] for the same [`Ptr`] must not be created until the first is dropped. | ||
#[inline] | ||
pub unsafe fn assert_unique(self) -> PtrMut<'a> { | ||
PtrMut(self.0, PhantomData) | ||
} | ||
|
||
/// Transforms this [`Ptr<T>`] into a `&T` with the same lifetime | ||
/// | ||
/// # Safety | ||
/// Must point to a valid `T` | ||
#[inline] | ||
pub unsafe fn deref<T>(self) -> &'a T { | ||
&*self.0.as_ptr().cast() | ||
&*self.as_ptr().cast() | ||
} | ||
|
||
/// Gets the underlying pointer, erasing the associated lifetime. | ||
/// | ||
/// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
self.0.as_ptr() | ||
} | ||
} | ||
impl_ptr!(PtrMut); | ||
|
@@ -113,7 +142,21 @@ impl<'a> PtrMut<'a> { | |
/// Must point to a valid `T` | ||
#[inline] | ||
pub unsafe fn deref_mut<T>(self) -> &'a mut T { | ||
&mut *self.inner().as_ptr().cast() | ||
&mut *self.as_ptr().cast() | ||
} | ||
|
||
/// Gets the underlying pointer, erasing the associated lifetime. | ||
/// | ||
/// 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 { | ||
self.0.as_ptr() | ||
} | ||
} | ||
impl_ptr!(OwningPtr); | ||
|
@@ -132,7 +175,30 @@ impl<'a> OwningPtr<'a> { | |
/// Must point to a valid `T`. | ||
#[inline] | ||
pub unsafe fn read<T>(self) -> T { | ||
self.inner().as_ptr().cast::<T>().read() | ||
self.as_ptr().cast::<T>().read() | ||
} | ||
|
||
//// Consumes the [`OwningPtr`] to drop the underlying data of type `T`. | ||
/// | ||
/// # Safety | ||
/// Must point to a valid `T`. | ||
#[inline] | ||
pub unsafe fn drop_as<T>(self) { | ||
self.as_ptr().cast::<T>().drop_in_place() | ||
} | ||
|
||
/// Gets the underlying pointer, erasing the associated lifetime. | ||
/// | ||
/// 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 { | ||
self.0.as_ptr() | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 doOwningPtr::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 satisfyfor<'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 ._.