Skip to content

Instantly share code, notes, and snippets.

@nikomatsakis
Last active May 19, 2020 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikomatsakis/2fd9d3f1eeede256aa7c17b3d4af1376 to your computer and use it in GitHub Desktop.
Save nikomatsakis/2fd9d3f1eeede256aa7c17b3d4af1376 to your computer and use it in GitHub Desktop.

Proposal

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.

Motivation for exposing these capabilities

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.

Motivation for using macro-based syntax

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.

Future possibilities

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment