-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Attribute to handle length of bytes(or bits) in Vec<T> with other struct field #76
Comments
Just to be clear, |
Correct, since that is the length of elements in the Vec. |
Does struct A {
len: u8,
#[deku(ctx = "len")]
messages: Vec<Message>
}
#[deku(ctx = "byte_len: u8")]
struct Message {
#[deku(bytes = "byte_len")]
msg: u8,
} |
I don't think so, since it's not the length of the bytes in one Message, it's the length of All the Messages in a Vec. Reminder that However, having the following issues when testing your code:
errors:
|
Yeah, that's just a demo.
hmm, the max bytes that Vec can have is |
I am just trying to figure out a way of parsing |
Ah, understood. Currently there is no way to do this. pub struct Packet {
#[deku(bytes = "1")]
length: u8,
// Read all required bytes, then parse it.
#[deku(
count = "length - 2",
map = "|all_msg: Vec<u8>|Vec::<Message>::try_from(all_msg)"
)]
messages: Vec<Message>,
} Anyway, I agree that this feature should be added. |
It might help to provide a more concise example to work off of... is there a header format you're trying to parse? What is the advantage to "Read all requred bytes, then parse it" ? |
In the asterix protocol, on page 24. It has a listed two-octet field LEN that gives the total length in octets of the record (Message in my example) Notice the variable length of Data Fields depending on the fspec defined. |
I understand now, thanks! Kinda sounds similar to reading TCP Options, I have used a custom I agree this feature would be usefull. |
Would something like this make sense? Allowing the use of pub struct Packet {
#[deku(bytes = "1")]
length: u8,
// Read all required bytes, then parse it.
#[deku(bytes = "length - 2")]
messages: Vec<Message>,
} |
lgtm |
I wish we could re-purpose The easiest way for now is to create something like @constfold described above or use custom reader/writer attributes |
Does |
The
This is interesting.... |
With @samuelsleight changes, something like this may now be possible: #[deku(bits = "5")]
field_a: Vec<u8> #[deku(bytes = "3")]
field_a: Vec<u8> impl<T: DekuRead> DekuRead<BitSize> for Vec<T>
{
fn read(
input: &BitSlice<Msb0, u8>,
bit_size: BitSize,
) -> Result<(&BitSlice<Msb0, u8>, Self), DekuError>
where
Self: Sized
{
read_vec_with_predicate(input, Vec::new(), (), move |read_bits, _| {
read_bits == bit_size.into()
})
}
} |
So, I've just sent a PR (#124) that will allow the following code to work as expected: #[derive(Debug, PartialEq, DekuRead, DekuWrite)]
pub struct Packet {
length: u8,
#[deku(bytes_read = "length - 2")]
messages: Vec<Message>,
} As no decision had yet been made here as to the exact attribute I've opted to make a decision just to get something functional implemented, and have gone with |
Consider the following code:
I can use the
count
attribute to give the length ofVec<T>
elements, but I see no way of telling the max bytes that a Vec can have in it's container as a whole. Wondering if this would be a feature, or do I need to do some weird custom write/read implementation with aread_bytes
field.The text was updated successfully, but these errors were encountered: