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 ToPrimitive for enums #314

Merged
merged 1 commit into from
Jul 11, 2017
Merged

Conversation

PlasmaPower
Copy link
Contributor

I had to double the compile fail tests, as they will bail on the first error.

I have some ideas for more complex to/from primitive derives (with inner values that implement to/from primitive), but I'll save those for a future PR.

@PlasmaPower
Copy link
Contributor Author

Failure unrelated to this PR, it's instead due to rust-lang/rust#42431

@cuviper
Copy link
Member

cuviper commented Jul 8, 2017

That failure is also #312.

Simple enums can be cast directly to integers with as. I do see the value in implementing this to support generic programming with ToPrimitive, but it should probably just use as internally.

@PlasmaPower
Copy link
Contributor Author

@cuviper Oh I had no idea! I'll change the implementation.

@PlasmaPower
Copy link
Contributor Author

as requires a value, not just a reference. We have no guarantee that the enum implements clone. It seems the current implementation is the only way.

@cuviper
Copy link
Member

cuviper commented Jul 8, 2017 via email

@PlasmaPower
Copy link
Contributor Author

PlasmaPower commented Jul 8, 2017

Should be, but we can't guarantee they implement Copy or implement it for them. It's illegal even if they do implement Copy, due to macro hygiene.

Yeah, it's awkward.

@cuviper
Copy link
Member

cuviper commented Jul 11, 2017

It's illegal even if they do implement Copy, due to macro hygiene.

I don't think it's quite that hygienic -- at least from a quick test, deriving with as on a Copy enum seems to work for me. But still, we can't assume Copy. Maybe someday we can specialize it.

bors r+

bors bot added a commit that referenced this pull request Jul 11, 2017
314: Derive ToPrimitive for enums r=cuviper

I had to double the compile fail tests, as they will bail on the first error.

I have some ideas for more complex to/from primitive derives (with inner values that implement to/from primitive), but I'll save those for a future PR.
@PlasmaPower
Copy link
Contributor Author

@cuviper I didn't check that, but trying to use the as operator errors either way because the compiler doesn't know if the enum has associated values. Even though we check that, the compiler doesn't know we check that. It gives an error about the enum not being a primitive type, and suggests using .into().

@bors
Copy link
Contributor

bors bot commented Jul 11, 2017

Build succeeded

@bors bors bot merged commit 5f3a3b0 into rust-num:master Jul 11, 2017
@cuviper
Copy link
Member

cuviper commented Jul 11, 2017

That's ok. What you wrote works, most importantly. Thanks!

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

Successfully merging this pull request may close these issues.

2 participants