Skip to content

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

@robot-rover

Description

@robot-rover

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions