Skip to content

stabilize ptr_cast_slice - #162927

Open
malezjaa wants to merge 2 commits into
rust-lang:mainfrom
malezjaa:stabilize-ptr-cast-slice
Open

malezjaa wants to merge 2 commits into
rust-lang:mainfrom
malezjaa:stabilize-ptr-cast-slice

Conversation

@malezjaa

@malezjaa malezjaa commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

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 T and NonNull<T> to make using raw pointers more ergonomic in Rust by supporting postfix method chain syntax while creating slices.

Implementation

  1. Implementation PR: Implement cast_slice for raw pointer types #149110
  2. Migrated std to use ptr.cast_slice: Migrate libraries from ptr::slice_from_raw_parts to .cast_slice #156109

Public API

// core::ptr::const_ptr
impl<T> *const T {
    pub const fn cast_slice(self, len: usize) -> *const [T];
}

// core::ptr::mut_ptr
impl<T> *mut T {
    pub const fn cast_slice(self, len: usize) -> *mut [T];
}

// core::ptr::non_null
impl<T> NonNull<T> {
    pub const fn cast_slice(self, len: usize) -> NonNull<[T]>;
}

r? @nia-e

@rustbot modify labels: +T-libs

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 17, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Sep 17, 2026
@rustbot

rustbot commented Sep 17, 2026

Copy link
Copy Markdown
Collaborator

nia-e is currently at their maximum review capacity.
They may take a while to respond.

@nia-e

nia-e commented Sep 17, 2026

Copy link
Copy Markdown
Member

seems fine. i think it's small enough that we can const-stabilise it too yeah.

@rfcbot merge libs

@rust-rfcbot

rust-rfcbot commented Sep 17, 2026 •

Copy link
Copy Markdown
Collaborator

@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.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 17, 2026
@Paladynee

Paladynee commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

i'd like to change the documentation to point to ptr::slice_from_raw_parts instead of inlining it before we stabilize, like ptr.copy_from_nonoverlapping pointing to ptr::copy_nonoverlapping

@rust-bors

This comment has been minimized.

@rustbot

rustbot commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

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.

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 20, 2026
@malezjaa

malezjaa commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Sorry, forgot I cant use github for resolving merge conflicts :\

@malezjaa
malezjaa force-pushed the stabilize-ptr-cast-slice branch from 6fc51db to d00335c Compare September 20, 2026 16:30
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 20, 2026
///
/// 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.

@Mark-Simulacrum Mark-Simulacrum Sep 20, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread library/alloc/src/lib.rs

@Mark-Simulacrum Mark-Simulacrum Sep 20, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View changes since the review

@Paladynee Paladynee Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_slice_with_len or cast_slice_with_len are the only ones I can think of that might be reasonable, besides the proposal.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick survey of our pointer API (mostly ignoring mut versions):

Stable as_*

  • as_array *const [T], N -> Option<*const [T; N]> 🟰

Stable cast_*

  • cast for *const T -> *const U
  • cast_mut for *const T -> *mut T
  • cast_const for *mut T -> *const T

Unstable as_*

  • as_slice for *const [T; N] -> *mut [T] 🟰
  • as_ptr for *const [T] -> *const T 🟰
  • as_ptr for *const [T; N] -> *const T 🟰
  • as_uninit_ref for *const T -> Option<&MaybeUninit<T>
  • as_uninit_slice for *const [T] -> Option<&[MaybeUninit<T>]>

Unstable cast_*

  • cast_init for *const MaybeUninit<T> -> *const T ❓ MaybeUninit::as_ptr
  • cast_uninit for *const T -> *const MaybeUninit<T>
  • cast_array for *const T, N -> *const [T; N]
  • cast_slice (this API) for *const T -> *const [T]

Unstable to_*

  • to_raw_parts for *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

@rustbot rustbot added the I-libs-nominated Nominated for discussion during a libs team meeting. label Sep 22, 2026
@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Sep 22, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@clarfonthey clarfonthey removed the I-libs-nominated Nominated for discussion during a libs team meeting. label Sep 22, 2026
@tgross35

Copy link
Copy Markdown
Member

From the meeting:

Nia: This is sound for any usize. I'm sympathetic to the naming argument that it looks weird, but cast_array is a precedent.

cast_array is unstable, I don't think that should be taken as precedent. We could just as easily change cast_array to as_array, which I do think we should do (above).

There is a stable as_array method

Fon: We have a week to figure out. I'll unnominate.

What week, it's in FCP 😱

@cuviper

cuviper commented Sep 23, 2026

Copy link
Copy Markdown
Member

@rfcbot concern naming

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Sep 23, 2026
@clarfonthey

clarfonthey commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Fon: We have a week to figure out. I'll unnominate.

What week, it's in FCP 😱

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.)

@clarfonthey clarfonthey added the I-libs-nominated Nominated for discussion during a libs team meeting. label Sep 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. I-libs-nominated Nominated for discussion during a libs team meeting. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracking Issue for #![feature(ptr_cast_slice)]

9 participants