bincode-org / bincode

A binary encoder / decoder implementation in Rust.
MIT License
2.63k stars 265 forks source link

Saving a Configuration in a Generic Variable #678

Closed thekief closed 10 months ago

thekief commented 10 months ago

This issue is somewhat of an extension of my last issue #666 that asked regarding adapting the configuration in regards to endianness.

Right now I create two configurations (p.e. le_config and be_config) depending on the endianness and when parsing I include an if-statement to add the proper configuration. Similar to this:

let (item, _): (HeaderItem, usize) = bincode::decode_from_slice(
            data,
            if is_little_endian {
                le_config
            } else {
                be_config
            },
        )
        .unwrap()

I would like to get rid of the clause by storing the configuration in a variable. I looked into it and could not find a solution for this. Is my assumption correct that this is a similar case to #399 ?

VictorKoenders commented 10 months ago

Yes this is similar to #399.

We use the type system to optimize the encoding and decoding of data, so it is not possible to have a dynamic configuration