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

* Use entry api to remove a lookup in a loop

* cargo fmt
master
Squirrel 2021-08-10 17:25:38 +01:00 committed by GitHub
parent c2a069f5de
commit d8a9a98cbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 4 deletions

View File

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