From 2e1399698b366af4a1579cdcbd9363633b8bc632 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 5 Nov 2023 20:43:56 +0300 Subject: [PATCH] refactor(tvix/eval): use IntoIterator trait for owned NixAttrs iter 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 Autosubmit: tazjin --- tvix/eval/src/value/attrs.rs | 37 +++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/tvix/eval/src/value/attrs.rs b/tvix/eval/src/value/attrs.rs index 9e891135c..10bdee4eb 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 { @@ -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,