How can bincode distinguish between tuples and (fixed size) arrays?

Continuing the discussion from Is it better to use bincode or postcard?:

// uses `bincode` version "2.0.0-rc.2"

fn main() {
    let tuple: (u16, u16) = (44, 55);
    let array: [u16; 2] = [44, 55];
    let stdcfg = bincode::config::standard();
    assert_eq!(bincode::encode_to_vec(&tuple, stdcfg).unwrap(), &[44, 55]);
    assert_eq!(bincode::encode_to_vec(&array, stdcfg).unwrap(), &[2, 44, 55]);
}

How can bincode distinguish between tuples and (fixed size) arrays? Aren't both going through the serde type tuple?

There's no 2.0 version of bincode. With the latest 1.3 version, the two data types serialize to the exact same representation.

Seems like the bincode function in the code example doesn’t use serde, and there’s another one that does, treating arrays and tuples equally: Rust Explorer

1 Like

:scream:


This means that the following code:

// uses `bincode` version "2.0.0-rc.2"

fn main() {
    let array: [u8; 3] = [11, 12, 13];
    let stdcfg = bincode::config::standard();
    assert_eq!(
        bincode::encode_to_vec(&array, stdcfg).unwrap(),
        bincode::serde::encode_to_vec(&array, stdcfg).unwrap(),
    );
}

Fails with:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `[3, 11, 12, 13]`,
 right: `[11, 12, 13]`', src/main.rs:6:5

That's not good. This means bincode::encode_to_vec isn't compatible with bincode::serde::encode_to_vec. I don't think this is intended, is it?

Note I'm referring to version 2.0.0-rc.2, which is a published (non-yanked) release candidate.

I don’t know, I’m not familiar with that crate at all. If you feel like this might be not intended, open an issue on their repo :slight_smile:

Even if it is intended, it might be insufficiently documented.

I opened the following issue:

Right… it’s almost as if they got their point of which one of skip_fixed_array_length and write_fixed_array_length is supported by bincode::serde::… the wrong way around.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.