feat(tvix/eval): impl FromIterator for NixAttrs

Allows for the removal of some BTreeMap usage when constructing NixAttrs
by allowing any iterator over 2-tuples to build a NixAttrs. Some
instances of BTreeMap didn't have anything to do with making NixAttrs,
and some were just the best tool for the job, so they are left using the
old `from_map` interface.

Change-Id: I668ea600b0d93eae700a6b1861ac84502c968d78
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7492
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
Lyle Mantooth 2022-11-28 09:48:29 -05:00 committed by IslandUsurper
parent 90fe9a51d7
commit 39dba6aa16
2 changed files with 64 additions and 52 deletions

View file

@ -7,6 +7,7 @@
//! some peculiarities that are encapsulated within this module.
use std::collections::btree_map;
use std::collections::BTreeMap;
use std::iter::FromIterator;
use crate::errors::ErrorKind;
use crate::vm::VM;
@ -88,6 +89,23 @@ impl AttrsRep {
#[derive(Clone, Debug, Default)]
pub struct NixAttrs(AttrsRep);
impl<K, V> FromIterator<(K, V)> for NixAttrs
where
NixString: From<K>,
Value: From<V>,
{
fn from_iter<T>(iter: T) -> NixAttrs
where
T: IntoIterator<Item = (K, V)>,
{
NixAttrs(AttrsRep::Map(
iter.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect(),
))
}
}
impl TotalDisplay for NixAttrs {
fn total_fmt(&self, f: &mut std::fmt::Formatter<'_>, set: &mut ThunkSet) -> std::fmt::Result {
f.write_str("{ ")?;