@@ -74,6 +74,7 @@ use core::ops::{Index, IndexMut};
7474use core:: ops;
7575use core:: ptr;
7676use core:: slice;
77+ use super :: vec_deque:: VecDeque ;
7778
7879use super :: SpecExtend ;
7980use super :: range:: RangeArgument ;
@@ -843,20 +844,20 @@ impl<T> Vec<T> {
843844 let end = * range. end ( ) . unwrap_or ( & len) ;
844845 assert ! ( start <= end) ;
845846 assert ! ( end <= len) ;
847+ let mut drain_vec = VecDeque :: new ( ) ;
846848
847849 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) ) ;
859853 }
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
860861 }
861862 }
862863
@@ -1755,64 +1756,43 @@ impl<T> Drop for IntoIter<T> {
17551756/// [`drain`]: struct.Vec.html#method.drain
17561757/// [`Vec`]: struct.Vec.html
17571758#[ 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 > {
17631760 /// Current remaining range to remove
1764- iter : slice:: IterMut < ' a , T > ,
1765- vec : * mut Vec < T > ,
1761+ deque : VecDeque < T >
17661762}
17671763
17681764#[ 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 > { }
17701766#[ 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 > { }
17721768
17731769#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1774- impl < ' a , T > Iterator for Drain < ' a , T > {
1770+ impl < T > Iterator for Drain < T > {
17751771 type Item = T ;
17761772
17771773 #[ inline]
17781774 fn next ( & mut self ) -> Option < T > {
1779- self . iter . next ( ) . map ( |elt| unsafe { ptr :: read ( elt as * const _ ) } )
1775+ self . deque . pop_front ( )
17801776 }
17811777
17821778 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1783- self . iter . size_hint ( )
1779+ ( self . deque . len ( ) , Some ( self . deque . len ( ) ) )
17841780 }
17851781}
17861782
17871783#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1788- impl < ' a , T > DoubleEndedIterator for Drain < ' a , T > {
1784+ impl < T > DoubleEndedIterator for Drain < T > {
17891785 #[ inline]
17901786 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 ( )
17921788 }
17931789}
17941790
17951791#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1796- impl < ' a , T > Drop for Drain < ' a , T > {
1792+ impl < T > Drop for Drain < T > {
17971793 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- }
18131794 }
18141795}
18151796
1816-
18171797#[ 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