Skip to content

HashMap<String, DisplayFromStr> #473

Discussion options

You must be logged in to vote

I've found out the solution 😄

#[serde_as(as = "HashMap<serde_with::Same, DisplayFromStr>")]

The reason is that the implementation of DeserializeAs for HashMap requires both K and V to implement DeserializeAs (see https://docs.rs/serde_with/1.14.0/serde_with/de/trait.DeserializeAs.html#impl-DeserializeAs%3C%27de%2C%20HashMap%3CK%2C%20V%2C%20S%3E%3E-for-HashMap%3CKU%2C%20VU%2C%20S%3E), but apparently String is not automatically inferred to implement DeserializeAs, hence using Same forces String to be resolved as DeserializeAs through its serde Deserialize implementation. Also using:

#[serde_as(as = "HashMap<_, DisplayFromStr>")]

works fine.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@jonasbb
Comment options

@slinkydeveloper
Comment options

Answer selected by slinkydeveloper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants