Comments (3)
Away from my computer, but this should work:
use deku::prelude::*;
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct Bit(#[deku(bits = "1")] u8);
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct Container {
qty: u8,
#[deku(count = "qty")]
bits_data: Vec<Bit>,
}
I would even prefer to read/write into BitVec for easier access and optimal memory footprint.
Interesting idea, I'd support a MR for this.
from deku.
Thanks, that works. I also added padding as in the code below to align on bytes boundary. Its also required for try_form
not to fail on too much data. See below.
#[derive(Debug, PartialEq, DekuRead, DekuWrite)]
struct BitContainer {
qty: u8,
#[deku(count = "qty", pad_bits_after="(qty+7)/8*8-qty")]
bits_data: Vec<Bit>,
field_after: u8
}
What's still missing is automatic filling of qty on write, so I won't need to fill it in the code.
- I can't do it using writer, because the
pad_bits_after
would not work. On read I need padding to refer to the readqty
but on write I need it to refer to the length ofbits_data
. (Interestingly, from my tests, on write deku ignores the 'qty' and decides how many bits to save based on theVec<Bit>
size, I think this is worth explaining in the documentation) - I can solve this using
update
on theqty
field. But that's error prone since it requires client code to remember to update prior to save. I think there would be value in having an attributeupdate_on_write
that executes right before write and has access to self that can calculate automatically fields that can be deduced from other fields - I can do it using writer/reader for only
bits_data
and expose only bits_data with qty taken care of behind the scene. - Could I define a new type
type MyVecBit = Vec<Bit>
and implement relevant traits to do the same as 4, only seamlessly from the person defining the structure? Any examples for that? Because this pattern will be used in multiple structures and I don't want to have verbose definitions all over
from deku.
I think there would be value in having an attribute update_on_write that executes right before write and has access to self that can calculate automatically fields that can be deduced from other fields
Agreed, this would need to be a breaking change, as DekuWrite
doesn't mutate self
.
from deku.
Related Issues (20)
- bit order in reader HOT 6
- Single value enum HOT 2
- Deku doesn't compile on ESP32 no-std HOT 3
- Missing support for core::net::Ipv4Addr/etc. HOT 1
- Replacing indices with references during parsing.
- Under / over-sized buffer parsing HOT 5
- Enable MIRI testing
- Help passing context around HOT 1
- Revisit enum id & type
- How to properly debug Deku input data? HOT 4
- Seemingly nonsensical vector count/read_size error HOT 2
- Conditional DekuWrite HOT 3
- Error: Parse("Too much data") using custom readers HOT 1
- Cant "count" from Some(field) directly - need as_ref().unwrap() HOT 1
- Passing a struct in ctx creates opaque error HOT 7
- Doc request: update for structs
- How to access enum-struct members? HOT 4
- Assert for tuple enum HOT 1
- Working with elements of Vec<StructUsingDeku> to get sum of their byte-sizes HOT 4
- Weird parsing with `bool` and non-aligned data. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deku.