Introduce two macros
raw_ref!(path)
raw_ref_mut!(path)
which borrow the contents at path
and produce (respectively) *const T
and *mut T
raw pointers.
Unlike the &
syntax, no "safe reference" is created. This means that the resulting raw pointer can be misaligned
or invalid to dereference and that is not UB until the pointer is actually dereferenced. For more details see RFC 2582.
This is an alternative to the previous syntax specified in RFC 2582, which was &raw const
and &raw mut
.
The motivations for making this change is that the syntax &raw const
and &raw mut
is not seen as particularly ideal. Among its downsides:
- It is verbose and wordy.
- It introduces a fresh contextual keyword
raw
. - It breaks the symmetry that we previously have had, where
&foo
creates a&T
type (and&mut foo
creates a&mut T
) type.
Further, we think that there may be an opportunity to revisit raw-pointer ergonomics in a bigger way that could improve the language overall (some background available in these three messages, particularly the final one). We don't feel ready to decide if that opportunity is worth the resulting churn, but we don't feel the need to commit to syntax like &raw const
that may become deprecated.
The raw_ref
and raw_ref_mut
macros would be stable and hence usable in perpetuity. If we do wind up adopting a larger "raw pointer reform" than these macros might be deprecated in favor of the newer types and operators.
On the other hand, if we wind up deciding that larger "raw pointer reform" doesn't make sense, we might add some more "builtin" syntax than the raw_ref
macros. Alternatively, we might simply keep the macros as they are, as they address a somewhat niche concern (creating raw pointers to invalid or unaligned memory, where the rules for a standard reference would be invalid).