@@ -74,6 +74,7 @@ use core::ops::{Index, IndexMut};
74
74
use core:: ops;
75
75
use core:: ptr;
76
76
use core:: slice;
77
+ use super :: vec_deque:: VecDeque ;
77
78
78
79
use super :: SpecExtend ;
79
80
use super :: range:: RangeArgument ;
@@ -843,20 +844,20 @@ impl<T> Vec<T> {
843
844
let end = * range. end ( ) . unwrap_or ( & len) ;
844
845
assert ! ( start <= end) ;
845
846
assert ! ( end <= len) ;
847
+ let mut drain_vec = VecDeque :: new ( ) ;
846
848
847
849
unsafe {
848
- // set self.vec length's to start, to be safe in case Drain is leaked
849
- self . set_len ( start) ;
850
- // Use the borrow in the IterMut to indicate borrowing behavior of the
851
- // whole Drain iterator (like &mut T).
852
- let range_slice = slice:: from_raw_parts_mut ( self . as_mut_ptr ( ) . offset ( start as isize ) ,
853
- end - start) ;
854
- Drain {
855
- tail_start : end,
856
- tail_len : len - end,
857
- iter : range_slice. iter_mut ( ) ,
858
- vec : self as * mut _ ,
850
+ for i in start..end {
851
+ let p = self . as_ptr ( ) . offset ( i as isize ) ;
852
+ drain_vec. push_back ( ptr:: read ( p) ) ;
859
853
}
854
+ let src = self . as_ptr ( ) . offset ( end as isize ) ;
855
+ let dst = self . as_mut_ptr ( ) . offset ( start as isize ) ;
856
+ ptr:: copy ( src, dst, len - end) ;
857
+ self . set_len ( len - ( end - start) ) ;
858
+ }
859
+ Drain {
860
+ deque : drain_vec
860
861
}
861
862
}
862
863
@@ -1755,64 +1756,43 @@ impl<T> Drop for IntoIter<T> {
1755
1756
/// [`drain`]: struct.Vec.html#method.drain
1756
1757
/// [`Vec`]: struct.Vec.html
1757
1758
#[ stable( feature = "drain" , since = "1.6.0" ) ]
1758
- pub struct Drain < ' a , T : ' a > {
1759
- /// Index of tail to preserve
1760
- tail_start : usize ,
1761
- /// Length of tail
1762
- tail_len : usize ,
1759
+ pub struct Drain < T > {
1763
1760
/// Current remaining range to remove
1764
- iter : slice:: IterMut < ' a , T > ,
1765
- vec : * mut Vec < T > ,
1761
+ deque : VecDeque < T >
1766
1762
}
1767
1763
1768
1764
#[ stable( feature = "drain" , since = "1.6.0" ) ]
1769
- unsafe impl < ' a , T : Sync > Sync for Drain < ' a , T > { }
1765
+ unsafe impl < T : Sync > Sync for Drain < T > { }
1770
1766
#[ stable( feature = "drain" , since = "1.6.0" ) ]
1771
- unsafe impl < ' a , T : Send > Send for Drain < ' a , T > { }
1767
+ unsafe impl < T : Send > Send for Drain < T > { }
1772
1768
1773
1769
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1774
- impl < ' a , T > Iterator for Drain < ' a , T > {
1770
+ impl < T > Iterator for Drain < T > {
1775
1771
type Item = T ;
1776
1772
1777
1773
#[ inline]
1778
1774
fn next ( & mut self ) -> Option < T > {
1779
- self . iter . next ( ) . map ( |elt| unsafe { ptr :: read ( elt as * const _ ) } )
1775
+ self . deque . pop_front ( )
1780
1776
}
1781
1777
1782
1778
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1783
- self . iter . size_hint ( )
1779
+ ( self . deque . len ( ) , Some ( self . deque . len ( ) ) )
1784
1780
}
1785
1781
}
1786
1782
1787
1783
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1788
- impl < ' a , T > DoubleEndedIterator for Drain < ' a , T > {
1784
+ impl < T > DoubleEndedIterator for Drain < T > {
1789
1785
#[ inline]
1790
1786
fn next_back ( & mut self ) -> Option < T > {
1791
- self . iter . next_back ( ) . map ( |elt| unsafe { ptr :: read ( elt as * const _ ) } )
1787
+ self . deque . pop_back ( )
1792
1788
}
1793
1789
}
1794
1790
1795
1791
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1796
- impl < ' a , T > Drop for Drain < ' a , T > {
1792
+ impl < T > Drop for Drain < T > {
1797
1793
fn drop ( & mut self ) {
1798
- // exhaust self first
1799
- while let Some ( _) = self . next ( ) { }
1800
-
1801
- if self . tail_len > 0 {
1802
- unsafe {
1803
- let source_vec = & mut * self . vec ;
1804
- // memmove back untouched tail, update to new length
1805
- let start = source_vec. len ( ) ;
1806
- let tail = self . tail_start ;
1807
- let src = source_vec. as_ptr ( ) . offset ( tail as isize ) ;
1808
- let dst = source_vec. as_mut_ptr ( ) . offset ( start as isize ) ;
1809
- ptr:: copy ( src, dst, self . tail_len ) ;
1810
- source_vec. set_len ( start + self . tail_len ) ;
1811
- }
1812
- }
1813
1794
}
1814
1795
}
1815
1796
1816
-
1817
1797
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1818
- impl < ' a , T > ExactSizeIterator for Drain < ' a , T > { }
1798
+ impl < T > ExactSizeIterator for Drain < T > { }
0 commit comments