@@ -277,6 +277,7 @@ pub use self::select::{Select, Handle};
277
277
use self :: select:: StartResult ;
278
278
use self :: select:: StartResult :: * ;
279
279
use self :: blocking:: SignalToken ;
280
+ use core:: iter:: IntoIterator ;
280
281
281
282
mod blocking;
282
283
mod oneshot;
@@ -306,6 +307,10 @@ pub struct Iter<'a, T: 'a> {
306
307
rx : & ' a Receiver < T >
307
308
}
308
309
310
+ pub struct IntoIter < T > {
311
+ rx : Receiver < T >
312
+ }
313
+
309
314
/// The sending-half of Rust's asynchronous channel type. This half can only be
310
315
/// owned by one task, but it can be cloned to send to other tasks.
311
316
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -899,6 +904,20 @@ impl<'a, T> Iterator for Iter<'a, T> {
899
904
fn next ( & mut self ) -> Option < T > { self . rx . recv ( ) . ok ( ) }
900
905
}
901
906
907
+ impl < T > Iterator for IntoIter < T > {
908
+ type Item = T ;
909
+ fn next ( & mut self ) -> Option < T > { self . rx . recv ( ) . ok ( ) }
910
+ }
911
+
912
+ impl < T > IntoIterator for Receiver < T > {
913
+ type Item = T ;
914
+ type IntoIter = IntoIter < T > ;
915
+
916
+ fn into_iter ( self ) -> IntoIter < T > {
917
+ IntoIter { rx : self }
918
+ }
919
+ }
920
+
902
921
#[ unsafe_destructor]
903
922
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
904
923
impl < T > Drop for Receiver < T > {
@@ -1507,6 +1526,22 @@ mod test {
1507
1526
assert_eq ! ( count_rx. recv( ) . unwrap( ) , 4 ) ;
1508
1527
}
1509
1528
1529
+ #[ test]
1530
+ fn test_recv_into_iter ( ) {
1531
+ use core:: iter:: IntoIterator ;
1532
+
1533
+ let mut iter = {
1534
+ let ( tx, rx) = channel :: < i32 > ( ) ;
1535
+ tx. send ( 1 ) . unwrap ( ) ;
1536
+ tx. send ( 2 ) . unwrap ( ) ;
1537
+
1538
+ rx. into_iter ( )
1539
+ } ;
1540
+ assert_eq ! ( iter. next( ) . unwrap( ) , 1 ) ;
1541
+ assert_eq ! ( iter. next( ) . unwrap( ) , 2 ) ;
1542
+ assert_eq ! ( iter. next( ) . is_none( ) , true ) ;
1543
+ }
1544
+
1510
1545
#[ test]
1511
1546
fn try_recv_states ( ) {
1512
1547
let ( tx1, rx1) = channel :: < i32 > ( ) ;
0 commit comments