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

Flattening a Unit aka () should produce no extra fields #1873

Closed
robot-rover opened this issue Aug 10, 2020 · 1 comment · Fixed by #1874
Closed

Flattening a Unit aka () should produce no extra fields #1873

robot-rover opened this issue Aug 10, 2020 · 1 comment · Fixed by #1874

Comments

@robot-rover
Copy link

I'm designing an http api where every response is json and has diffferent fields depending on the api call, but always has a status and code field. I am acomplishing this through the Response struct:

#[derive(Debug, Serialize)]
pub struct Response<T> {
    #[serde(flatten)]
    data: T,
    status: Status,
}

In situations where I just want to return a status, I intended on constructing a Response<()>, but that gives me this error:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("can only flatten structs and maps (got unit)", line: 0, column: 0)', src/libcore/result.rs:1189:5

I expected that flattening a Unit would just add no fields. I could make a struct with no fields and make a Response<NoFields>, but that seems unidiomatic and requires adding an extra type that doesn't do anything. Is there a reason that you can't flatten a Unit?

@dtolnay
Copy link
Member

dtolnay commented Aug 10, 2020

Fixed in 1.0.115.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants