Skip to content

Correct code for operations on empty Vecs #24604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions src/libcollections/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,10 @@ impl<T> Vec<T> {
let begin = ptr as *const T;
let end = if mem::size_of::<T>() == 0 {
(ptr as usize + self.len()) as *const T
} else if self.cap == 0 {
begin
} else {
ptr.offset(self.len() as isize) as *const T
begin.offset(self.len() as isize)
};
mem::forget(self);
IntoIter { allocation: ptr, cap: cap, ptr: begin, end: end }
Expand Down Expand Up @@ -772,6 +774,8 @@ impl<T> Vec<T> {
let begin = *self.ptr as *const T;
let end = if mem::size_of::<T>() == 0 {
(*self.ptr as usize + self.len()) as *const T
} else if self.cap == 0 {
begin
} else {
(*self.ptr).offset(self.len() as isize) as *const T
};
Expand Down Expand Up @@ -883,16 +887,23 @@ impl<T> Vec<T> {
//
// 2) If the size of the elements in the vector is >1, the `usize` ->
// `isize` conversion can't overflow.
let offset = vec.len() as isize;
let start = vec.as_mut_ptr();

let end = if vec.cap == 0 {
start
} else {
unsafe {
start.offset(vec.len() as isize)
}
};

let mut pv = PartialVecNonZeroSized {
vec: vec,

start_t: start,
// This points inside the vector, as the vector has length
// `offset`.
end_t: unsafe { start.offset(offset) },
end_t: end,
start_u: start as *mut U,
end_u: start as *mut U,

Expand Down Expand Up @@ -1054,17 +1065,18 @@ impl<T> Vec<T> {
assert!(at <= self.len(), "`at` out of bounds");

let other_len = self.len - at;
let ptr = if self.cap == 0 {
self.as_ptr()
} else {
self.as_ptr().offset(at as isize)
};
let mut other = Vec::with_capacity(other_len);

// Unsafely `set_len` and copy items to `other`.
unsafe {
self.set_len(at);
other.set_len(other_len);

ptr::copy_nonoverlapping(
self.as_ptr().offset(at as isize),
other.as_mut_ptr(),
other.len());
ptr::copy_nonoverlapping(ptr, other.as_mut_ptr(), other.len());
}
other
}
Expand Down