@@ -22,6 +22,11 @@ use crate::collections::TryReserveError;
22
22
use crate :: raw_vec:: RawVec ;
23
23
use crate :: vec:: Vec ;
24
24
25
+ #[ stable( feature = "drain" , since = "1.6.0" ) ]
26
+ pub use self :: drain:: Drain ;
27
+
28
+ mod drain;
29
+
25
30
#[ cfg( test) ]
26
31
mod tests;
27
32
@@ -866,6 +871,18 @@ impl<T> VecDeque<T> {
866
871
/// ```
867
872
#[ stable( feature = "deque_extras" , since = "1.16.0" ) ]
868
873
pub fn truncate ( & mut self , len : usize ) {
874
+ /// Runs the destructor for all items in the slice when it gets dropped (normally or
875
+ /// during unwinding).
876
+ struct Dropper < ' a , T > ( & ' a mut [ T ] ) ;
877
+
878
+ impl < ' a , T > Drop for Dropper < ' a , T > {
879
+ fn drop ( & mut self ) {
880
+ unsafe {
881
+ ptr:: drop_in_place ( self . 0 ) ;
882
+ }
883
+ }
884
+ }
885
+
869
886
// Safe because:
870
887
//
871
888
// * Any slice passed to `drop_in_place` is valid; the second case has
@@ -888,8 +905,11 @@ impl<T> VecDeque<T> {
888
905
let drop_back = back as * mut _ ;
889
906
let drop_front = front. get_unchecked_mut ( len..) as * mut _ ;
890
907
self . head = self . wrap_sub ( self . head , num_dropped) ;
908
+
909
+ // Make sure the second half is dropped even when a destructor
910
+ // in the first one panics.
911
+ let _back_dropper = Dropper ( & mut * drop_back) ;
891
912
ptr:: drop_in_place ( drop_front) ;
892
- ptr:: drop_in_place ( drop_back) ;
893
913
}
894
914
}
895
915
}
@@ -2526,113 +2546,6 @@ impl<T> ExactSizeIterator for IntoIter<T> {
2526
2546
#[ stable( feature = "fused" , since = "1.26.0" ) ]
2527
2547
impl < T > FusedIterator for IntoIter < T > { }
2528
2548
2529
- /// A draining iterator over the elements of a `VecDeque`.
2530
- ///
2531
- /// This `struct` is created by the [`drain`] method on [`VecDeque`]. See its
2532
- /// documentation for more.
2533
- ///
2534
- /// [`drain`]: struct.VecDeque.html#method.drain
2535
- /// [`VecDeque`]: struct.VecDeque.html
2536
- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2537
- pub struct Drain < ' a , T : ' a > {
2538
- after_tail : usize ,
2539
- after_head : usize ,
2540
- iter : Iter < ' a , T > ,
2541
- deque : NonNull < VecDeque < T > > ,
2542
- }
2543
-
2544
- #[ stable( feature = "collection_debug" , since = "1.17.0" ) ]
2545
- impl < T : fmt:: Debug > fmt:: Debug for Drain < ' _ , T > {
2546
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2547
- f. debug_tuple ( "Drain" )
2548
- . field ( & self . after_tail )
2549
- . field ( & self . after_head )
2550
- . field ( & self . iter )
2551
- . finish ( )
2552
- }
2553
- }
2554
-
2555
- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2556
- unsafe impl < T : Sync > Sync for Drain < ' _ , T > { }
2557
- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2558
- unsafe impl < T : Send > Send for Drain < ' _ , T > { }
2559
-
2560
- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2561
- impl < T > Drop for Drain < ' _ , T > {
2562
- fn drop ( & mut self ) {
2563
- self . for_each ( drop) ;
2564
-
2565
- let source_deque = unsafe { self . deque . as_mut ( ) } ;
2566
-
2567
- // T = source_deque_tail; H = source_deque_head; t = drain_tail; h = drain_head
2568
- //
2569
- // T t h H
2570
- // [. . . o o x x o o . . .]
2571
- //
2572
- let orig_tail = source_deque. tail ;
2573
- let drain_tail = source_deque. head ;
2574
- let drain_head = self . after_tail ;
2575
- let orig_head = self . after_head ;
2576
-
2577
- let tail_len = count ( orig_tail, drain_tail, source_deque. cap ( ) ) ;
2578
- let head_len = count ( drain_head, orig_head, source_deque. cap ( ) ) ;
2579
-
2580
- // Restore the original head value
2581
- source_deque. head = orig_head;
2582
-
2583
- match ( tail_len, head_len) {
2584
- ( 0 , 0 ) => {
2585
- source_deque. head = 0 ;
2586
- source_deque. tail = 0 ;
2587
- }
2588
- ( 0 , _) => {
2589
- source_deque. tail = drain_head;
2590
- }
2591
- ( _, 0 ) => {
2592
- source_deque. head = drain_tail;
2593
- }
2594
- _ => unsafe {
2595
- if tail_len <= head_len {
2596
- source_deque. tail = source_deque. wrap_sub ( drain_head, tail_len) ;
2597
- source_deque. wrap_copy ( source_deque. tail , orig_tail, tail_len) ;
2598
- } else {
2599
- source_deque. head = source_deque. wrap_add ( drain_tail, head_len) ;
2600
- source_deque. wrap_copy ( drain_tail, drain_head, head_len) ;
2601
- }
2602
- } ,
2603
- }
2604
- }
2605
- }
2606
-
2607
- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2608
- impl < T > Iterator for Drain < ' _ , T > {
2609
- type Item = T ;
2610
-
2611
- #[ inline]
2612
- fn next ( & mut self ) -> Option < T > {
2613
- self . iter . next ( ) . map ( |elt| unsafe { ptr:: read ( elt) } )
2614
- }
2615
-
2616
- #[ inline]
2617
- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2618
- self . iter . size_hint ( )
2619
- }
2620
- }
2621
-
2622
- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2623
- impl < T > DoubleEndedIterator for Drain < ' _ , T > {
2624
- #[ inline]
2625
- fn next_back ( & mut self ) -> Option < T > {
2626
- self . iter . next_back ( ) . map ( |elt| unsafe { ptr:: read ( elt) } )
2627
- }
2628
- }
2629
-
2630
- #[ stable( feature = "drain" , since = "1.6.0" ) ]
2631
- impl < T > ExactSizeIterator for Drain < ' _ , T > { }
2632
-
2633
- #[ stable( feature = "fused" , since = "1.26.0" ) ]
2634
- impl < T > FusedIterator for Drain < ' _ , T > { }
2635
-
2636
2549
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2637
2550
impl < A : PartialEq > PartialEq for VecDeque < A > {
2638
2551
fn eq ( & self , other : & VecDeque < A > ) -> bool {
0 commit comments