Skip to content
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

2.3.0 breaks HashMap serialization as Vec of pairs with bincode #570

Closed
stefunctional opened this issue Mar 10, 2023 · 2 comments · Fixed by #571
Closed

2.3.0 breaks HashMap serialization as Vec of pairs with bincode #570

stefunctional opened this issue Mar 10, 2023 · 2 comments · Fixed by #571

Comments

@stefunctional
Copy link

HashMap serialization as Vec of pairs with bincode that worked with serde_with 2.2.0 breaks with 2.3.0.

Reproduction

Cargo.toml:

[package]
name = "playground"
version = "0.1.0"
edition = "2021"

[dependencies]
bincode = "1.3.3"
serde = { version = "1.0.154", features = ["derive"] }
serde_with = "=2.3.0"

src/lib.rs:

use serde::Serialize;
use serde_with::serde_as;
use std::collections::HashMap;

#[serde_as]
#[derive(Debug, Default, Serialize)]
struct Foo {
    #[serde_as(as = "Vec<(_, _)>")]
    xs: HashMap<i32, i32>,
}

fn main() {
    bincode::serialize(&Foo::default()).unwrap();
}

Expected output

cargo run passes with no output.

Actual output

cargo run fails with:

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: SequenceMustHaveLength', src/main.rs:13:41
stephaneyfx added a commit to stephaneyfx/serde_with that referenced this issue Mar 10, 2023
Some serializers (e.g. bincode) require a length to serialize a sequence. This code does what `iterator_len_hint` does (called by `collect_seq` in `serde`).

Resolves jonasbb#570
@jonasbb
Copy link
Owner

jonasbb commented Mar 10, 2023

Hi, thanks for the bug report. Sorry for the inconvenience. I think that is a side effect of the changes done for #565. There are other issues with that, so it will need to be reverted.

bors bot added a commit that referenced this issue Mar 10, 2023
572: Revert a breaking change to the Seq trait bound r=jonasbb a=jonasbb

The code released in #566 broke multiple thing. It broke compatability with bincode #570 and #571.

It broke this atribute combination:

```rust
 #[serde_as(as = "Seq<(_, Vec<_>)>")]
BTreeMap<usize, Vec<usize>>,
```

```
error[E0277]: the trait bound `serde_with::Seq<(serde_with::Same, std::vec::Vec<serde_with::Same>)>: serde_with::SerializeAs<_>` is not satisfied
```

bors r+

Co-authored-by: Jonas Bushart <jonas@bushart.org>
stephaneyfx added a commit to stephaneyfx/serde_with that referenced this issue Mar 10, 2023
Some serializers (e.g. bincode) require a length to serialize a sequence. This test verifies that mapping a map to a sequence when serializing provides the length to the serializer.

For jonasbb#570
@stephaneyfx
Copy link
Contributor

No worries. Thank you for this useful crate!

@bors bors bot closed this as completed in 38f638f Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants