stabilize ptr_cast_slice - #162927
stabilize ptr_cast_slice#162927malezjaa wants to merge 2 commits into
Conversation
|
|
|
seems fine. i think it's small enough that we can const-stabilise it too yeah. @rfcbot merge libs |
|
@nia-e has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns: Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/libs-ping: FCP proposed for libs, please feel free to register concerns. |
|
i'd like to change the documentation to point to |
This comment has been minimized.
This comment has been minimized.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
|
Sorry, forgot I cant use github for resolving merge conflicts :\ |
6fc51db to
d00335c
Compare
| /// | ||
| /// This function is safe, but actually using the return value is unsafe. | ||
| /// See the documentation of [`slice::from_raw_parts`] for slice safety requirements. | ||
| /// See [`ptr::slice_from_raw_parts`] for more information. |
There was a problem hiding this comment.
I think it's critical we keep the "safety" aspect present here. I'm OK indirecting but this makes it too easy to skip over information that is pretty important for the reader.
There was a problem hiding this comment.
So something like this,
/// This function is safe, but actually using the return value is unsafe.
/// See [`ptr::slice_from_raw_parts`] for more information.
///
/// [`ptr::slice_from_raw_parts`]: crate::ptr::slice_from_raw_parts
or should I just revert to the original version?
There was a problem hiding this comment.
Commenting on a random file to start a thread.
I'm not a huge fan of the name cast_slice. I think all of our existing stable cast functions (and unstable, with the exception of cast_array, which I would rename too) don't take any parameters -- just like as casts, they may be lossy, but they're not adding any information into the result.
I'm checking my box since I don't have a great alternative (e.g., to_slice, maybe?) but wanted to raise that point in case others share it and have ideas on alternative names.
There was a problem hiding this comment.
this feature was initially proposed in the ACP as with_len, but explicitly accepted as cast_slice as consistency with cast_array.
here's the relevant libs-api meeting: https://hackmd.io/@rust-libs/SkVJ_fcgbe#new-change-proposal-rusttflibs693-Add-a-method-for-raw-pointers-that-calls-ptrslice_from_raw_parts_mut
here's the libs-api meeting for cast_array, which doesn't touch upon the naming too much: https://hackmd.io/@rust-libs/Bk0IDObHxx#new-change-proposal-rusttflibs602-ACP-add-arrayfrom_raw_parts
There was a problem hiding this comment.
IMO, that consistency argument doesn’t hold much value until we actually stabilize cast_array (or this one). I think having slice/array in the name is important, it’s only the cast part that I have some qualms about.
There was a problem hiding this comment.
to_slice_with_len or cast_slice_with_len are the only ones I can think of that might be reasonable, besides the proposal.
There was a problem hiding this comment.
Quick survey of our pointer API (mostly ignoring mut versions):
Stable as_*
as_array*const [T], N -> Option<*const [T; N]>🟰
Stable cast_*
castfor*const T -> *const Ucast_mutfor*const T -> *mut Tcast_constfor*mut T -> *const T
Unstable as_*
as_slicefor*const [T; N] -> *mut [T]🟰as_ptrfor*const [T] -> *const T🟰as_ptrfor*const [T; N] -> *const T🟰as_uninit_reffor*const T -> Option<&MaybeUninit<T>as_uninit_slicefor*const [T] -> Option<&[MaybeUninit<T>]>
Unstable cast_*
cast_initfor*const MaybeUninit<T> -> *const T❓MaybeUninit::as_ptrcast_uninitfor*const T -> *const MaybeUninit<T>cast_arrayfor*const T, N -> *const [T; N]cast_slice(this API) for*const T -> *const [T]
Unstable to_*
to_raw_partsfor*const T -> (*const (), <T as Pointee>::Metadata)
The difference between cast_ and as_ feels a bit inconsistent. I've added an 🟰 to the places where the API is equivalent to a non-raw-pointer T/[T]/[T; N] method's name, all of those seem fine.
The remaining unstable cast_ methods are less certain. All of those feel more similar to the as_ methods in the sense that they add/remove length information or add/remove a transparent wrapper. The stable cast_ OTOH are more fundamental changes to reinterpret as an unrelated type or change allowed accesses - not the most descriptive rule, but I think it works well enough here?
All of that to say, I think cast_array and cast_slice should be as_array and as_bikeshed rather than cast_bikeshed.
Fon's proposal to use with_len seems nice, so perhaps as_slice_with_len? Or go a completely different route and call it fn slice_from_raw_parts to echo the &[T] API.
Since this naming could use discussion, nominating for the naming things meeting:
@rustbot label +I-libs-nominated
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
|
From the meeting:
There is a stable
What week, it's in FCP 😱 |
|
@rfcbot concern naming |
That was exactly my point. Any concerns would have to be formally registered before the meeting to avoid it just passing FCP. (It was a long meeting. Just woke up from a post-libs-meeting-recovery nap, and now will re-nominate for next week.) |
Stabilization report
Closes #149103
This feature adds method calls on raw pointers that call into the respective
ptr::slice_from_raw_parts{_mut}method for the types*const T,*mut TandNonNull<T>to make using raw pointers more ergonomic in Rust by supporting postfix method chain syntax while creating slices.Implementation
cast_slicefor raw pointer types #149110stdto useptr.cast_slice: Migrate libraries from ptr::slice_from_raw_parts to .cast_slice #156109Public API
r? @nia-e
@rustbot modify labels: +T-libs