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

Add #[derive] on C-like enums for From impls from the various numeric types #24823

Closed
lilyball opened this issue Apr 25, 2015 · 3 comments
Closed
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@lilyball
Copy link
Contributor

C-like enums used to be able to say #[derive(FromPrimitive)] to allow for converting from integral types into enum variants. This no longer works as std::num::FromPrimitive has been removed (see #24822).

It seems reasonable to say that the "correct" replacement is From impls for various integral types. It would be great if we could derive those impls. #[derive(From)] is perhaps a bit non-obvious as to what it's supposed to be deriving. I'd like to be able to say #[derive(From<i32>)], but that's not valid syntax.

Given that, either the attribute syntax should be extended to allow for <> in identifiers (it should perhaps allow for any well-formed types), or a more conservative approach would be to make #[derive(From(i32))] work instead (which is valid syntax but aborts in the derive machinery, saying the attribute is malformed). Since the current derive approach converts #[derive(Foo,Bar)] into #[derive_Foo] #[derive_Bar] this could be accomplished by converting #[derive(From(i32))] into #[derive_From(i32)].

Ideally there'd be some way to say "derive From<T> for all integral types", since FromPrimitive allowed for converting from all integral types, but I'm not certain how that should work. With the more conservative approach we could just pick an identifier and say something like #[derive(From(raw))], but that's not very discoverable.

@lilyball
Copy link
Contributor Author

We may also want to consider being able to derive Into for the numeric types as well, although that's not as interesting because the enum variants can be converted into integral types with as (but it's still potentially useful).

Note that we can't derive From<T> for i32, which would be preferred, as that requires T to be public and I'm not aware of any precedent for #[derive] only working on public types. But that restriction doesn't apply to Into, so we can derive e.g. Into<i32> just fine.

@lilyball lilyball changed the title Add #[derive] for From impls from the various numeric types Add #[derive] on C-like enums for From impls from the various numeric types Apr 25, 2015
@steveklabnik
Copy link
Member

Triage: almost two years later, custom derives are almost here on stable. That said, this sounds like a question for @rust-lang/libs ; I'd assume this would use TryFrom today, given that it's a fallible operation.

@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-libs labels Mar 24, 2017
@Mark-Simulacrum
Copy link
Member

Closing -- I believe this should be implemented out-of-tree with Macros 1.1 if we want it. If you disagree, then this needs to go through the RFC process, so please follow that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants