Skip to content

Commit 74e111c

Browse files
author
Jorge Aparicio
committed
impl Iterator for &mut Iterator and Box<Iterator>
closes #20953 closes #21361
1 parent 43f2c19 commit 74e111c

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

src/liballoc/boxed.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
1818
use core::default::Default;
1919
use core::fmt;
2020
use core::hash::{self, Hash};
21+
use core::iter::Iterator;
2122
use core::marker::Sized;
2223
use core::mem;
2324
use core::option::Option;
@@ -185,6 +186,16 @@ impl<T: ?Sized> DerefMut for Box<T> {
185186
fn deref_mut(&mut self) -> &mut T { &mut **self }
186187
}
187188

189+
// FIXME(#21363) remove `old_impl_check` when bug is fixed
190+
#[old_impl_check]
191+
impl<'a, T> Iterator for Box<Iterator<Item=T> + 'a> {
192+
type Item = T;
193+
194+
fn next(&mut self) -> Option<T> {
195+
(**self).next()
196+
}
197+
}
198+
188199
#[cfg(test)]
189200
mod test {
190201
#[test]

src/liballoc/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@
7070
#![feature(lang_items, unsafe_destructor)]
7171
#![feature(box_syntax)]
7272
#![feature(optin_builtin_traits)]
73+
// FIXME(#21363) remove `old_impl_check` when bug is fixed
74+
#![feature(old_impl_check)]
7375
#![allow(unknown_features)] #![feature(int_uint)]
7476

7577
#[macro_use]

src/libcore/iter.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ pub trait Iterator {
9999
fn size_hint(&self) -> (uint, Option<uint>) { (0, None) }
100100
}
101101

102+
// FIXME(#21363) remove `old_impl_check` when bug is fixed
103+
#[old_impl_check]
104+
impl<'a, T> Iterator for &'a mut (Iterator<Item=T> + 'a) {
105+
type Item = T;
106+
107+
fn next(&mut self) -> Option<T> {
108+
(**self).next()
109+
}
110+
}
111+
102112
/// Conversion from an `Iterator`
103113
#[stable]
104114
#[rustc_on_unimplemented="a collection of type `{Self}` cannot be \

src/libcore/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
#![feature(unboxed_closures)]
6464
#![allow(unknown_features)] #![feature(int_uint)]
6565
#![feature(on_unimplemented)]
66+
// FIXME(#21363) remove `old_impl_check` when bug is fixed
67+
#![feature(old_impl_check)]
6668
#![deny(missing_docs)]
6769

6870
#[macro_use]

src/test/run-pass/issue-20953.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let mut shrinker: Box<Iterator<Item=i32>> = Box::new(vec![1].into_iter());
13+
println!("{:?}", shrinker.next());
14+
for v in shrinker { assert!(false); }
15+
16+
let mut shrinker: &mut Iterator<Item=i32> = &mut vec![1].into_iter();
17+
println!("{:?}", shrinker.next());
18+
for v in shrinker { assert!(false); }
19+
}

src/test/run-pass/issue-21361.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let v = vec![1, 2, 3];
13+
let boxed: Box<Iterator<Item=i32>> = Box::new(v.into_iter());
14+
assert_eq!(boxed.max(), Some(3));
15+
16+
let v = vec![1, 2, 3];
17+
let boxed: &mut Iterator<Item=i32> = &mut v.into_iter();
18+
assert_eq!(boxed.max(), Some(3));
19+
}

0 commit comments

Comments
 (0)