diff options
| author | Zalathar <Zalathar@users.noreply.github.com> | 2024-01-13 17:12:15 +1100 |
|---|---|---|
| committer | Zalathar <Zalathar@users.noreply.github.com> | 2024-01-13 17:21:54 +1100 |
| commit | a655558b380bf689f806e45fc896b7e6ba739cb7 (patch) | |
| tree | 39e5025db816509136b69ab3da59151b929d0d25 /library/alloc | |
| parent | 89110dafe79621fa07d6cd7c8493dd1d0a83cfaf (diff) | |
| download | rust-a655558b380bf689f806e45fc896b7e6ba739cb7.tar.gz rust-a655558b380bf689f806e45fc896b7e6ba739cb7.zip | |
Remove special-case handling of `vec.split_off(0)`
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 8 | ||||
| -rw-r--r-- | library/alloc/tests/vec.rs | 24 |
2 files changed, 18 insertions, 14 deletions
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 8aa0c6e7ed6..9603f03d27f 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -2195,14 +2195,6 @@ impl<T, A: Allocator> Vec<T, A> { assert_failed(at, self.len()); } - if at == 0 { - // the new vector can take over the original buffer and avoid the copy - return mem::replace( - self, - Vec::with_capacity_in(self.capacity(), self.allocator().clone()), - ); - } - let other_len = self.len - at; let mut other = Vec::with_capacity_in(other_len, self.allocator().clone()); diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index 0f5e0d99eca..364acb32f65 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -958,23 +958,35 @@ fn test_append() { #[test] fn test_split_off() { let mut vec = vec![1, 2, 3, 4, 5, 6]; + let orig_ptr = vec.as_ptr(); let orig_capacity = vec.capacity(); - let vec2 = vec.split_off(4); + + let split_off = vec.split_off(4); assert_eq!(vec, [1, 2, 3, 4]); - assert_eq!(vec2, [5, 6]); + assert_eq!(split_off, [5, 6]); assert_eq!(vec.capacity(), orig_capacity); + assert_eq!(vec.as_ptr(), orig_ptr); } #[test] fn test_split_off_take_all() { - let mut vec = vec![1, 2, 3, 4, 5, 6]; + // Allocate enough capacity that we can tell whether the split-off vector's + // capacity is based on its size, or (incorrectly) on the original capacity. + let mut vec = Vec::with_capacity(1000); + vec.extend([1, 2, 3, 4, 5, 6]); let orig_ptr = vec.as_ptr(); let orig_capacity = vec.capacity(); - let vec2 = vec.split_off(0); + + let split_off = vec.split_off(0); assert_eq!(vec, []); - assert_eq!(vec2, [1, 2, 3, 4, 5, 6]); + assert_eq!(split_off, [1, 2, 3, 4, 5, 6]); assert_eq!(vec.capacity(), orig_capacity); - assert_eq!(vec2.as_ptr(), orig_ptr); + assert_eq!(vec.as_ptr(), orig_ptr); + + // The split-off vector should be newly-allocated, and should not have + // stolen the original vector's allocation. + assert!(split_off.capacity() < orig_capacity); + assert_ne!(split_off.as_ptr(), orig_ptr); } #[test] |
