chore(tvix/eval): minor readability improvement in attrs

Change-Id: If9d9eaf60934e96ec4b41c57818afe0c2a99c862
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6206
Reviewed-by: grfn <grfn@gws.fyi>
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2022-08-14 03:06:11 +03:00 committed by tazjin
parent de26894814
commit 11ea7b82d8

View file

@ -5,6 +5,7 @@
/// ///
/// Due to this, construction and management of attribute sets has /// Due to this, construction and management of attribute sets has
/// some peculiarities that are encapsulated within this module. /// some peculiarities that are encapsulated within this module.
use std::collections::btree_map;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::fmt::Display; use std::fmt::Display;
use std::rc::Rc; use std::rc::Rc;
@ -168,23 +169,19 @@ impl NixAttrs {
let mut m = m.clone(); let mut m = m.clone();
match m.entry(NixString::NAME) { match m.entry(NixString::NAME) {
std::collections::btree_map::Entry::Vacant(e) => { btree_map::Entry::Vacant(e) => {
e.insert(name.clone()); e.insert(name.clone());
} }
std::collections::btree_map::Entry::Occupied(_) => { btree_map::Entry::Occupied(_) => { /* name from `m` has precedence */ }
/* name from `m` has precedence */
}
}; };
match m.entry(NixString::VALUE) { match m.entry(NixString::VALUE) {
std::collections::btree_map::Entry::Vacant(e) => { btree_map::Entry::Vacant(e) => {
e.insert(value.clone()); e.insert(value.clone());
} }
std::collections::btree_map::Entry::Occupied(_) => { btree_map::Entry::Occupied(_) => { /* value from `m` has precedence */ }
/* value from `m` has precedence */
}
}; };
NixAttrs(AttrsRep::Map(m)) NixAttrs(AttrsRep::Map(m))
@ -307,11 +304,11 @@ fn attempt_optimise_kv(slice: &mut [Value]) -> Option<NixAttrs> {
// checking against duplicate keys. // checking against duplicate keys.
fn set_attr(attrs: &mut NixAttrs, key: NixString, value: Value) -> EvalResult<()> { fn set_attr(attrs: &mut NixAttrs, key: NixString, value: Value) -> EvalResult<()> {
match attrs.0.map_mut().entry(key) { match attrs.0.map_mut().entry(key) {
std::collections::btree_map::Entry::Occupied(entry) => Err(Error::DuplicateAttrsKey { btree_map::Entry::Occupied(entry) => Err(Error::DuplicateAttrsKey {
key: entry.key().as_str().to_string(), key: entry.key().as_str().to_string(),
}), }),
std::collections::btree_map::Entry::Vacant(entry) => { btree_map::Entry::Vacant(entry) => {
entry.insert(value); entry.insert(value);
Ok(()) Ok(())
} }
@ -344,7 +341,7 @@ fn set_nested_attr(
// duplicate key. // duplicate key.
match attrs.0.map_mut().entry(key) { match attrs.0.map_mut().entry(key) {
// Vacant entry -> new attribute set is needed. // Vacant entry -> new attribute set is needed.
std::collections::btree_map::Entry::Vacant(entry) => { btree_map::Entry::Vacant(entry) => {
let mut map = NixAttrs(AttrsRep::Map(BTreeMap::new())); let mut map = NixAttrs(AttrsRep::Map(BTreeMap::new()));
// TODO(tazjin): technically recursing further is not // TODO(tazjin): technically recursing further is not
@ -357,7 +354,7 @@ fn set_nested_attr(
// Occupied entry: Either error out if there is something // Occupied entry: Either error out if there is something
// other than attrs, or insert the next value. // other than attrs, or insert the next value.
std::collections::btree_map::Entry::Occupied(mut entry) => match entry.get_mut() { btree_map::Entry::Occupied(mut entry) => match entry.get_mut() {
Value::Attrs(attrs) => { Value::Attrs(attrs) => {
set_nested_attr( set_nested_attr(
Rc::make_mut(attrs), Rc::make_mut(attrs),