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 <flokli@flokli.de>
Autosubmit: tazjin <tazjin@tvl.su>
This commit is contained in:
Vincent Ambo 2023-11-05 20:43:56 +03:00 committed by clbot
parent 327548d2fd
commit 2e1399698b

View file

@ -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,