Skip to content

json_string and embedded struct #53

@v1gnesh

Description

@v1gnesh

bitfield is supposed to return the parsed struct. Instead it just returns input array (bytes).
(fn from_bytes([u8; 1]) -> Self)
Ref: https://docs.rs/modular-bitfield/latest/modular_bitfield/#generated-implementations

To reproduce:

#![allow(dead_code)]

use std::io::Cursor;
use binrw::BinRead;
use modular_bitfield::prelude::*;
use simd_json_derive::Serialize;

#[bitfield]
#[derive(Debug, BinRead, Serialize)]
#[br(map = Self::from_bytes)]
pub struct Flags {
    r1: B3,
    v1: B1,
    v2: B1,
    v3: B1,
    v4: B1,
    r2: B1,
}

#[derive(Debug, BinRead, Serialize)]
#[br(big)]
pub struct Outside {
    y0: u16,
    y1: u16,
    f1: Flags,
}

fn main() {

    let rec = vec![0x01, 0xB0, 0x00, 0x00, 0x1e];

    let value = Outside::read(&mut Cursor::new(rec)).unwrap();

    println!("\n{}", value.json_string().unwrap());
    // println!("\n{:?}", value);
}
println!("\n{:?}", value);

Outside { y0: 432, y1: 0, f1: Flags { r1: 6, v1: 1, v2: 1, v3: 0, v4: 0, r2: 0 } }
println!("\n{}", value.json_string().unwrap());

{"y0":432,"y1":0,"f1":{"bytes":[30]}}

So it looks like json_string is acting before binrw can derive those bytes.
When trying to manually impl Serialize for Flags, self. only shows self.bytes.
This explains why json_string just dumps the bytes as is.

Any idea how to make this right?

Somehow, Serialize should run after binrw's map.

But placing it like this (innocently) doesn't help:

#[br(map = Self::from_bytes)]
#[derive(Serialize)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions