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

[Bug]: Roundtripping Option<UnitStruct> always results in None #99

Open
2 tasks done
tekacs opened this issue Jan 8, 2024 · 3 comments
Open
2 tasks done

[Bug]: Roundtripping Option<UnitStruct> always results in None #99

tekacs opened this issue Jan 8, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@tekacs
Copy link

tekacs commented Jan 8, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct

Current Behaviour

Given the type:

pub struct Test;

Some(Test), roundtripped through encode/decode comes back as None.
It appears that it's actually being encoded as None (F6 / null in CBOR).

Expected Behaviour

The value Some(Test) should roundtrip as Some(Test) and be encoded in a fashion which can be recovered.

Environment Information

ciborium = "0.2.1"
serde = { version = "1", features = ["derive"] }

Steps To Reproduce

There's a minimal reproduction of this issue here:

https://www.rustexplorer.com/b/3cig4b

@tekacs tekacs added the bug Something isn't working label Jan 8, 2024
@tekacs
Copy link
Author

tekacs commented Jan 8, 2024

Having now tried this with serde_json in the same reproduction, I find that this issue occurs there too, so... not a ciborium thing?

https://www.rustexplorer.com/b/f3jizk

Apologies. I'll try to figure out why this happens first.

@tekacs tekacs closed this as not planned Won't fix, can't repro, duplicate, stale Jan 8, 2024
@tekacs
Copy link
Author

tekacs commented Jan 8, 2024

So it looks like this is an issue with serde_json as well... but I guess there's an open question as to whether this is solvable in ciborium?

serde-rs/serde#2462

Reopening in case there's any appetite or suggestions to resolve this here.

@tekacs tekacs reopened this Jan 8, 2024
@ahmedcharles
Copy link
Contributor

ahmedcharles commented Feb 19, 2024

If you look at https://serde.rs/json.html

struct X(i32, i32);
let x = X(0, 0); // Represented as `[0,0]`

struct Y(i32);
let y = Y(0); // Represented as just the inner value `0`

struct Z;
let z = Z; // Represented as `null`

This could've easily been [0,0], [0] and []. RON for example, does distinguish None from ().

ciborium could change this but it'd be a breaking change, so not sure if people want to do that. Or it could there could be an option.

serde_derive supports #[serde(transparent)] for [0] to 0. It could have something similar for having [] turn into null, i.e. serialize_none().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants