@@ -17,7 +17,6 @@ use core::prelude::*;
17
17
use core:: fmt;
18
18
use core:: hash;
19
19
use core:: iter:: FromIterator ;
20
- use core:: marker:: PhantomData ;
21
20
use core:: mem;
22
21
use core:: ops:: { self , Deref , Add , Index } ;
23
22
use core:: ptr;
@@ -745,10 +744,9 @@ impl String {
745
744
746
745
Drain {
747
746
start : start,
748
- tail_start : end,
747
+ end : end,
749
748
iter : chars_iter,
750
749
string : self_ptr,
751
- _marker : PhantomData ,
752
750
}
753
751
}
754
752
}
@@ -1132,14 +1130,14 @@ impl fmt::Write for String {
1132
1130
/// A draining iterator for `String`.
1133
1131
#[ unstable( feature = "collections_drain" , reason = "recently added" ) ]
1134
1132
pub struct Drain < ' a > {
1133
+ /// Will be used as &'a mut String in the destructor
1135
1134
string : * mut String ,
1136
1135
/// Start of part to remove
1137
1136
start : usize ,
1138
- /// Index of tail to preserve
1139
- tail_start : usize ,
1137
+ /// End of part to remove
1138
+ end : usize ,
1140
1139
/// Current remaining range to remove
1141
1140
iter : Chars < ' a > ,
1142
- _marker : PhantomData < & ' a mut String > ,
1143
1141
}
1144
1142
1145
1143
unsafe impl < ' a > Sync for Drain < ' a > { }
@@ -1149,15 +1147,12 @@ unsafe impl<'a> Send for Drain<'a> {}
1149
1147
impl < ' a > Drop for Drain < ' a > {
1150
1148
fn drop ( & mut self ) {
1151
1149
unsafe {
1152
- // memmove back untouched tail, then truncate & reset length
1150
+ // Use Vec::drain. "Reaffirm" the bounds checks to avoid
1151
+ // panic code being inserted again.
1153
1152
let self_vec = ( * self . string ) . as_mut_vec ( ) ;
1154
- let tail_len = self_vec. len ( ) - self . tail_start ;
1155
- if tail_len > 0 {
1156
- let src = self_vec. as_ptr ( ) . offset ( self . tail_start as isize ) ;
1157
- let dst = self_vec. as_mut_ptr ( ) . offset ( self . start as isize ) ;
1158
- ptr:: copy ( src, dst, tail_len) ;
1153
+ if self . start <= self . end && self . end <= self_vec. len ( ) {
1154
+ self_vec. drain ( self . start ..self . end ) ;
1159
1155
}
1160
- self_vec. set_len ( self . start + tail_len) ;
1161
1156
}
1162
1157
}
1163
1158
}
@@ -1183,6 +1178,3 @@ impl<'a> DoubleEndedIterator for Drain<'a> {
1183
1178
self . iter . next_back ( )
1184
1179
}
1185
1180
}
1186
-
1187
- #[ unstable( feature = "collections_drain" , reason = "recently added" ) ]
1188
- impl < ' a > ExactSizeIterator for Drain < ' a > { }
0 commit comments