Skip to content

Instantly share code, notes, and snippets.

@thomcc
Created January 22, 2022 00:24
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 thomcc/ddd4b76c050ad7f9dfbcc68752d2f105 to your computer and use it in GitHub Desktop.
Save thomcc/ddd4b76c050ad7f9dfbcc68752d2f105 to your computer and use it in GitHub Desktop.

Summary

This RFC extends the meaning of the existing #[repr] attribute with the non_exhaustive option. This option may only be used on fieldless (often called "C-like") enums which use a explicit representation (that is, either a primitive representation or the C representation), and allows the enum to hold values not included in the type definition without invoking undefined behavior.

As a result, enums with #[repr(_, non_exhaustive)] cannot be matched on exhaustively without the use of a wildcard arm (similar to #[non_exhaustive] enums defined in external crates), and prevents the compiler from performing certain layout optimizations (such as the niche optimization).

Note that this RFC does not provide to construct a #[repr(_, non_exhaustive)] enum holding arbitrary values, but just allows unsafe code to do so without invoking language-level undefined behavior.

Motivation

There are a few cases where the validity requirements for an enum are inconvenient one way or another. I'll go ov

  1. Use a #[repr(transparent)] newtype struct that wraps the equivalent integer type, and add a number of associated constants.

    This is tedious (lots of boilerplate) and not entirely obvious.

  2. Use a type alias (e.g. pub type SomeEnum = u8;), and then directly, usually mapping it to a higher lever .

  3. One

Unfortunately, it can be surprising non-obvious that

Anecdotally, I've seen several users surprised that #[repr(C)] enum isn't a good idea for interfacing with C

in fact, the idea for this RFC was inspired by one of them asking if #[non_exhaustive] solved the problem)

FFI

For FFI code, it would often be desirable to represent a function return type or a structure field as an enum, particularly if that's how it's represented in another language (typically C, or something calling through C).

Unfortunately, this is almost always a mistake, since C (and several other languages) consider adding a variant to an enum to be an ABI-compatible update, but this is not the case for Rust enums, and it can be challenging to ensure that the version of the library you think you're using (and which contains the header definition) exactly matches the version of the library you're using.

If #[repr(C, non_exhaustive)] enums (for example) were allowed, this would be a very clean and natural way to represent enums from C. In particular, it would

As it stands now, the foll

Serialization/Deserialization

This actually comes up in a few cases for serialization, but consider performing zero-copy deserialization from binary data.

For example, for zero-copy it's often desie

There are a number of cases where low level code would like to represent a type as an enum, but can't because

Guide-level explanation

Explain the proposal as if it was already included in the language and you were teaching it to another Rust programmer. That generally means:

  • Introducing new named concepts.
  • Explaining the feature largely in terms of examples.
  • Explaining how Rust programmers should think about the feature, and how it should impact the way they use Rust. It should explain the impact a s concretely as possible.
  • If applicable, provide sample error messages, deprecation warnings, or migration guidance.
  • If applicable, describe the differences between teaching this to existing Rust programmers and new Rust programmers.

For implementation-oriented RFCs (e.g. for compiler internals), this section should focus on how compiler contributors should think about the chan ge, and give examples of its concrete impact. For policy RFCs, this section should provide an example-driven introduction to the policy, and expla in its impact in concrete terms.

Reference-level explanation

This is the technical portion of the RFC. Explain the design in sufficient detail that:

  • Its interaction with other features is clear.
  • It is reasonably clear how the feature would be implemented.
  • Corner cases are dissected by example.

The section should return to the examples given in the previous section, and explain more fully how the detailed proposal makes those examples wor k.

Drawbacks

Why should we not do this?

Rationale and alternatives

  • Why is this design the best in the space of possible designs?
  • What other designs have been considered and what is the rationale for not choosing them?
  • What is the impact of not doing this?

Prior art

Discuss prior art, both the good and the bad, in relation to this proposal. A few examples of what this can include are:

  • For language, library, cargo, tools, and compiler proposals: Does this feature exist in other programming languages and what experience have the ir community had?
  • For community proposals: Is this done by some other community and what were their experiences with it?
  • For other teams: What lessons can we learn from what other communities have done here?
  • Papers: Are there any published papers or great posts that discuss this? If you have some relevant papers to refer to, this can serve as a more detailed theoretical background.

This section is intended to encourage you as an author to think about the lessons from other languages, provide readers of your RFC with a fuller picture. If there is no prior art, that is fine - your ideas are interesting to us whether they are brand new or if it is an adaptation from other language s.

Note that while precedent set by other languages is some motivation, it does not on its own motivate an RFC. Please also take into consideration that rust sometimes intentionally diverges from common language features.

Unresolved questions

  • What parts of the design do you expect to resolve through the RFC process before this gets merged?
  • What parts of the design do you expect to resolve through the implementation of this feature before stabilization?
  • What related issues do you consider out of scope for this RFC that could be addressed in the future independently of the solution that comes out of this RFC?

Future possibilities

Think about what the natural extension and evolution of your proposal would be and how it would affect the language and project as a whole in a holistic way. Try to use this section as a tool to more fully consider all possible interactions with the project and language in your proposal. Also consider how this all fits into the roadmap for the project and of the relevant sub-team.

This is also a good place to "dump ideas", if they are out of scope for the RFC you are writing but otherwise related.

If you have tried and cannot think of any future possibilities, you may simply state that you cannot think of anything.

Note that having something written down in the future-possibilities section is not a reason to accept the current or a future RFC; such notes should be in the section on motivation or rationale in this or subsequent RFCs. The section merely provides additional information.

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