HashMap<String, DisplayFromStr>
#473
-
Hi, #[serde_as]
#[derive(Debug, Clone, Deserialize)]
pub struct Configuration {
#[serde_as(as = "HashMap<String, DisplayFromStr>")]
pub endpoints: HashMap<String, Uri>,
} But I can't make this working, I get this compilation error:
Interestingly, Thank you for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I've found out the solution 😄 #[serde_as(as = "HashMap<serde_with::Same, DisplayFromStr>")] The reason is that the implementation of #[serde_as(as = "HashMap<_, DisplayFromStr>")] works fine. |
Beta Was this translation helpful? Give feedback.
I've found out the solution 😄
#[serde_as(as = "HashMap<serde_with::Same, DisplayFromStr>")]
The reason is that the implementation of
DeserializeAs
forHashMap
requires both K and V to implementDeserializeAs
(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 apparentlyString
is not automatically inferred to implementDeserializeAs
, hence usingSame
forcesString
to be resolved asDeserializeAs
through its serdeDeserialize
implementation. Also using:#[serde_as(as = "HashMap<_, DisplayFromStr>")]
works fine.