Auto merge of #1253 - RalfJung:vec-extend, r=RalfJung
test Vec::extend Currently fails, until https://github.com/rust-lang/rust/issues/70301 gets fixed.
This commit is contained in:
commit
350004fe75
3 changed files with 39 additions and 1 deletions
|
|
@ -1 +1 @@
|
|||
6050e523bae6de61de4e060facc43dc512adaccd
|
||||
e6cef0445779724b469ab7b9a8d3c05d9e848ca8
|
||||
|
|
|
|||
|
|
@ -71,6 +71,39 @@ fn vec_reallocate() -> Vec<u8> {
|
|||
v
|
||||
}
|
||||
|
||||
fn vec_push_ptr_stable() {
|
||||
let mut v = Vec::with_capacity(10);
|
||||
v.push(0);
|
||||
let v0 = unsafe { &mut *(&mut v[0] as *mut _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
|
||||
v.push(1);
|
||||
let _val = *v0;
|
||||
}
|
||||
|
||||
fn vec_extend_ptr_stable() {
|
||||
let mut v = Vec::with_capacity(10);
|
||||
v.push(0);
|
||||
let v0 = unsafe { &mut *(&mut v[0] as *mut _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
|
||||
// `slice::Iter` (with `T: Copy`) specialization
|
||||
v.extend(&[1]);
|
||||
let _val = *v0;
|
||||
// `vec::IntoIter` specialization
|
||||
v.extend(vec![2]);
|
||||
let _val = *v0;
|
||||
// `TrustedLen` specialization
|
||||
v.extend(std::iter::once(3));
|
||||
let _val = *v0;
|
||||
// base case
|
||||
v.extend(std::iter::once(3).filter(|_| true));
|
||||
let _val = *v0;
|
||||
}
|
||||
|
||||
fn vec_truncate_ptr_stable() {
|
||||
let mut v = vec![0; 10];
|
||||
let v0 = unsafe { &mut *(&mut v[0] as *mut _) }; // laundering the lifetime -- we take care that `v` does not reallocate, so that's okay.
|
||||
v.truncate(5);
|
||||
let _val = *v0;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(vec_reallocate().len(), 5);
|
||||
|
||||
|
|
@ -89,4 +122,9 @@ fn main() {
|
|||
// Test interesting empty slice comparison
|
||||
// (one is a real pointer, one an integer pointer).
|
||||
assert_eq!((200..-5).step_by(1).collect::<Vec<isize>>(), []);
|
||||
|
||||
// liballoc has a more extensive test of this, but let's at least do a smoke test here.
|
||||
vec_push_ptr_stable();
|
||||
vec_extend_ptr_stable();
|
||||
vec_truncate_ptr_stable();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue