std::vec: remove aliasing &mut [] and &[] from shift_opt.

Also, dramatically simplify it with some tasteful raw pointers, rather
than treating everything as a nail with `transmute`.
This commit is contained in:
Huon Wilson
2013-12-16 23:53:57 +11:00
parent e7b0e0adbb
commit 33b6bf4bc1

View File

@@ -1621,54 +1621,38 @@ impl<T> OwnedVector<T> for ~[T] {
} }
fn shift_opt(&mut self) -> Option<T> { fn shift_opt(&mut self) -> Option<T> {
unsafe { match self.len() {
let ln = match self.len() { 0 => None,
0 => return None, 1 => self.pop_opt(),
1 => return self.pop_opt(),
2 => { 2 => {
let last = self.pop(); let last = self.pop();
let first = self.pop_opt(); let first = self.pop_opt();
self.push(last); self.push(last);
return first; first
} }
x => x len => {
}; unsafe {
let next_len = len - 1;
let next_ln = self.len() - 1; let ptr = self.as_ptr();
// Save the last element. We're going to overwrite its position // copy out the head element, for the moment it exists
let work_elt = self.pop(); // unsafely on the stack and as the first element of the
// We still should have room to work where what last element was // vector.
assert!(self.capacity() >= ln); let head = ptr::read_ptr(ptr);
// Pretend like we have the original length so we can use
// the vector copy_memory to overwrite the hole we just made
self.set_len(ln);
// Memcopy the head element (the one we want) to the location we just // Memcpy everything to the left one element (leaving the
// popped. For the moment it unsafely exists at both the head and last // last element unsafely in two consecutive memory
// positions // locations)
{ ptr::copy_memory(self.as_mut_ptr(), ptr.offset(1), next_len);
let first_slice = self.slice(0, 1);
let last_slice = self.slice(next_ln, ln); // set the new length, which means the second instance of
raw::copy_memory(cast::transmute(last_slice), first_slice); // the last element is forgotten.
self.set_len(next_len);
Some(head)
} }
// Memcopy everything to the left one element
{
let init_slice = self.slice(0, next_ln);
let tail_slice = self.slice(1, ln);
raw::copy_memory(cast::transmute(init_slice),
tail_slice);
} }
// Set the new length. Now the vector is back to normal
self.set_len(next_ln);
// Swap out the element we want from the end
let vp = self.as_mut_ptr();
let vp = ptr::mut_offset(vp, (next_ln - 1) as int);
Some(ptr::replace_ptr(vp, work_elt))
} }
} }