Repository: rust
Issue: Tracking Issue for raw-pointer-to-reference conversion methods
Labels: C-tracking-issue,T-libs

<!--
Thank you for creating a tracking issue!

Tracking issues are for tracking a feature from implementation to stabilization.

Make sure to include the relevant RFC for the feature if it has one.

If the new feature is small, it may be fine to skip the RFC process. In that
case, you can use `issue = "none"` in your initial implementation PR. The
reviewer will ask you to open a tracking issue if they agree your feature can be
added without an RFC.
-->

Feature gate: `#![feature(as_ref_unchecked)]` (or similar)

This is a tracking issue for raw-pointer-to-reference conversion methods (https://github.com/rust-lang/libs-team/issues/342).

With https://github.com/rust-lang/rust/issues/106116, we have methods for *almost* all casts/conversions one wants to do on references and pointers, to avoid `as` casts and prefix operators. Just one direction is missing: turning raw pointers into references. Here we have `as_ref`/`as_mut`, but they behave different from `&*ptr`/`&mut *ptr`: they return an `Option` and perform a null-check. (These are the only methods on raw pointers that perform a null check.)

### Public API

```rust
impl<T> *const T {
  unsafe const fn as_ref_unchecked<'a>(self) -> &'a T {
    &*self
  }
}
impl<T> *mut T {
  unsafe const fn as_ref_unchecked<'a>(self) -> &'a T {
    &*self
  }
  unsafe const fn as_mut_unchecked<'a>(self) -> &'a mut T {
    &mut *self
  }
}
```


### Motivating examples or use cases

Some random examples from a quick grep of the rustc sources:
```rust
&mut *(out as *mut &mut dyn PrintBackendInfo)
&mut *(state as *mut &mut dyn FnMut(&[u8]) -> io::Result<()>)
&mut *(self.0 as *mut _)
&mut *(vec as *mut Vec<Library>)
&mut *(value as *mut T as *mut UnsafeCell<T>)
&mut *(s as *mut T).cast::<[T; 1]>()
```

The examples above then become
```rust
out.cast::<&mut dyn PrintBackendInfo>().as_mut_unchecked()
state.cast::<&mut dyn FnMut(&[u8]) -> io::Result<()>>().as_mut_unchecked()
self.0.cast().as_mut_unchecked()
vec.cast::<Vec<Library>>().as_mut_unchecked()
ptr::from_mut(value).cast::<UnsafeCell<T>>().as_mut_unchecked()
ptr::from_mut(s).cast::<[T; 1]>().as_mut_unchecked()
```
Unfortunately, since the `as_mut` name is already taken, these are all longer than the prefix variants. But they can be read left-to-right which is an advantage.
### Steps / History

<!--
For larger features, more steps might be involved.
If the feature is changed later, please add those PRs here as well.
-->

- [x] Implementation: https://github.com/rust-lang/rust/pull/122492
- [ ] Final comment period (FCP)[^1]
- [ ] Stabilization PR

<!--
Once the feature has gone through a few release cycles and there are no
unresolved questions left, the feature might be ready for stabilization.

If this feature didn't go through the RFC process, a final comment period
(FCP) is always needed before stabilization. This works as follows:

A library API team member can kick off the stabilization process, at which point
the rfcbot will ask all the team members to verify they agree with
stabilization. Once enough members agree and there are no concerns, the final
comment period begins: this issue will be marked as such and will be listed
in the next This Week in Rust newsletter. If no blocking concerns are raised in
that period of 10 days, a stabilization PR can be opened by anyone.
-->

### Unresolved Questions

<!--
Include any open questions that need to be answered before the feature can be
stabilised. If multiple (unrelated) big questions come up, it can be a good idea
to open a separate issue for each, to make it easier to keep track of the
discussions.

It's useful to link any relevant discussions and conclusions (whether on GitHub,
Zulip, or the internals forum) here.
-->

- Is there a shorter name we could use?

[^1]: https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html