Closed
Description
Feature gate: #![feature(enum_as_repr)]
This is a tracking issue for traits to convert primitive enums into their repr (core::enums::AsRepr
), and attempt to convert reprs into primitive enums (core::enums::FromRepr
). AsRepr
is automatically implemented for all relevant enum types, as well as std::mem::Discriminant
, and FromRepr
may be derived.
Public API
// core::enums
// Automatically implemented for all data-free enums with an explicit repr attribute.
pub unsafe trait AsRepr: Sized {
type Repr;
fn as_repr(&self) -> Self::Repr;
}
pub unsafe trait FromRepr: AsRepr {
fn try_from_repr(from: Self::Repr) -> Result<Self, TryFromReprError<Self::Repr>>;
unsafe fn from_repr(from: Self::Repr) -> Self;
}
Steps / History
- Discussed as part of RFCs 3040 and 3046
- Implementation: Automatically implement AsRepr and allow deriving FromRepr for fieldless enums #81642
- Final comment period (FCP)
- Stabilization PR
Unresolved Questions
- Do we want a blanket impl of
FromRepr
forDiscriminant<T>
? We want to consider carefully if that impl would have unwanted semver implications.