about summary refs log tree commit diff
path: root/tvix/eval/src/value
diff options
context:
space:
mode:
authorAdam Joseph <adam@westernsemico.com>2022-11-23T07·23-0800
committerAdam Joseph <adam@westernsemico.com>2022-11-23T20·33+0000
commitff658006f070c421c6d0fdad3611f1029d17e671 (patch)
treeb58d6e665e1634fc26cc8636628de4bacb0ccb31 /tvix/eval/src/value
parentc06840ff87ad5d1a749c68496ec03b5e9f610926 (diff)
feat(tvix/eval): ExactSizeIterator for Iter<KeyValue<'a>> and Keys r/5305
Signed-off-by: Adam Joseph <adam@westernsemico.com>
Change-Id: Ia373eb30d8516a056f1349f9011dee9816593d6f
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7357
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/eval/src/value')
-rw-r--r--tvix/eval/src/value/attrs.rs20
1 files changed, 20 insertions, 0 deletions
diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs
index 6e839798d4ac..1593ab19d3aa 100644
--- a/tvix/eval/src/value/attrs.rs
+++ b/tvix/eval/src/value/attrs.rs
@@ -524,6 +524,16 @@ impl<'a> Iterator for Iter<KeyValue<'a>> {
     }
 }
 
+impl<'a> ExactSizeIterator for Iter<KeyValue<'a>> {
+    fn len(&self) -> usize {
+        match &self.0 {
+            KeyValue::Empty => 0,
+            KeyValue::KV { .. } => 2,
+            KeyValue::Map(inner) => inner.len(),
+        }
+    }
+}
+
 enum KeysInner<'a> {
     Empty,
     KV(IterKV),
@@ -562,6 +572,16 @@ impl<'a> IntoIterator for &'a NixAttrs {
     }
 }
 
+impl<'a> ExactSizeIterator for Keys<'a> {
+    fn len(&self) -> usize {
+        match &self.0 {
+            KeysInner::Empty => 0,
+            KeysInner::KV(_) => 2,
+            KeysInner::Map(m) => m.len(),
+        }
+    }
+}
+
 /// Internal representation of an owning attrset iterator
 pub enum IntoIterRepr {
     Empty,