Skip to content

Commit 490ea42

Browse files
author
Ulrik Sverdrup
committed
Update for review comments
Thank you Gankro & Alex Crichton
1 parent 03d0c02 commit 490ea42

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

src/libcollections/string.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use core::prelude::*;
1717
use core::fmt;
1818
use core::hash;
1919
use core::iter::FromIterator;
20-
use core::marker::PhantomData;
2120
use core::mem;
2221
use core::ops::{self, Deref, Add, Index};
2322
use core::ptr;
@@ -745,10 +744,9 @@ impl String {
745744

746745
Drain {
747746
start: start,
748-
tail_start: end,
747+
end: end,
749748
iter: chars_iter,
750749
string: self_ptr,
751-
_marker: PhantomData,
752750
}
753751
}
754752
}
@@ -1132,14 +1130,14 @@ impl fmt::Write for String {
11321130
/// A draining iterator for `String`.
11331131
#[unstable(feature = "collections_drain", reason = "recently added")]
11341132
pub struct Drain<'a> {
1133+
/// Will be used as &'a mut String in the destructor
11351134
string: *mut String,
11361135
/// Start of part to remove
11371136
start: usize,
1138-
/// Index of tail to preserve
1139-
tail_start: usize,
1137+
/// End of part to remove
1138+
end: usize,
11401139
/// Current remaining range to remove
11411140
iter: Chars<'a>,
1142-
_marker: PhantomData<&'a mut String>,
11431141
}
11441142

11451143
unsafe impl<'a> Sync for Drain<'a> {}
@@ -1149,15 +1147,12 @@ unsafe impl<'a> Send for Drain<'a> {}
11491147
impl<'a> Drop for Drain<'a> {
11501148
fn drop(&mut self) {
11511149
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.
11531152
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);
11591155
}
1160-
self_vec.set_len(self.start + tail_len);
11611156
}
11621157
}
11631158
}
@@ -1183,6 +1178,3 @@ impl<'a> DoubleEndedIterator for Drain<'a> {
11831178
self.iter.next_back()
11841179
}
11851180
}
1186-
1187-
#[unstable(feature = "collections_drain", reason = "recently added")]
1188-
impl<'a> ExactSizeIterator for Drain<'a> { }

0 commit comments

Comments
 (0)