-
Notifications
You must be signed in to change notification settings - Fork 1
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
WIP: conditionally read Option<T> #2
Conversation
First of all, I'd like to say that this would be a good addition, so thanks for taking the time to work on it! #[protocol(default_if = "...")]
#[protocol(skip_if = "self.optional.is_none()")] // This would actually be redundant in this scenario, but I'm leaving it for clarity
#[protocol(length = "1")]
optional: Option<T> |
I'm not sure I like the idea of implementing So, just thinking out loud here: I'm going to read some more into Deku to see how they solve it. Deku is nice, but its api is really rather complex once you start doing stuff outside its scope. |
I'm not sold on using However, I see a lot of potential in using I think we could apply a very similar solution to enums, by allowing either The length prefixes were already getting quite clunky, so I was thinking working on an attribute macro that prepends the length prefix to the field without requiring a length field to be explicitly present, so if you wanted to keep using the Option in the same way that you currently do, you could, but you also have the option of having it be external. But, we could maybe take a similar approach to my suggestion for enums, where we impl Protocol for types like |
That does seem like a clean solution, indeed! |
This seems like it will be quite a big change, so are you willing to implement it? |
I'll see what I can do, but I can't guarantee anything; after looking what I use from deku, I realize that I'd need a lot more than this. So I may just stay with deku :/ |
Alright, just let me know if you end up not implementing this, as it would be quite a valuable addition, so I can add it instead. Good luck! |
Hey @vhdirk, I had a free day today so I decided to implement this more generic tagging. You can now do the following to have a tag with any type prepended directly to your type: #[protocol(tag(type = "u16", write_value = "self.prefixed_arr.len() as u16"))]
prefixed_arr: Vec<u8>, If you want it separately, you can do so too: #[protocol(write_value = "self.opt.is_some()")]
flag: bool,
#[protocol(tag = "flag")]
opt: Option<u32>, And this applies for enums too. You said you would need a bunch of other functionality from deku as well to switch, but I've run out of ideas, so I'd love to hear what else you'd want to see. |
Hi. Thanks for your hard work! I'm regarding this PR as closed then, and opening a new one :) |
So, I really like the readability of bin-proto's internals. But feature-wise, it's not where I'd need it to be to replace deku.
For one, I'd like the ability to conditionally read an Option, based on an external field. Kinda like externally length prefixed, but for Option.
I started making it, but I quickly realized that, in order for it to be compatible with other attributes, like
bits
, it start's getting somewhat complicated. And from what I can tell, with the current infrastructure, I'd need a traitConditional
andConditionalWithBitfield
or something alike? That feels somewhat clunky.I guess it can be easily solved using the new Ctx system, but I'd like for it to be a first-class citizen.
What's your opinion on this?