auto merge of #8884 : blake2-ppc/rust/exact-size-hint, r=huonw

The message of the first commit explains (edited for changed trait name):

The trait `ExactSize` is introduced to solve a few small niggles:

* We can't reverse (`.invert()`) an enumeration iterator
* for a vector, we have `v.iter().position(f)` but `v.rposition(f)`.
* We can't reverse `Zip` even if both iterators are from vectors

`ExactSize` is an empty trait that is intended to indicate that an
iterator, for example `VecIterator`, knows its exact finite size and
reports it correctly using `.size_hint()`. Only adaptors that preserve
this at all times, can expose this trait further. (Where here we say
finite for fitting in uint).

---

It may seem complicated just to solve these small "niggles",
(It's really the reversible enumerate case that's the most interesting)
but only a few core iterators need to implement this trait.

While we gain more capabilities generically for some iterators,
it becomes a tad more complicated to figure out if a type has
the right trait impls for it.
This commit is contained in:
bors
2013-09-03 06:56:05 -07:00
10 changed files with 160 additions and 63 deletions

View File

@@ -24,7 +24,7 @@ use container::{Container, Mutable};
use num::Times;
use iterator::{Iterator, FromIterator, Extendable};
use iterator::{Filter, AdditiveIterator, Map};
use iterator::{Invert, DoubleEndedIterator};
use iterator::{Invert, DoubleEndedIterator, ExactSize};
use libc;
use num::{Saturating};
use option::{None, Option, Some};
@@ -484,9 +484,8 @@ for CharSplitIterator<'self, Sep> {
let mut next_split = None;
if self.only_ascii {
for (j, byte) in self.string.byte_rev_iter().enumerate() {
for (idx, byte) in self.string.byte_iter().enumerate().invert() {
if self.sep.matches(byte as char) && byte < 128u8 {
let idx = len - j - 1;
next_split = Some((idx, idx + 1));
break;
}
@@ -2006,16 +2005,13 @@ impl<'self> StrSlice<'self> for &'self str {
/// or `None` if there is no match
fn find<C: CharEq>(&self, search: C) -> Option<uint> {
if search.only_ascii() {
for (i, b) in self.byte_iter().enumerate() {
if search.matches(b as char) { return Some(i) }
}
self.byte_iter().position(|b| search.matches(b as char))
} else {
for (index, c) in self.char_offset_iter() {
if search.matches(c) { return Some(index); }
}
None
}
None
}
/// Returns the byte index of the last character of `self` that matches `search`
@@ -2026,18 +2022,13 @@ impl<'self> StrSlice<'self> for &'self str {
/// or `None` if there is no match
fn rfind<C: CharEq>(&self, search: C) -> Option<uint> {
if search.only_ascii() {
let mut index = self.len();
for b in self.byte_rev_iter() {
index -= 1;
if search.matches(b as char) { return Some(index); }
}
self.byte_iter().rposition(|b| search.matches(b as char))
} else {
for (index, c) in self.char_offset_rev_iter() {
if search.matches(c) { return Some(index); }
}
None
}
None
}
/// Returns the byte index of the first matching substring