temp_value being called during the read #569
Replies: 5 comments 3 replies
-
|
can you provide a minimal reproduction? this would help debug. |
Beta Was this translation helpful? Give feedback.
-
|
I'm sorry, I found my issue. I was calling To_bytes internally to calculate the CRC, but of course that is going to trigger the function, and the final to_bytes as the end will trigger it again. I'll have to think of a different way to accomplish this. Thanks for the reply, sorry for wasting your time. I do have a feature request however. Would it be possible to add a flag such that the "update" function be called automatically if present before the serialization? I feel that would make that feature much more powerful. |
Beta Was this translation helpful? Give feedback.
-
|
Sorry I was on vacation. Not to drag this out but I wanted to express my thought better. So instead of the update function modifying the structure (and needing mutability) I was thinking for a way to define a function which would be used as the output. This would work exactly like the "temp_value" but not require it to be "temp". This way a value could be read from a stream and deserialized, and on the output automatically replaced or updated via a function. Maybe it is just the protocol I'm working with, but I have several use cases where I would like to do that. |
Beta Was this translation helpful? Give feedback.
-
|
So basically I want the following to actually work (have a temp_value without it being temp). So if you are reading from this protocol you get the received sequence number and could run whatever validation code. When you serialize a message the value in the structure is not used, the output from the function is used in place. I would use this for the sequence, and I think for the CRC as well. I hope that makes it clear (I know I'm re-using the message, normally this would be a newly constructed message that the user wouldn't care about the sequence). |
Beta Was this translation helpful? Give feedback.
-
|
Yes this will absolutely work (and is what I ended up doing). I just thought because I needed this 3 times doing one example, other people may run into it as well. I guess as you point out, the customized reader/writer isn't much more code so maybe not worth it. I thought it might be super easy given the temp_value is like 99% of what would be needed. I really appreciate the project, and you spending time thinking about this. I won't bother you anymore with my musings. Thanks again. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've got the following and am trying to use the temp_value to write out a sequence number during serialization:
#[deku(temp, temp_value="get_sequence()")]
sequence: u8,
It seems the temp_value is getting called during the de-serialization which I wouldn't expect. I would expect it to read the encoded value during the de-serialization, and use the temp_value when serializing.
Basically I have several spots in the protocol I want to read the value and use it as is during de-serialization, and provide some code during serialization. I know it could be achieved with update....but that has to be called manually with a mutable reference.
Is there any existing mechanism that would achieve this? I'm using 19.1.
Beta Was this translation helpful? Give feedback.
All reactions