Skip to content

std: Fix iteration over vectors of 0-size values #13468

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

Merged
merged 1 commit into from
Apr 13, 2014
Merged
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
48 changes: 46 additions & 2 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,12 @@ impl<T> Vec<T> {
/// ```
#[inline]
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
let slice = Slice { data: self.ptr as *T, len: self.len };
// See the comment in as_slice() for what's going on here.
let slice = if mem::size_of::<T>() == 0 {
Slice { data: 1 as *T, len: self.len }
} else {
Slice { data: self.ptr as *T, len: self.len }
};
unsafe { transmute(slice) }
}

Expand Down Expand Up @@ -1335,7 +1340,15 @@ impl<T> Vector<T> for Vec<T> {
/// ```
#[inline]
fn as_slice<'a>(&'a self) -> &'a [T] {
let slice = Slice { data: self.ptr as *T, len: self.len };
// If we have a 0-sized vector, then the base pointer should not be NULL
// because an iterator over the slice will attempt to yield the base
// pointer as the first element in the vector, but this will end up
// being Some(NULL) which is optimized to None.
let slice = if mem::size_of::<T>() == 0 {
Slice { data: 1 as *T, len: self.len }
} else {
Slice { data: self.ptr as *T, len: self.len }
};
unsafe { transmute(slice) }
}
}
Expand Down Expand Up @@ -1588,4 +1601,35 @@ mod tests {
vec.retain(|x| x%2 == 0);
assert!(vec == Vec::from_slice([2u, 4]));
}

#[test]
fn zero_sized_values() {
let mut v = Vec::new();
assert_eq!(v.len(), 0);
v.push(());
assert_eq!(v.len(), 1);
v.push(());
assert_eq!(v.len(), 2);
assert_eq!(v.pop(), Some(()));
assert_eq!(v.pop(), Some(()));
assert_eq!(v.pop(), None);

assert_eq!(v.iter().len(), 0);
v.push(());
assert_eq!(v.iter().len(), 1);
v.push(());
assert_eq!(v.iter().len(), 2);

for &() in v.iter() {}

assert_eq!(v.mut_iter().len(), 2);
v.push(());
assert_eq!(v.mut_iter().len(), 3);
v.push(());
assert_eq!(v.mut_iter().len(), 4);

for &() in v.mut_iter() {}
unsafe { v.set_len(0); }
assert_eq!(v.mut_iter().len(), 0);
}
}