refactor(tvix/nix-compat): check presence with btree_map's entry API
Walking a btree_map twice is more expensive than copying a string, especially because the cloning only happens in the (non-hot) error path. This fixes a clippy lint, so it's related to b/321. Change-Id: I2ccfd0bc46792a45d277f47564e595b87107d8be Reviewed-on: https://cl.tvl.fyi/c/depot/+/9962 Autosubmit: tazjin <tazjin@tvl.su> Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
This commit is contained in:
parent
b3b1f649d6
commit
327548d2fd
1 changed files with 9 additions and 8 deletions
|
@ -9,7 +9,7 @@ use nom::character::complete::char as nomchar;
|
||||||
use nom::combinator::{all_consuming, map_res};
|
use nom::combinator::{all_consuming, map_res};
|
||||||
use nom::multi::{separated_list0, separated_list1};
|
use nom::multi::{separated_list0, separated_list1};
|
||||||
use nom::sequence::{delimited, preceded, separated_pair, terminated, tuple};
|
use nom::sequence::{delimited, preceded, separated_pair, terminated, tuple};
|
||||||
use std::collections::{BTreeMap, BTreeSet};
|
use std::collections::{btree_map, BTreeMap, BTreeSet};
|
||||||
use thiserror;
|
use thiserror;
|
||||||
|
|
||||||
use crate::derivation::parse_error::{into_nomerror, ErrorKind, NomError, NomResult};
|
use crate::derivation::parse_error::{into_nomerror, ErrorKind, NomError, NomResult};
|
||||||
|
@ -274,13 +274,14 @@ where
|
||||||
for (k, v) in pairs.into_iter() {
|
for (k, v) in pairs.into_iter() {
|
||||||
// collect the 2-tuple to a BTreeMap,
|
// collect the 2-tuple to a BTreeMap,
|
||||||
// and fail if the key was already seen before.
|
// and fail if the key was already seen before.
|
||||||
if kvs.contains_key(&k) {
|
match kvs.entry(k) {
|
||||||
return Err(nom::Err::Failure(NomError {
|
btree_map::Entry::Vacant(e) => { e.insert(v); },
|
||||||
input: i,
|
btree_map::Entry::Occupied(e) => {
|
||||||
code: ErrorKind::DuplicateMapKey(k),
|
return Err(nom::Err::Failure(NomError {
|
||||||
}));
|
input: i,
|
||||||
} else {
|
code: ErrorKind::DuplicateMapKey(e.key().clone()),
|
||||||
kvs.insert(k, v);
|
}));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok((rest, kvs))
|
Ok((rest, kvs))
|
||||||
|
|
Loading…
Add table
Reference in a new issue