From 39dba6aa1624b78690ade7f1b657283b48b56b39 Mon Sep 17 00:00:00 2001 From: Lyle Mantooth Date: Mon, 28 Nov 2022 09:48:29 -0500 Subject: 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 --- tvix/eval/src/value/attrs.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'tvix/eval/src/value') diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs index fced32a865a6..0ef28f068cf2 100644 --- a/tvix/eval/src/value/attrs.rs +++ b/tvix/eval/src/value/attrs.rs @@ -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 FromIterator<(K, V)> for NixAttrs +where + NixString: From, + Value: From, +{ + fn from_iter(iter: T) -> NixAttrs + where + T: IntoIterator, + { + 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("{ ")?; -- cgit 1.4.1