-
Notifications
You must be signed in to change notification settings - Fork 35
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
Better way for parsing trait-marked constants in struct fields #19
Comments
I've been considering this and it may make the most sense to implement methods in the marker trait. Do you have any thoughts on this @gluxon? |
Were you thinking of adding a deserialization method, or something else? |
What I'm considering is some sort of approach that takes advantage of the fact that every enum that implements a given marker trait will have variants such that when each variant is converted to an integer, it will have a unique value across all variants in the set of variants for enums implementing the marker trait. This is guaranteed because each integer value must be unique in fields that are not bitwise flags which will never have marker traits. How to implement this is a bit trickier as doing it automatically sounds a bit more complicated but it may be possible to do manually with some |
So I was thinking of something like this, but it doesn't work since type ids are defined at runtime. #[derive(neli)]
enum GenlResponse {
#[type(3,4)] Nlmsg(Nlmsg),
#[type(16)] CtrlCmd(CtrlCmd),
}
Yeah, I'm leaning towards this as well. Maybe a generic |
Hi Brandon, I've been thinking on this for a bit and I'm pretty sure I have a solution. It's going to take some reworking of the macros but I'd like to do this now rather than down the road. Proposed solutionI'd like to remove the macro
impl_trait!(MarkerTrait, WrapperName, u16, Nla, AnotherConst); This would generate pub enum WrapperName {
Nla(Nla),
AnotherConst(AnotherConst),
UnrecognizedVariant(u16),
} In addition, BenefitsDealing with returning one of a few types in How does this sound? |
Pretty good so far. From my understanding, the proposed reworkings of |
Hi Brandon, I'm not sure I understand. Can you clarify a little bit? |
Sure. From my perspective, there's 2 (heavily related) improvements that the Lines 32 to 42 in 899a5c5
I was hoping we could address the 2nd point in a more general way, but the reality is that this issue probably only comes up with error responses. Meaning we could just have the socket iterator spit out a |
Hi Brandon, finally getting back around to this! I'm going to try to resolve some of these older issues for the v0.5.0 release now that I have a little more time to attend to neli again. I think you'll be very interested in #50 as this directly ties into some of what you mentioned here. Feel free to give feedback there if you have any thoughts. I do think when parsing based on a marker trait my proposed solution makes the most sense for a limited scope item to resolve. I think #50 is a much larger project that is equally or more important but will need a different approach. This can be handled by reworking types a bit while issue 50 seems like it will require some ability to try parsing and then reparsing as something else if it fails, all supported by the ability to rewind the buffer. How does it sound if I go ahead with the macro changes for now and leave #31 and #50 to cover your other concerns? |
Take a look at the linked commit above. I think that should resolve this. |
Hey, sorry for the late response. I won't have time for an in-depth code review but if it implements what you proposed above I'm definitely excited for it. 🙂
Will do. The poster there is asking the question I was. |
This should be fixed via e868974. Closing. |
The current way I get around the many constants that can be passed into certain fields like
Nlmsghdr.nl_type
is separating each set of constants into its own finite enum and marking the enum as compatible with this field by implementing theNlType
trait for that enum. This is a good first step but a use case that came up in this pull request made it clear that currently the best way to differentiate between different enums when parsing a stream where for examplenl_type
could be one of multiple enums is to use au16
as the type there and then manually try parsing as both enum types. This is not an ideal way to do this and I'm interested in figuring out a better way to do this.The text was updated successfully, but these errors were encountered: