Skip to content
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

Add raw-pointer-to-reference conversion methods #342

Closed
RalfJung opened this issue Feb 24, 2024 · 1 comment
Closed

Add raw-pointer-to-reference conversion methods #342

RalfJung opened this issue Feb 24, 2024 · 1 comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@RalfJung
Copy link
Member

Proposal

Problem statement

With rust-lang/rust#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.)

Motivating examples or use cases

Some random examples from a quick grep of the rustc sources:

&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]>()

Solution sketch

I propose to add methods as_ref_unchecked/as_mut_unchecked for direct raw-pointer-to-reference conversions.

impl<T> *const T {
  unsafe fn as_ref_unchecked<'a>(self) -> &'a T {
    &*self
  }
}
impl<T> *mut T {
  unsafe fn as_ref_unchecked<'a>(self) -> &'a T {
    &*self
  }
  unsafe fn as_mut_unchecked<'a>(self) -> &'a mut T {
    &mut *self
  }
}

The examples above then become

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.

Alternatives

If we do nothing, one has to write this to get the same behavior:

out.cast::<&mut dyn PrintBackendInfo>().as_mut().unwrap_unchecked()
state.cast::<&mut dyn FnMut(&[u8]) -> io::Result<()>>().as_mut().unwrap_unchecked()
self.0.cast().as_mut().unwrap_unchecked()
vec.cast::<Vec<Library>>().as_mut().unwrap_unchecked()
ptr::from_mut(value).cast::<UnsafeCell<T>>().as_mut().unwrap_unchecked()
ptr::from_mut(s).cast::<[T; 1]>().as_mut().unwrap_unchecked()

Links and related work

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.
@RalfJung RalfJung added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Feb 24, 2024
@RalfJung RalfJung changed the title Ass raw-pointer-to-reference conversion methods Add raw-pointer-to-reference conversion methods Feb 24, 2024
@m-ou-se
Copy link
Member

m-ou-se commented Mar 5, 2024

Discussed in the libs-api meeting.

👍

Feel free to open a tracking issue and open a PR to rust-lang/rust to add it as an unstable feature.

@m-ou-se m-ou-se closed this as completed Mar 5, 2024
@m-ou-se m-ou-se added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

2 participants