Replies: 1 comment 1 reply
-
Hi! Maybe it is not important but can you talk about the keys more? Are bits 0-2 are size and then bits 3-7 are the value type? I just want to make sure that you’re not going to end up in an impossible situation later since the whole byte is consumed by magic so you will need to match every permutation instead of expecting to retain the rest of the key for additional processing later. With regards to writing only the keys, I think that your best bet probably is to use some free function that maps the items to just their key values instead of trying to do extra writes and then meddling with positioning to overwrite them. With regards to duplicating magic bytes, I think magic is restricted to literals only to avoid user error (i.e. thinking that a variable could be used as magic, which it cannot because it goes into the pattern part of a match arm) rather than a specific technical reason, so probably a patch would be accepted to loosen the grammar rules to allow a literal or path and then you would be able to use constants. |
Beta Was this translation helpful? Give feedback.
-
Hello,
Firstly, thank you for this amazing package ! It's saving me a huge amount of time and effort.
I'm trying to find a way of working with key-value pairs. Specifically, the keys are all u32, and the value is one of the main standard numerical data types. Three bits in the key tell you the number of bytes in the subsequent value, but not the type itself (e.g. you know that the following 4 bytes make up your value but not whether that's
i32
,u32
orf32
).To work with this, I've created an
enum
having each key as a magic, falling through to a struct which stores the original key and bytes.However, in some cases, I only need to pack the keys into an array of bytes. I can't find a way to do this cleanly; the magic doesn't seem to accept a different enum in repr u32, or a const u32 variable as its value, so the snippet below won't compile.
I was thinking I could possibly try and do some conditional cursor movement to overwrite any data, or just post-process the array of bytes to cut out the values from the key-value pairs, but that's still a bit ugly since I'd still be instantiating each enum case with dummy data that's not used. I'd like to avoid maintaining the key definitions in two places since there are quite a few.
Have you got any suggestions how to handle this?
Beta Was this translation helpful? Give feedback.
All reactions