Use entry api to remove a lookup in deserialisation loop (#438)

* Use entry api to remove a lookup in a loop

* cargo fmt
This commit is contained in:
Squirrel 2021-08-10 17:25:38 +01:00 committed by GitHub
parent ed3309f0e4
commit 55aefbd137

View file

@ -15,7 +15,7 @@ use serde::ser;
use crate::datetime::{self, DatetimeFromString}; use crate::datetime::{self, DatetimeFromString};
pub use crate::datetime::{Datetime, DatetimeParseError}; pub use crate::datetime::{Datetime, DatetimeParseError};
pub use crate::map::Map; pub use crate::map::{Entry, Map};
/// Representation of a TOML value. /// Representation of a TOML value.
#[derive(PartialEq, Clone, Debug)] #[derive(PartialEq, Clone, Debug)]
@ -526,12 +526,13 @@ impl<'de> de::Deserialize<'de> for Value {
} }
let mut map = Map::new(); let mut map = Map::new();
map.insert(key, visitor.next_value()?); map.insert(key, visitor.next_value()?);
while let Some(key) = visitor.next_key()? { while let Some(key) = visitor.next_key::<String>()? {
if map.contains_key(&key) { if let Entry::Vacant(vacant) = map.entry(&key) {
vacant.insert(visitor.next_value()?);
} else {
let msg = format!("duplicate key: `{}`", key); let msg = format!("duplicate key: `{}`", key);
return Err(de::Error::custom(msg)); return Err(de::Error::custom(msg));
} }
map.insert(key, visitor.next_value()?);
} }
Ok(Value::Table(map)) Ok(Value::Table(map))
} }