Skip to content

override VecDeque's Iter::try_fold #57974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/liballoc/collections/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::fmt;
use core::iter::{repeat_with, FromIterator, FusedIterator};
use core::mem;
use core::ops::Bound::{Excluded, Included, Unbounded};
use core::ops::{Index, IndexMut, RangeBounds};
use core::ops::{Index, IndexMut, RangeBounds, Try};
use core::ptr;
use core::ptr::NonNull;
use core::slice;
Expand Down Expand Up @@ -2172,6 +2172,14 @@ impl<'a, T> Iterator for Iter<'a, T> {
accum = front.iter().fold(accum, &mut f);
back.iter().fold(accum, &mut f)
}

fn try_fold<B, F, R>(&mut self, init: B, mut f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
let accum = front.iter().try_fold(init, &mut f)?;
back.iter().try_fold(accum, &mut f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about this -- if it short-circuits it's not going to resume from the correct place, since it's try_folding on slice iterators and not updating the state of the self iterator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see a test like this one:

rust/src/libcore/tests/iter.rs

Lines 1990 to 1993 in 7164a9f

let a = [10, 20, 30, 40, 100, 60, 70, 80, 90];
let mut iter = a.iter().rev();
assert_eq!(iter.try_fold(0_i8, |acc, &x| acc.checked_add(x)), None);
assert_eq!(iter.next(), Some(&70));

}
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
#![feature(slice_partition_dedup)]
#![feature(maybe_uninit)]
#![feature(alloc_layout_extra)]
#![feature(try_trait)]

// Allow testing this library

Expand Down
37 changes: 37 additions & 0 deletions src/liballoc/tests/vec_deque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,3 +1433,40 @@ fn test_rotate_right_random() {
}
}
}

#[test]
fn test_try_fold_empty() {
assert_eq!(Some(0), VecDeque::<u32>::new().iter().try_fold(0, |_, _| None));
}

#[test]
fn test_try_fold_none() {
let v: VecDeque<u32> = (0..12).collect();
assert_eq!(None, v.into_iter().try_fold(0, |a, b|
if b < 11 { Some(a + b) } else { None }));
}

#[test]
fn test_try_fold_ok() {
let v: VecDeque<u32> = (0..12).collect();
assert_eq!(Ok::<_, ()>(66), v.into_iter().try_fold(0, |a, b| Ok(a + b)));
}

#[test]
fn test_try_fold_unit() {
let v: VecDeque<()> = std::iter::repeat(()).take(42).collect();
assert_eq!(Some(()), v.into_iter().try_fold((), |(), ()| Some(())));
}

#[test]
fn test_try_fold_rotated() {
let mut v: VecDeque<_> = (0..12).collect();
for n in 0..10 {
if n & 1 == 0 {
v.rotate_left(n);
} else {
v.rotate_right(n);
}
assert_eq!(Ok::<_, ()>(66), v.iter().try_fold(0, |a, b| Ok(a + b)));
}
}