Skip to content

Conversation

@danielhenrymantilla
Copy link
Collaborator

Co-authored-by: @glchapman

Fixes #11

So the enum

#[derive(TryFromPrimitive)]
#[repr(u8)]
enum Example { A, B, C, D, E, F = 8, G, H, I, J, K, L, M, }

now generates:

impl ::num_enum::TryFromPrimitive for Example {
    // ...
    fn try_from_primitive(number: Self::Primitive)
     ->
         ::core::result::Result<Self,
                                ::num_enum::TryFromPrimitiveError<Self>> {
        #![allow(non_upper_case_globals)]
        const A: u8 = 0;
        const B: u8 = u8::wrapping_add(A, 1);
        const C: u8 = u8::wrapping_add(B, 1);
        const D: u8 = u8::wrapping_add(C, 1);
        const E: u8 = u8::wrapping_add(D, 1);
        const F: u8 = 8;
        const G: u8 = u8::wrapping_add(F, 1);
        const H: u8 = u8::wrapping_add(G, 1);
        const I: u8 = u8::wrapping_add(H, 1);
        const J: u8 = u8::wrapping_add(I, 1);
        const K: u8 = u8::wrapping_add(J, 1);
        const L: u8 = u8::wrapping_add(K, 1);
        const M: u8 = u8::wrapping_add(L, 1);

@danielhenrymantilla danielhenrymantilla self-assigned this Sep 27, 2019
@danielhenrymantilla danielhenrymantilla added the enhancement New feature or request label Sep 27, 2019
Copy link
Owner

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me :) Thanks for the improvement!

@illicitonion illicitonion merged commit e67d411 into master Sep 28, 2019
@danielhenrymantilla danielhenrymantilla deleted the feature/improve_constant_values branch September 28, 2019 09:26
illicitonion added a commit that referenced this pull request Oct 4, 2019
Notable changes:
* Improved constant values generated code (issue #11) (#12)
* Upgrade dependencies to stable versions (#15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggestion for nicer expand output

3 participants