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

Replace FromPrimitive with our own tag macro #63

Merged
merged 8 commits into from
Nov 12, 2019
Merged

Replace FromPrimitive with our own tag macro #63

merged 8 commits into from
Nov 12, 2019

Conversation

HeroicKatora
Copy link
Member

This is more flexible and removes the dependency.

Note that FromPrimitive was used with only a single primitive type as the source. This not only increases the public interface much more than necessary but is also misleading. All fields that can be converted as such have specified widths iirc.

The num-derive crate does also not handle non-exhaustiveness which is a major future compatibility hazard as more tags etc. are to be supported.

Closes: #57, #59

Factor out parts that are unique to instantiations such as the name, the
documentation, the underlying type, the public conversion methods.

Also adds the ability to document individual items and remodels the
syntax to more closely resemble ordinary enum definitions.
Several other uses that currently rely on num-traits for conversion have
no Unknown representation. Thus, the converter now outputs only known
variants or an error and the exposing function then can put the error
value into an Unknown variant instead.
The discriminant of some enum types can be retrieved with the `as`
operator and in C style enums it is also possible to set a specific
discriminant. However, that may silently change—breaking the value
written to the file. It is better to use that technique mostly for ffi
purposes.
Copy link
Contributor

@fintelia fintelia left a comment

Choose a reason for hiding this comment

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

I generally don't like using macros if there are any reasonable alternatives, but I really don't see any here so LGTM

src/decoder/ifd.rs Outdated Show resolved Hide resolved
$(if n == $val { Tag::$tag } else)* {
Tag::Unknown(n)

impl $name {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm coming to this somewhat late so perhaps I'm missing some context, but is there a reason not to replace the methods below with impls of TryFrom<$ty> for $tag and From<$tag> for $ty?

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought I'd minimize the public interface that comes from within the macro. It might still be a good idea to expose those.

@HeroicKatora
Copy link
Member Author

HeroicKatora commented Nov 12, 2019

I generally don't like using macros if there are any reasonable alternatives, but I really don't see any here.

Yeah, sadly. We'll get #[non_exhaustive] (see rust-lang/rust#44109) in a few releases and when we upgrade the compile requirements then we can definitely think of reworking this to purely perform discriminant casting but that will not provide a safe option for fn from_u16(u16) -> Option<Self>...

Thanks to the review comment for pointing this style issue out.
src/decoder/mod.rs Outdated Show resolved Hide resolved
Turns out the only use was as a private bound which was unused within
that method.
@HeroicKatora HeroicKatora merged commit 8e1c67e into master Nov 12, 2019
@HeroicKatora HeroicKatora deleted the tagged branch November 12, 2019 19:14
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.

num-derive dependency worth it?
3 participants