-
Notifications
You must be signed in to change notification settings - Fork 38
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
Pad a vector's items #238
Comments
As an alternative a directive could be added to pad a whole struct, which in my example is |
Hi, sorry for the delay. #[derive(BinRead)]
#[br(import(arbitrary: u8), little)]
// Is 7+ bytes but needs to get padded to `arbitrary`
pub struct Bar {
f1: u8,
f2: u16,
len: u32,
#[br(count = len, pad_after = u32::from(arbitrary) - len - 7)]
v: Vec<u8>
}
#[derive(BinRead)]
pub struct Foo {
arbitrary: u8,
len: u8,
#[br(args { count: len.into(), inner: (arbitrary,) })]
pub list: Vec<Bar>
} |
I've been using this functionality recently too and I don't believe that it's a viable solution to expect developers to write reliable code by manually computing sizes of fields. My current workaround so far has been to create a padded wrapper: #[derive(BinRead, Debug)]
struct TestList {
n: u8,
s: u8,
#[br(args { count: n as usize, inner: binrw::args!{ padding: s } })]
elements: Vec<Padded<Element>>,
}
#[derive(BinRead, Debug)]
#[br(little, import {
padding: u8,
})]
struct Padded<T>
where
T: BinRead<Args<'static> = ()> + std::fmt::Debug + Sized + Default,
{
#[br(pad_size_to = padding)]
inner: T,
} and then add a Deref on top of that. Obviously this is not feature complete with correctly passing args through to inner members. |
I spent a couple minutes making a draft of changes, hopefully someone with more insight can polish it up? |
PTAL #250 @csnover @jam1garner |
This doesn't currently work
The text was updated successfully, but these errors were encountered: