Skip to content

Commit a655558

Browse files
committed
Remove special-case handling of vec.split_off(0)
1 parent 89110da commit a655558

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

library/alloc/src/vec/mod.rs

-8
Original file line numberDiff line numberDiff line change
@@ -2195,14 +2195,6 @@ impl<T, A: Allocator> Vec<T, A> {
21952195
assert_failed(at, self.len());
21962196
}
21972197

2198-
if at == 0 {
2199-
// the new vector can take over the original buffer and avoid the copy
2200-
return mem::replace(
2201-
self,
2202-
Vec::with_capacity_in(self.capacity(), self.allocator().clone()),
2203-
);
2204-
}
2205-
22062198
let other_len = self.len - at;
22072199
let mut other = Vec::with_capacity_in(other_len, self.allocator().clone());
22082200

library/alloc/tests/vec.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -958,23 +958,35 @@ fn test_append() {
958958
#[test]
959959
fn test_split_off() {
960960
let mut vec = vec![1, 2, 3, 4, 5, 6];
961+
let orig_ptr = vec.as_ptr();
961962
let orig_capacity = vec.capacity();
962-
let vec2 = vec.split_off(4);
963+
964+
let split_off = vec.split_off(4);
963965
assert_eq!(vec, [1, 2, 3, 4]);
964-
assert_eq!(vec2, [5, 6]);
966+
assert_eq!(split_off, [5, 6]);
965967
assert_eq!(vec.capacity(), orig_capacity);
968+
assert_eq!(vec.as_ptr(), orig_ptr);
966969
}
967970

968971
#[test]
969972
fn test_split_off_take_all() {
970-
let mut vec = vec![1, 2, 3, 4, 5, 6];
973+
// Allocate enough capacity that we can tell whether the split-off vector's
974+
// capacity is based on its size, or (incorrectly) on the original capacity.
975+
let mut vec = Vec::with_capacity(1000);
976+
vec.extend([1, 2, 3, 4, 5, 6]);
971977
let orig_ptr = vec.as_ptr();
972978
let orig_capacity = vec.capacity();
973-
let vec2 = vec.split_off(0);
979+
980+
let split_off = vec.split_off(0);
974981
assert_eq!(vec, []);
975-
assert_eq!(vec2, [1, 2, 3, 4, 5, 6]);
982+
assert_eq!(split_off, [1, 2, 3, 4, 5, 6]);
976983
assert_eq!(vec.capacity(), orig_capacity);
977-
assert_eq!(vec2.as_ptr(), orig_ptr);
984+
assert_eq!(vec.as_ptr(), orig_ptr);
985+
986+
// The split-off vector should be newly-allocated, and should not have
987+
// stolen the original vector's allocation.
988+
assert!(split_off.capacity() < orig_capacity);
989+
assert_ne!(split_off.as_ptr(), orig_ptr);
978990
}
979991

980992
#[test]

0 commit comments

Comments
 (0)