You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Converting an enum to and from an integer is repetitive and brittle. So instead we should add the traits ToInt/FromInt/ToUint/FromUint, and add a syntax extension to automatically generate implementations of those traits for a given enum.
The 'to' side is currently automatic, foo as uint. The 'from' side is trickier since it has to only consider the valid constructors. In any case, while this is a real problem I do not feel it's best approached via syntax extension. More like an additional dimension of the deriving thing.
That only captures the ToInt side of things. Trying to convert an integer into an enum can be prone to error because you need to make sure the definition of an enum is sharing the same integer bindings as the conversion code. It's easy to add a new variant and forget to update the converters.
Ack, I completely misread #2132. Yes you are right, they are the same. This deriving method @graydon mentioned is the modern way of generating this kind of code.
I suppose I did not know when I wrote that comment that derivingis implemented as a syntax extension. In that case, yeah, let's merge #2132 with this and say this is just a new deriving mode. Nominating for feature complete.
Activity
nikomatsakis commentedon Oct 26, 2012
+1
graydon commentedon Oct 27, 2012
The 'to' side is currently automatic,
foo as uint
. The 'from' side is trickier since it has to only consider the valid constructors. In any case, while this is a real problem I do not feel it's best approached via syntax extension. More like an additional dimension of thederiving
thing.sanxiyn commentedon Feb 12, 2013
This is #2132?
erickt commentedon Feb 12, 2013
That only captures the
ToInt
side of things. Trying to convert an integer into an enum can be prone to error because you need to make sure the definition of an enum is sharing the same integer bindings as the conversion code. It's easy to add a new variant and forget to update the converters.erickt commentedon Feb 12, 2013
Ack, I completely misread #2132. Yes you are right, they are the same. This
deriving
method @graydon mentioned is the modern way of generating this kind of code.graydon commentedon May 1, 2013
I suppose I did not know when I wrote that comment that
deriving
is implemented as a syntax extension. In that case, yeah, let's merge #2132 with this and say this is just a new deriving mode. Nominating for feature complete.graydon commentedon May 9, 2013
accepted for feature-complete milestone
emberian commentedon Jul 7, 2013
Related to #7080
catamorphism commentedon Aug 26, 2013
Bug triage. Milestone looks good to me.
Rollup merge of rust-lang#81501 - calebcartwright:update-rustfmt, r=s…