-
Notifications
You must be signed in to change notification settings - Fork 17
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
Provide a big endian example #7
Comments
@hecatia-elegua Hello! I came across this crate recently and it looks nice & tidy. |
@v1gnesh hey, thank you :) Most useful would be if you'd use bilge in a project with BE, fiddle around if it doesn't work and report it here. |
It's for use in reading a file... like this. |
and that's unefficient? got it. let mut reader = Cursor::new(b"DOG\x02\x00\x01\x00\x12\0\0Rudy\0");
let dog: Dog = reader.read_ne().unwrap(); would |
I have So something in either of these places would be nice: #[bitsize(32)]
#[derive(FromBEBits)] Don't know if it's possible in the |
Hmm, I've seen binrw We'll definitely add a/multiple derive/s for Still not sure which one you need. Still would be nice if you'd code up your small example with bilge so I can see what it spits out and should spit out. |
@hecatia-elegua, I hope this example with modular_bitfield is sufficient to convey what I'm trying. Things to note: #[macro_export]
macro_rules! serialize_bitfield {
( $struct_name:ident { $($field_name:ident),* } ) => {
impl Serialize for $struct_name {
fn json_write<W>(&self, writer: &mut W) -> simd_json_derive::Result
where W: std::io::Write,
{
$(
self.$field_name().json_write(writer)?;
)*
writer.write_all(b"")
}
}
};
}
serialize_bitfield!(Flags { b7, b6, b5, b4, b3, b2, b1, b0, });
#[bitfield]
#[derive(Debug, BinRead)]
#[br(map = Self::from_bytes)]
pub struct Flags {
b0: B1,
b1: B1,
b2: B1,
b3: B1,
b4: B1,
b5: B1,
b6: B1,
b7: B1,
}
#[derive(Debug, BinRead, Serialize)]
#[br(big)]
pub struct Outside {
a: u16,
b: u16,
c: Flags,
#[br(if(c.b1() == 1))]
d: u8,
}
fn main() {
let data = [0x01, 0xB0, 0x00, 0x00, 0x1E, 0x0E];
let value = Outside::read(&mut Cursor::new(data)).unwrap();
let val = value.json_string().unwrap();
println!("\n{}", &val);
} Output: {"a":432,"b":0,"c":00011110,"d":14} |
@hecatia-elegua, It seems that reverse_bits will come in handy. |
Are you just using json because you want to print? Then you don't need "c":{"b0":0,"b1":0,...} but it actually doesn't matter a lot, since you can extend bilge with your own
We do have accessor functions ( |
this does it: #[br(map = |x: [u8; 1]| Self::from_bytes(x.map(|b| b.reverse_bits())))] now we just need the |
I'm looking to convert from a custom binary format to a universal format; so started with json (simd-json-derive).
Yeah, that's what I get. Accessor functions will help in custom parsing, but in the final output, just printing bits is fine.
Yup, so in the post above, you'll see I'm passing the field names in reverse to the macro (for impl).
Thanks for this example, I'm pretty slow in getting things when it comes to programming. writer.write_all(format!("{:b}", self.value).as_ref()) ... ah but I can't get |
in edit for posterity: I meant |
Looks like binrw needs to add support for this first. |
This has nothing to do with |
Try this? -- https://gist.github.com/rust-play/08742b3d2a5ac8b7eba6b2919bd2b310 Just noticed again... yes, it's the Impl Serialize that's having trouble getting to |
ah sorry I completely missed putting x into the struct, see: |
Thank you for showing me. |
It should show it, but it was pretty low on the autocomplete list for me. |
Right now, we fill up our inner field from the right, i.e.
will be saved as:
0b1_00000000_00000010_01
on little endian and afaik0b1_00000010_00000000_01
on big endian.Since we get/set the whole field, this should just work.
So, provide an example for big endian (implement an actual usecase).
The text was updated successfully, but these errors were encountered: