rust icon indicating copy to clipboard operation
rust copied to clipboard

Tracking Issue for ptr_const_cast

Open Kixunil opened this issue 3 years ago • 8 comments

Feature gate: #![feature(ptr_const_cast)]

This is a tracking issue for methods on pointers for casting constness without changing the type

Public API

impl<T: ?Sized> *const T {
    pub fn as_mut(self) -> *mut T;
}

impl<T: ?Sized> *mut T {
    pub fn as_const(self) -> *const T;
}

Steps / History

  • [x] Implementation: #92657
  • [ ] Final comment period (FCP)
  • [ ] Stabilization PR

Unresolved Questions

  • ~~*mut T also already has as_mut() which does something different and has a different signature, adding a mehod of the same name to *const T could be confusing, so maybe a better name should be used. cast_const/cast_mut was suggested.~~ Resolved to use cast_*

Kixunil avatar Jan 08 '22 17:01 Kixunil

I like this function (as it allows a codebase to avoid uses of as if they desire). I agree thta as_mut() could lead to some ambiguity, and cast_mut seems better to me. It's the most similar to cast, so having a similar name seems correct.

As noted in the documentation, a cast_const method isn't strictly necessary, but could be nice for symmetry's sake.

josephlr avatar Apr 18 '22 16:04 josephlr

I'm starting to like cast_ naming as it seems to be a bit more readable too. Will try to find the time for a PR. I don't mind anyone else doing it either. It's a trivial change anyway.

Kixunil avatar Apr 20 '22 16:04 Kixunil

Once they're called cast_, I wonder if we should also add a type parameter like the existing cast method, so you don't have to double up .cast().cast_const() etc.

(However, they currently document that as an explicit feature, not changing the type.)

cuviper avatar Jun 16 '22 18:06 cuviper

@cuviper yes, not changing the type is the main point because it protects against refactors. You can already do both with as.

More detailed explanation

Kixunil avatar Jun 17 '22 08:06 Kixunil

Shall we rename these to cast_*, and also stabilize them?

@rfcbot merge

joshtriplett avatar Jul 20 '22 19:07 joshtriplett

Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members:

  • [x] @Amanieu
  • [ ] @BurntSushi
  • [x] @dtolnay
  • [x] @joshtriplett
  • [x] @m-ou-se
  • [x] @yaahc

No concerns currently listed.

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!

See this document for info about what commands tagged team members can give me.

rfcbot avatar Jul 20 '22 19:07 rfcbot

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

rfcbot avatar Jul 26 '22 18:07 rfcbot

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

rfcbot avatar Aug 05 '22 18:08 rfcbot