Skip to content
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

implement missing iterator traits for slice::Windows #22166

Merged
merged 1 commit into from
Feb 12, 2015
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
12 changes: 12 additions & 0 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,18 @@ mod tests {
let wins: &[&[_]] = &[&[1,2,3], &[2,3,4]];
assert_eq!(v.windows(3).collect::<Vec<_>>(), wins);
assert!(v.windows(6).next().is_none());

let wins: &[&[_]] = &[&[3,4], &[2,3], &[1,2]];
assert_eq!(v.windows(2).rev().collect::<Vec<&[_]>>(), wins);
let mut it = v.windows(2);
assert_eq!(it.indexable(), 3);
let win: &[_] = &[1,2];
assert_eq!(it.idx(0).unwrap(), win);
let win: &[_] = &[2,3];
assert_eq!(it.idx(1).unwrap(), win);
let win: &[_] = &[3,4];
assert_eq!(it.idx(2).unwrap(), win);
assert_eq!(it.idx(3), None);
}

#[test]
Expand Down
38 changes: 36 additions & 2 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,8 +1179,42 @@ impl<'a, T> Iterator for Windows<'a, T> {
if self.size > self.v.len() {
(0, Some(0))
} else {
let x = self.v.len() - self.size;
(x.saturating_add(1), x.checked_add(1))
let size = self.v.len() - self.size + 1;
(size, Some(size))
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> DoubleEndedIterator for Windows<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<&'a [T]> {
if self.size > self.v.len() {
None
} else {
let ret = Some(&self.v[self.v.len()-self.size..]);
self.v = &self.v[..self.v.len()-1];
ret
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> ExactSizeIterator for Windows<'a, T> {}
Copy link
Member

Choose a reason for hiding this comment

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

This looks like it may not actually be able to be implemented due to this line in size_hint:

(x.saturating_add(1), x.checked_add(1))

If x were uint::MAX then this would return (uint::MAX, None), breaking the contract of ExactSizeIterator. The value of x, however is self.v.len() - self.size and we know that size != 0 so we're guaranteed that the checked_add will always return Some.

Could you update the above size_hint to return (x + 1, Some(x + 1)) to be clear that it is a precise size?


#[unstable(feature = "core", reason = "trait is experimental")]
impl<'a, T> RandomAccessIterator for Windows<'a, T> {
#[inline]
fn indexable(&self) -> uint {
self.size_hint().0
}

#[inline]
fn idx(&mut self, index: uint) -> Option<&'a [T]> {
if index + self.size > self.v.len() {
None
} else {
Some(&self.v[index .. index+self.size])
}
}
}
Expand Down