Support NifMap decoding when elixir map has missing fields#746
Open
DanielSidhion wants to merge 3 commits into
Open
Support NifMap decoding when elixir map has missing fields#746DanielSidhion wants to merge 3 commits into
DanielSidhion wants to merge 3 commits into
Conversation
This is done in preparation to add field attributes to rustler, which will reuse some existing logic.
DanielSidhion
commented
Jun 12, 2026
|
|
||
| use syn::{Ident, Lit, Meta}; | ||
|
|
||
| pub(crate) trait TryFromRustlerNestedAttr: Sized { |
Author
There was a problem hiding this comment.
The name for this trait should change to be a bit more global than just "rustler nested attributes", but I couldn't think of anything interesting. Appreciate any suggestions.
Since field attributes might change the behaviour of code generated for a specific field, we introduce an extra struct to hold information about the field and which attributes it has to make it easier for future code to work with them.
This allows Elixir maps with missing keys to decode as None variants for fields that are Options, as long as the attribute is applied to them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #745.
This PR currently only implements the behaviour for
NifMap, but it could in theory supportNifStructas well, although the utility of that is way lower, since elixir structs will always have the fields - but in Rust they could map to a struct with more fields that will always becomeNone.It also only implements the decoding behaviour, but encoding could be done similarly: if the option is
None, don't add that key to the elixir map. I could implement that for completeness.The PR has 3 commits, it might be easier to review commit by commit to understand the changes faster.
The implementation is not ideal (e.g. the way I check whether a type is
Optionhas false negatives and false positives, as explained in the comments), but given the alternative (explained later) and how the rest of the code behaves, I decided this was a good approach to take.Instead of checking if the type of a field is
Option, we could've done the entire implementation at runtime: when the elixir map doesn't have the field we're looking for, delegate the return value to a trait. This trait would returnOk(None)forOption<T>types, and andErr(_)value for all other types. However, this would require some sort of extra trait to gate the implementation specifically forOption<T>, because we can't just have an implementation forTandOption<T>. My idea here would be to add an extra "marker" trait for each type that implementsrustler::Decoderexcept forOption<T>. This would be a lot of overhead compared to just checking the type at procgen time, so I opted for that approach instead.