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 TryFrom to convert repr to enum #300

Merged
merged 10 commits into from
Sep 18, 2023
Merged

Conversation

ModProg
Copy link
Contributor

@ModProg ModProg commented Aug 31, 2023

Resolves #146

Synopsis

Conversion from the repr integer to an enum.

Solution

A TryFrom derive macro implementing TryFrom<integertype> for Enum.

Checklist

  • Documentation is updated (if required)
  • Tests are added/updated (if required)
    • basic tests
    • compile fail
  • CHANGELOG entry is added (if required)

@ModProg ModProg force-pushed the try_from_repr branch 2 times, most recently from 5610f38 to fa44984 Compare September 1, 2023 11:06
impl/doc/try_from.md Outdated Show resolved Hide resolved
impl/src/try_from.rs Outdated Show resolved Hide resolved
impl/src/try_from.rs Outdated Show resolved Hide resolved
src/convert.rs Outdated Show resolved Hide resolved
@ModProg
Copy link
Contributor Author

ModProg commented Sep 1, 2023

We could also support some #[try_from(<type>)] for types whose definition is a superset to the repr type.

Copy link
Collaborator

@tyranron tyranron left a comment

Choose a reason for hiding this comment

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

@ModProg good job! Thanks 👍
Overall design is solid and thoughtful. The implementation is also good, despite few bikesheddings described below.

We could also support some #[try_from(<type>)] for types whose definition is a superset to the repr type.

Definitely not as a part of this PR. Let's sketch and discuss the design of it in #146.

tests/compile_fail/try_from/invalid_repr.rs Show resolved Hide resolved
src/convert.rs Outdated Show resolved Hide resolved
impl/doc/try_from.md Show resolved Hide resolved
impl/src/try_from.rs Outdated Show resolved Hide resolved
impl/src/try_from.rs Outdated Show resolved Hide resolved
impl/src/try_from.rs Outdated Show resolved Hide resolved
impl/src/try_from.rs Outdated Show resolved Hide resolved
@tyranron
Copy link
Collaborator

tyranron commented Sep 6, 2023

@ModProg @JelteF yet another thing that I think of: we will definitely want to support this case in the future:

#[derive(TryFrom)]
enum Foo {
    #[try_from(u128)] // generates `impl TryFrom<u128> for Foo`
    Bar(u8),
}

While at the same time, in other situations we would like to have something like this:

#[derive(TryFrom)]
#[try_from(u128)] // generates `impl TryFrom<u128> for Foo`
#[repr(u8)]
enum Foo {
    Bar,
}

So, we need a way to differentiate a repr conversions from the ones of the inner types clearly.

And this question touches not only TryFrom, because we also will want to have something like this for Into:

#[derive(Into)] // generates `impl From<Foo> for u8`
#[repr(u8)]
enum Foo {
    Bar = 35,
}

And TryInto:

#[derive(TryInto)]
#[try_into(u8)] // generates `impl TryFrom<Foo> for u8`
#[repr(u128)] 
enum Foo {
    Bar = 35,
}

To disambiguate them clearly, I propose to use repr sub-attribute:

#[derive(Into, TryFrom)]
#[into(repr)] // generates `impl From<Foo> for u8`
#[try_from(repr)] // generates `impl TryFrom<u8> for Foo`
#[repr(u8)]
enum Foo1 {
    Bar,
}

#[derive(TryFrom, TryInto)]
#[try_from(repr(u128))] // generates `impl TryFrom<u128> for Foo`
#[try_into(repr(u8))] // generates `impl TryFrom<Foo> for u8`
#[repr(u16)]
enum Foo2 {
    Bar,
}

Does it make sense?

@ModProg
Copy link
Contributor Author

ModProg commented Sep 6, 2023

@tyranron, looks sensible. I'll implement support for the repr syntax for now, and will error on it not specified.

@ModProg
Copy link
Contributor Author

ModProg commented Sep 6, 2023

@tyranron how would you expect repr(<types>) to work?

impl TryFrom<$type> for $enum {
    fn try_from(value: $type) -> Result<Self> {
        let value: $actual_repr = value.try_into().map_err(|e| TryFromError::message(e.to_string())?;
        match value {
            $actual_implementation
        }
    }
}

@tyranron
Copy link
Collaborator

tyranron commented Sep 7, 2023

@ModProg as a regular forwarding implementation, like you've pointed, yes. But as I've mentioned before, let's not making it as a part of this PR. Better implement as a separate one after merging this.

impl/doc/try_from.md Outdated Show resolved Hide resolved
src/convert.rs Outdated Show resolved Hide resolved
src/convert.rs Outdated Show resolved Hide resolved
JelteF
JelteF previously approved these changes Sep 15, 2023
Copy link
Owner

@JelteF JelteF left a comment

Choose a reason for hiding this comment

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

Looks good to me (apart from the few small comments I left). @tyranron did you have any more feedback.

impl/doc/try_from.md Outdated Show resolved Hide resolved
@tyranron
Copy link
Collaborator

@JelteF yes, I was waiting for #298 to land. Give me a moment.

@tyranron
Copy link
Collaborator

@JelteF oh, and please, change the required CI jobs in project settings to reflect MSRV 1.65.0 -> MSRV 1.72.0 migration. Otherwise, CI won't pass. Thanks!

@tyranron tyranron merged commit 557e801 into JelteF:master Sep 18, 2023
16 checks passed
@tyranron tyranron added this to the 1.0.0 milestone Sep 18, 2023
@DaniPopes DaniPopes mentioned this pull request Nov 11, 2023
3 tasks
DaniPopes added a commit to DaniPopes/derive_more that referenced this pull request Nov 11, 2023
The MSRV bump in JelteF#300 is unnecessary.
The 1.72 minimum is only needed for tests, but the actual implementation depends on 1.65.
tyranron added a commit that referenced this pull request Dec 5, 2023
## Synopsis

The MSRV bump in #300 is
unnecessary. The 1.72 minimum is only needed for tests, but the actual
implementation depends on 1.65.

## Solution

- Lower MSRV from 1.72 to 1.65.
- Omit tests requiring higher MSRV in `msrv` CI job.
- Describe "MSRV policy" in README.

Co-authored-by: Kai Ren <tyranron@gmail.com>
liveseed added a commit to liveseed/derive_more that referenced this pull request Aug 20, 2024
## Synopsis

The MSRV bump in JelteF/derive_more#300 is
unnecessary. The 1.72 minimum is only needed for tests, but the actual
implementation depends on 1.65.

## Solution

- Lower MSRV from 1.72 to 1.65.
- Omit tests requiring higher MSRV in `msrv` CI job.
- Describe "MSRV policy" in README.

Co-authored-by: Kai Ren <tyranron@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Derive TryFrom<T> for enums marked #[repr(T)]
3 participants