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:
Vincent Ambo 2023-11-05 20:36:58 +03:00 committed by clbot
parent b3b1f649d6
commit 327548d2fd

View file

@ -9,7 +9,7 @@ use nom::character::complete::char as nomchar;
use nom::combinator::{all_consuming, map_res};
use nom::multi::{separated_list0, separated_list1};
use nom::sequence::{delimited, preceded, separated_pair, terminated, tuple};
use std::collections::{BTreeMap, BTreeSet};
use std::collections::{btree_map, BTreeMap, BTreeSet};
use thiserror;
use crate::derivation::parse_error::{into_nomerror, ErrorKind, NomError, NomResult};
@ -274,13 +274,14 @@ where
for (k, v) in pairs.into_iter() {
// collect the 2-tuple to a BTreeMap,
// and fail if the key was already seen before.
if kvs.contains_key(&k) {
return Err(nom::Err::Failure(NomError {
input: i,
code: ErrorKind::DuplicateMapKey(k),
}));
} else {
kvs.insert(k, v);
match kvs.entry(k) {
btree_map::Entry::Vacant(e) => { e.insert(v); },
btree_map::Entry::Occupied(e) => {
return Err(nom::Err::Failure(NomError {
input: i,
code: ErrorKind::DuplicateMapKey(e.key().clone()),
}));
}
}
}
Ok((rest, kvs))