Search code examples
rustserde

How to see serde's generated implementation of Deserialize?


I use:

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Foo {
    ...
}

I want to write a custom Deserialize for Foo.

How can I see the derived Deserialize code which serde generates? I'd like to use that as a starting point.


Solution

  • How can I see the derived Deserialize code which serde generates? I'd like to use that as a starting point.

    You can ask rustc to dump the macro-expanded code (it's under "tools" in the rust playground). Since the rustc invocation is a bit complicated especially for a full-blown cargo-based project, rustacean extraordinaire David Tolnay has published a cargo expand command which you can cargo install, which basically does the annoying work for you.

    I would not recommend it as a starting point when it comes to serde tho, the generated ser/de code is somewhat gnarly / hard to read. I would suggest going through the serde documentation instead, especially serde datamodel and custom serialization.