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.)
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
}
}
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:
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.
Proposal
Problem statement
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 haveas_ref
/as_mut
, but they behave different from&*ptr
/&mut *ptr
: they return anOption
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:
Solution sketch
I propose to add methods
as_ref_unchecked
/as_mut_unchecked
for direct raw-pointer-to-reference conversions.The examples above then become
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:
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):
Second, if there's a concrete solution: