Repository navigation
Can you directly call MapPreventDuplicates::deserialize_as() to get a HashMap with no duplicates?
#976
|
Hello! I've got a pub struct CustomProps(HashMap<String, String>);
impl<'de> Deserialize<'de> for CustomProps {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
// how can I also check for duplicates using `MapPreventDuplicates`, if at all?
let custom_props: HashMap<String, String> = HashMap::deserialize(deserializer)?;
if !custom_props.is_empty() {
println!("Note! You have custom properties");
}
Ok(Self(custom_props))
}
}I also want to ensure there are no duplicate values using let custom_props: HashMap<String, String> = MapPreventDuplicates::deserialize_as(deserializer)?;but I can't figure out how the type annotations for Thanks! |
Replies: 1 comment 2 replies
|
You can use So I think this should work here: let custom_props: HashMap<String, String> = As::<MapPreventDuplicates<Same, Same>>::deserialize(deserializer)?; |
You can use
Asto make theDeserializeAstypes compatible with the plainserde(with=...). You have to take care of all the actions theserde_asmacro does, especially replacing_withSame.So I think this should work here: