@@ -277,6 +277,7 @@ pub use self::select::{Select, Handle};
277277use self :: select:: StartResult ;
278278use self :: select:: StartResult :: * ;
279279use self :: blocking:: SignalToken ;
280+ use core:: iter:: IntoIterator ;
280281
281282mod blocking;
282283mod oneshot;
@@ -306,6 +307,10 @@ pub struct Iter<'a, T: 'a> {
306307 rx : & ' a Receiver < T >
307308}
308309
310+ pub struct IntoIter < T > {
311+ rx : Receiver < T >
312+ }
313+
309314/// The sending-half of Rust's asynchronous channel type. This half can only be
310315/// owned by one task, but it can be cloned to send to other tasks.
311316#[ stable( feature = "rust1" , since = "1.0.0" ) ]
@@ -899,6 +904,20 @@ impl<'a, T> Iterator for Iter<'a, T> {
899904 fn next ( & mut self ) -> Option < T > { self . rx . recv ( ) . ok ( ) }
900905}
901906
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+
902921#[ unsafe_destructor]
903922#[ stable( feature = "rust1" , since = "1.0.0" ) ]
904923impl < T > Drop for Receiver < T > {
@@ -1507,6 +1526,22 @@ mod test {
15071526 assert_eq ! ( count_rx. recv( ) . unwrap( ) , 4 ) ;
15081527 }
15091528
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+
15101545 #[ test]
15111546 fn try_recv_states ( ) {
15121547 let ( tx1, rx1) = channel :: < i32 > ( ) ;
0 commit comments