Add raw-pointer-to-reference conversion methods #342
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
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 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:
The text was updated successfully, but these errors were encountered: