From ff658006f070c421c6d0fdad3611f1029d17e671 Mon Sep 17 00:00:00 2001 From: Adam Joseph Date: Tue, 22 Nov 2022 23:23:28 -0800 Subject: feat(tvix/eval): ExactSizeIterator for Iter> and Keys Signed-off-by: Adam Joseph Change-Id: Ia373eb30d8516a056f1349f9011dee9816593d6f Reviewed-on: https://cl.tvl.fyi/c/depot/+/7357 Tested-by: BuildkiteCI Reviewed-by: tazjin --- tvix/eval/src/value/attrs.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'tvix/eval') 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> { } } +impl<'a> ExactSizeIterator for Iter> { + 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, -- cgit 1.4.1