From 50313b89c194b42726518351f57e00498028284d Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Mon, 22 Apr 2024 18:03:55 -0400 Subject: [PATCH] Add `Record::drain` implementation --- crates/nu-protocol/src/value/record.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/crates/nu-protocol/src/value/record.rs b/crates/nu-protocol/src/value/record.rs index 8e372fd480..a204e5d78b 100644 --- a/crates/nu-protocol/src/value/record.rs +++ b/crates/nu-protocol/src/value/record.rs @@ -260,10 +260,9 @@ impl Record { where R: RangeBounds + Clone, { - todo!() - // Drain { - // iter: self.inner.drain(range), - // } + Drain { + iter: self.inner[(range.start_bound().cloned(), range.end_bound().cloned())].iter(), + } } /// Sort the record by its columns. @@ -630,14 +629,16 @@ impl ExactSizeIterator for IntoValues { impl FusedIterator for IntoValues {} pub struct Drain<'a> { - iter: std::vec::Drain<'a, (String, Value)>, + iter: std::slice::Iter<'a, (String, Value)>, } impl Iterator for Drain<'_> { type Item = (String, Value); fn next(&mut self) -> Option { - self.iter.next() + self.iter + .next() + .map(|(col, val)| (col.clone(), val.clone())) } fn size_hint(&self) -> (usize, Option) { @@ -647,7 +648,9 @@ impl Iterator for Drain<'_> { impl DoubleEndedIterator for Drain<'_> { fn next_back(&mut self) -> Option { - self.iter.next_back() + self.iter + .next_back() + .map(|(col, val)| (col.clone(), val.clone())) } }