From: Markus Fischer Date: 2009-06-13T20:54:19+09:00 Subject: bit-struct feedback (was: Re: binaryparse: length in bytes of block definition?) Hello Joel, I've now rewritten my code to take advantage of you latest 0.12 changes and have to say that it was a very pleasing experience. Everything worked out of the box and the addition of the IO object was a real time safer in moving from binaryparser to be bit-struct-only. During this rewrite-session I made some notes how my further bit-struct experience went. If you're annoyed by them, simply ignore 'em ;-) * Allow block also for "nest" ? It may sound redundant in the first place, but when it's just for providing more structure without requiring an extra class definition, it could be useful: class Foo < BitStruct nest :coord do signed :x, 8 signed :y, 8 end end Foo.new.coord.x * Is explicit :length for vector necessary? class Tag < BitStruct signed :flags, 32 vector :axis, Vec, :length => 3 end compared to class Tag < BitStruct signed :flags, 32 vector :axis, Vec, 3 end Or am I missing any ambiguities because of the simple cases? * Allow length not only in bits, but also bytes? In my cases I've never encountered a situation where I needed a member of a structure to represent anything which is not on a byte boundary. All my signed and unsigned int have to read "32" (32bit, 4bytes) all over the place. For strings I'm writing "64*8" for string with 64 characters. How about a new default_option :granularity which is "1" by default. When set to "8", every specified length (except for vector ...) is multiplied by that factor before used? * Why "vector" and not just "array"? I'm getting pretty confused all the time due the term vector :) I'm always thinking about graphical vectors, etc. For, me arrays would also better describe the environment how the data is used (array of ints .. vector of ints ... ?). Joel VanderWerf wrote: > Markus Fischer wrote: >> C-struct example: >> >> typedef struct { >> float normal[3]; >> float dist; >> } dplane_t; >> >> Ruby (?): >> >> class MyFloat < BitStruct >> float :value, 32 >> end >> >> class Plane < BitStruct >> vector :normal, MyFloat, :length => 3 >> float :dist, 32 >> end > > and then > > Plane.new.normal[2].value As you said, looks like one layer to much. Thanks for your block-support to vector, could this maybe be detected for simple cases? class Plane < BitStruct vector :normal, :length => 3 do signed 32 end end I.e., having only one element and deliberately skip the naming to be prepared for Plane.new.normal[2] > As you can see, the reader method is fine, but the writer has to be > broken down into a sequence of writes to each nested chunk... yuck. > > The reason it doesn't allow simply > > md.row[2].col[7].x = 1.23 [...] > > Any other ideas...? Unfortunately not at this time. I'm using bit-struct solely for reading binary data ATM and not creating them. thanks, - Markus