Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive enum with variants without fields #33

Closed
azriel91 opened this issue Sep 19, 2018 · 1 comment
Closed

Derive enum with variants without fields #33

azriel91 opened this issue Sep 19, 2018 · 1 comment

Comments

@azriel91
Copy link
Contributor

@azriel91 azriel91 commented Sep 19, 2018

EnumIter and EnumString both work by using Default::default() values for any fields on an enum variant. I have a use case where I want to match on the enum's discriminant, but the fields are !Default.

To do this, I propose a #[derive(EnumDiscriminants)] as well as the attribute #[strum_discriminants(derive(..))], which will have the following effect:

struct NonDefault;

#[derive(EnumDiscriminants)]
#[strum_discriminants(
    name(HelloVariants),
    derive(EnumString, EnumIter),
    strum(serialize_all = "snake_case")
)]
enum Hello {
    Variant0,
    Variant1(NonDefault),
    Variant2 { a: NonDefault },
}

// produces:
/// Auto-generated discriminant variants for the `Hello` enum.
#[derive(Clone, Copy, Debug, PartialEq, Eq, EnumString, EnumIter)]
#[strum(serialize_all = "snake_case")]
enum HelloVariants {
    Variant0,
    Variant1,
    Variant2,
}

impl From<Hello> for HelloVariants {
    fn from(val: Hello) -> HelloVariants {
        match val {
            Hello::Variant0 => HelloVariants::Variant0,
            Hello::Variant1(..) => HelloVariants::Variant1,
            Hello::Variant2 { .. } => HelloVariants::Variant2,
        }
    }
}

impl<'_enum> From<&'_enum Hello> for HelloVariants {
    fn from(val: &'_enum Hello) -> HelloVariants {
        match *val {
            Hello::Variant0 => HelloVariants::Variant0,
            Hello::Variant1(..) => HelloVariants::Variant1,
            Hello::Variant2 { .. } => HelloVariants::Variant2,
        }
    }
}
azriel91 added a commit to azriel91/strum that referenced this issue Sep 19, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 19, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 19, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 19, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 19, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 20, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 24, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 24, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 24, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 24, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 24, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 24, 2018
azriel91 added a commit to azriel91/strum that referenced this issue Sep 24, 2018
Peternator7 pushed a commit that referenced this issue Sep 26, 2018
* Run rustfmt over repository.

* Bump `syn` to 0.15

* Implemented ability to `serialize_all` using cases from `heck`.

Issue #21

* Use `path` and `version` in dependency specifications.

Issue #21

* Updated documentation.

Issue #21

* Added `CHANGELOG.md`.

* Also convert case when deriving `Display`.

Issue #21

* Added `EnumDiscriminants` derive.

Issue #33

* Added the ability to rename derived `EnumDiscriminants`.

Issue #33

* Updated `README.md` and lib.rs docs.

Issue #33

* Updated `CHANGELOG.md`.

Issue #33

* WIP: refactoring to allow attributes on discriminants enum.

* Use single `strum_discriminants` top level attribute.

Issue #33

* Allow multiple declarations of `strum_discriminants` attribute.

Issue #33

* Pass through all other attributes to discriminant enum.

Issue #33

* Add `impl From<MyEnum> for MyEnumDiscriminants`.

Issue #33

* Add `impl<'_enum> From<&'_enum MyEnum> for MyEnumDiscriminants`.

Issue #33

* Added complex case test for `From` derivation.

Issue #33

* Added docs to some helper functions.

* Added docs about `From` impls.

Issue #33
@azriel91
Copy link
Contributor Author

@azriel91 azriel91 commented Sep 26, 2018

Done in #34 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant