about summary refs log tree commit diff
path: root/tvix/eval/src/value/attrs.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-11-05T17·43+0300
committerclbot <clbot@tvl.fyi>2023-11-05T20·28+0000
commit2e1399698b366af4a1579cdcbd9363633b8bc632 (patch)
treeb0f0303fd4ded0ae4a2d408d0600fc17d43f42ed /tvix/eval/src/value/attrs.rs
parent327548d2fdefddc211d15302a8108999f352e685 (diff)
refactor(tvix/eval): use IntoIterator trait for owned NixAttrs iter r/6957
Uses the standard library IntoIterator trait for the construction of
our iterators. Clippy complains about duplicating this.

While doing this, I opted to rename the `IntoIter` type into something
that is more useful to users, in case somebody ends up working with
these manually.

This fixes a clippy lint, and is related to b/321.

Change-Id: I851fde0d7b8b38d182343a0fd6d9f8dd2a33ee11
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9963
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Autosubmit: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/eval/src/value/attrs.rs')
-rw-r--r--tvix/eval/src/value/attrs.rs37
1 files changed, 22 insertions, 15 deletions
diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs
index 9e891135c9c2..10bdee4eba3a 100644
--- a/tvix/eval/src/value/attrs.rs
+++ b/tvix/eval/src/value/attrs.rs
@@ -305,19 +305,9 @@ impl NixAttrs {
         self.iter()
     }
 
-    pub fn into_iter(self) -> IntoIter {
-        match self.0 {
-            AttrsRep::Empty => IntoIter(IntoIterRepr::Empty),
-            AttrsRep::KV { name, value } => IntoIter(IntoIterRepr::Finite(
-                vec![(NAME_REF.clone(), name), (VALUE_REF.clone(), value)].into_iter(),
-            )),
-            AttrsRep::Im(map) => IntoIter(IntoIterRepr::Im(map.into_iter())),
-        }
-    }
-
-    /// Same as into_iter(), but marks call sites which rely on the
+    /// Same as [IntoIterator::into_iter], but marks call sites which rely on the
     /// iteration being lexicographic.
-    pub fn into_iter_sorted(self) -> IntoIter {
+    pub fn into_iter_sorted(self) -> OwnedAttrsIterator {
         self.into_iter()
     }
 
@@ -406,6 +396,21 @@ impl NixAttrs {
     }
 }
 
+impl IntoIterator for NixAttrs {
+    type Item = (NixString, Value);
+    type IntoIter = OwnedAttrsIterator;
+
+    fn into_iter(self) -> Self::IntoIter {
+        match self.0 {
+            AttrsRep::Empty => OwnedAttrsIterator(IntoIterRepr::Empty),
+            AttrsRep::KV { name, value } => OwnedAttrsIterator(IntoIterRepr::Finite(
+                vec![(NAME_REF.clone(), name), (VALUE_REF.clone(), value)].into_iter(),
+            )),
+            AttrsRep::Im(map) => OwnedAttrsIterator(IntoIterRepr::Im(map.into_iter())),
+        }
+    }
+}
+
 /// In Nix, name/value attribute pairs are frequently constructed from
 /// literals. This particular case should avoid allocation of a map,
 /// additional heap values etc. and use the optimised `KV` variant
@@ -584,10 +589,12 @@ pub enum IntoIterRepr {
     Im(imbl::ordmap::ConsumingIter<(NixString, Value)>),
 }
 
+/// Wrapper type which hides the internal implementation details from
+/// users.
 #[repr(transparent)]
-pub struct IntoIter(IntoIterRepr);
+pub struct OwnedAttrsIterator(IntoIterRepr);
 
-impl Iterator for IntoIter {
+impl Iterator for OwnedAttrsIterator {
     type Item = (NixString, Value);
 
     fn next(&mut self) -> Option<Self::Item> {
@@ -599,7 +606,7 @@ impl Iterator for IntoIter {
     }
 }
 
-impl ExactSizeIterator for IntoIter {
+impl ExactSizeIterator for OwnedAttrsIterator {
     fn len(&self) -> usize {
         match &self.0 {
             IntoIterRepr::Empty => 0,