@@ -22,6 +22,11 @@ use crate::collections::TryReserveError;
2222use crate :: raw_vec:: RawVec ;
2323use crate :: vec:: Vec ;
2424
25+ #[ stable( feature = "drain" , since = "1.6.0" ) ]
26+ pub use self :: drain:: Drain ;
27+
28+ mod drain;
29+
2530#[ cfg( test) ]
2631mod tests;
2732
@@ -2541,127 +2546,6 @@ impl<T> ExactSizeIterator for IntoIter<T> {
25412546#[ stable( feature = "fused" , since = "1.26.0" ) ]
25422547impl < T > FusedIterator for IntoIter < T > { }
25432548
2544- /// A draining iterator over the elements of a `VecDeque`.
2545- ///
2546- /// This `struct` is created by the [`drain`] method on [`VecDeque`]. See its
2547- /// documentation for more.
2548- ///
2549- /// [`drain`]: struct.VecDeque.html#method.drain
2550- /// [`VecDeque`]: struct.VecDeque.html
2551- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2552- pub struct Drain < ' a , T : ' a > {
2553- after_tail : usize ,
2554- after_head : usize ,
2555- iter : Iter < ' a , T > ,
2556- deque : NonNull < VecDeque < T > > ,
2557- }
2558-
2559- #[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
2560- impl < T : fmt:: Debug > fmt:: Debug for Drain < ' _ , T > {
2561- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2562- f. debug_tuple ( "Drain" )
2563- . field ( & self . after_tail )
2564- . field ( & self . after_head )
2565- . field ( & self . iter )
2566- . finish ( )
2567- }
2568- }
2569-
2570- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2571- unsafe impl < T : Sync > Sync for Drain < ' _ , T > { }
2572- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2573- unsafe impl < T : Send > Send for Drain < ' _ , T > { }
2574-
2575- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2576- impl < T > Drop for Drain < ' _ , T > {
2577- fn drop ( & mut self ) {
2578- struct DropGuard < ' r , ' a , T > ( & ' r mut Drain < ' a , T > ) ;
2579-
2580- impl < ' r , ' a , T > Drop for DropGuard < ' r , ' a , T > {
2581- fn drop ( & mut self ) {
2582- self . 0 . for_each ( drop) ;
2583-
2584- let source_deque = unsafe { self . 0 . deque . as_mut ( ) } ;
2585-
2586- // T = source_deque_tail; H = source_deque_head; t = drain_tail; h = drain_head
2587- //
2588- // T t h H
2589- // [. . . o o x x o o . . .]
2590- //
2591- let orig_tail = source_deque. tail ;
2592- let drain_tail = source_deque. head ;
2593- let drain_head = self . 0 . after_tail ;
2594- let orig_head = self . 0 . after_head ;
2595-
2596- let tail_len = count ( orig_tail, drain_tail, source_deque. cap ( ) ) ;
2597- let head_len = count ( drain_head, orig_head, source_deque. cap ( ) ) ;
2598-
2599- // Restore the original head value
2600- source_deque. head = orig_head;
2601-
2602- match ( tail_len, head_len) {
2603- ( 0 , 0 ) => {
2604- source_deque. head = 0 ;
2605- source_deque. tail = 0 ;
2606- }
2607- ( 0 , _) => {
2608- source_deque. tail = drain_head;
2609- }
2610- ( _, 0 ) => {
2611- source_deque. head = drain_tail;
2612- }
2613- _ => unsafe {
2614- if tail_len <= head_len {
2615- source_deque. tail = source_deque. wrap_sub ( drain_head, tail_len) ;
2616- source_deque. wrap_copy ( source_deque. tail , orig_tail, tail_len) ;
2617- } else {
2618- source_deque. head = source_deque. wrap_add ( drain_tail, head_len) ;
2619- source_deque. wrap_copy ( drain_tail, drain_head, head_len) ;
2620- }
2621- } ,
2622- }
2623- }
2624- }
2625-
2626- while let Some ( item) = self . next ( ) {
2627- let guard = DropGuard ( self ) ;
2628- drop ( item) ;
2629- mem:: forget ( guard) ;
2630- }
2631-
2632- DropGuard ( self ) ;
2633- }
2634- }
2635-
2636- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2637- impl < T > Iterator for Drain < ' _ , T > {
2638- type Item = T ;
2639-
2640- #[ inline]
2641- fn next ( & mut self ) -> Option < T > {
2642- self . iter . next ( ) . map ( |elt| unsafe { ptr:: read ( elt) } )
2643- }
2644-
2645- #[ inline]
2646- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2647- self . iter . size_hint ( )
2648- }
2649- }
2650-
2651- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2652- impl < T > DoubleEndedIterator for Drain < ' _ , T > {
2653- #[ inline]
2654- fn next_back ( & mut self ) -> Option < T > {
2655- self . iter . next_back ( ) . map ( |elt| unsafe { ptr:: read ( elt) } )
2656- }
2657- }
2658-
2659- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2660- impl < T > ExactSizeIterator for Drain < ' _ , T > { }
2661-
2662- #[ stable( feature = "fused" , since = "1.26.0" ) ]
2663- impl < T > FusedIterator for Drain < ' _ , T > { }
2664-
26652549#[ stable( feature = "rust1" , since = "1.0.0" ) ]
26662550impl < A : PartialEq > PartialEq for VecDeque < A > {
26672551 fn eq ( & self , other : & VecDeque < A > ) -> bool {
0 commit comments