Skip to content

Commit 43123a1

Browse files
committed
---
yaml --- r: 152955 b: refs/heads/try2 c: ab1bd3a h: refs/heads/master i: 152953: eab0c25 152951: 426eb9a v: v3
1 parent 483e298 commit 43123a1

File tree

3 files changed

+45
-21
lines changed

3 files changed

+45
-21
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 1ea9991921d2969517e445230997b8771d84bdb4
8+
refs/heads/try2: ab1bd3adf673ef7a515242a2dcc09ce360d41d9c
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcore/option.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -587,20 +587,32 @@ impl<A> ExactSize<A> for Item<A> {}
587587
/// ```
588588
#[inline]
589589
pub fn collect<T, Iter: Iterator<Option<T>>, V: FromIterator<T>>(iter: Iter) -> Option<V> {
590-
// FIXME(#11084): This should be twice as fast once this bug is closed.
591-
let mut iter = iter.scan(false, |state, x| {
592-
match x {
593-
Some(x) => Some(x),
594-
None => {
595-
*state = true;
596-
None
590+
// FIXME(#11084): This could be replaced with Iterator::scan when this
591+
// performance bug is closed.
592+
593+
struct Adapter<Iter> {
594+
iter: Iter,
595+
found_none: bool,
596+
}
597+
598+
impl<T, Iter: Iterator<Option<T>>> Iterator<T> for Adapter<Iter> {
599+
#[inline]
600+
fn next(&mut self) -> Option<T> {
601+
match self.iter.next() {
602+
Some(Some(value)) => Some(value),
603+
Some(None) => {
604+
self.found_none = true;
605+
None
606+
}
607+
None => None,
597608
}
598609
}
599-
});
610+
}
600611

601-
let v: V = FromIterator::from_iter(iter.by_ref());
612+
let mut adapter = Adapter { iter: iter, found_none: false };
613+
let v: V = FromIterator::from_iter(adapter.by_ref());
602614

603-
if iter.state {
615+
if adapter.found_none {
604616
None
605617
} else {
606618
Some(v)

branches/try2/src/libcore/result.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -585,20 +585,32 @@ impl<T: Show, E> Result<T, E> {
585585
/// ```
586586
#[inline]
587587
pub fn collect<T, E, Iter: Iterator<Result<T, E>>, V: FromIterator<T>>(iter: Iter) -> Result<V, E> {
588-
// FIXME(#11084): This should be twice as fast once this bug is closed.
589-
let mut iter = iter.scan(None, |state, x| {
590-
match x {
591-
Ok(x) => Some(x),
592-
Err(err) => {
593-
*state = Some(err);
594-
None
588+
// FIXME(#11084): This could be replaced with Iterator::scan when this
589+
// performance bug is closed.
590+
591+
struct Adapter<Iter, E> {
592+
iter: Iter,
593+
err: Option<E>,
594+
}
595+
596+
impl<T, E, Iter: Iterator<Result<T, E>>> Iterator<T> for Adapter<Iter, E> {
597+
#[inline]
598+
fn next(&mut self) -> Option<T> {
599+
match self.iter.next() {
600+
Some(Ok(value)) => Some(value),
601+
Some(Err(err)) => {
602+
self.err = Some(err);
603+
None
604+
}
605+
None => None,
595606
}
596607
}
597-
});
608+
}
598609

599-
let v: V = FromIterator::from_iter(iter.by_ref());
610+
let mut adapter = Adapter { iter: iter, err: None };
611+
let v: V = FromIterator::from_iter(adapter.by_ref());
600612

601-
match iter.state {
613+
match adapter.err {
602614
Some(err) => Err(err),
603615
None => Ok(v),
604616
}

0 commit comments

Comments
 (0)