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 Decode for transparent enums #14

Merged
merged 2 commits into from
May 30, 2024

Conversation

bsh98
Copy link
Contributor

@bsh98 bsh98 commented Jan 25, 2024

Summary

Adds support for deriving ssz::Decode on #[ssz(enum_behaviour = "transparent")] enums by attempting to decode all variants in order until one is successful. Returns ssz::DecodeError::NoMatchingVariant if none can be successfully decoded. This is the same approach used by serde for their untagged enum representation.

Motivation

This is useful in situations where we are expecting one of a few distinctly decodable types.

Considerations

In some cases, the ordering of the enum variants will matter and this may not always be obvious to the user. The user must be careful not to include a variant that will "shadow" subsequent variants. It would be ideal to warn/panic in these cases. (Is this possible?)

Copy link
Member

@michaelsproul michaelsproul left a comment

Choose a reason for hiding this comment

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

Sorry for the slow review!

As you noted, this sort of Decode implementation has its downsides. I would be OK with merging this PR anyway, as users can opt-out of deriving Decode if they don't desire this behaviour.

I'm afk at the moment but will review the code more thoroughly tomorrow

Copy link
Member

@michaelsproul michaelsproul left a comment

Choose a reason for hiding this comment

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

Nice, very clean impl. I'll publish a new release with this change shortly!

@michaelsproul michaelsproul merged commit 981cf5a into sigp:main May 30, 2024
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