jbe
February 4, 2023, 11:14am
1
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
?
H2CO3
February 4, 2023, 11:29am
2
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
jbe
February 4, 2023, 11:40am
4
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
Even if it is intended, it might be insufficiently documented.
jbe
February 4, 2023, 11:59am
6
I opened the following issue:
opened 11:58AM - 04 Feb 23 UTC
Using version `2.0.0-rc.2`, the following code:
```
fn main() {
let arr… ay: [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:
```text
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `[3, 11, 12, 13]`,
right: `[11, 12, 13]`', src/main.rs:6:5
```
This means [`bincode::encode_to_vec`](https://docs.rs/bincode/2.0.0-rc.2/bincode/fn.encode_to_vec.html) isn't compatible with [`bincode::serde::encode_to_vec`](https://docs.rs/bincode/2.0.0-rc.2/bincode/serde/fn.encode_to_vec.html). This is highly surprising.
See also: [How can bincode distinguish between tuples and (fixed size) arrays?](https://users.rust-lang.org/t/how-can-bincode-distinguish-between-tuples-and-fixed-size-arrays/88756) on URLO.
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
system
Closed
May 5, 2023, 12:12pm
8
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.