diff options
| author | Waffle <waffle.lapkin@gmail.com> | 2021-02-27 00:20:46 +0300 |
|---|---|---|
| committer | Waffle <waffle.lapkin@gmail.com> | 2021-02-27 00:27:34 +0300 |
| commit | 2f04a793ae0b9331f6d853a971a670c8ea99b0b7 (patch) | |
| tree | 1897993205a9c779e3c68408b1d537171d1434e8 /library/alloc/src/vec/mod.rs | |
| parent | cecdb181ade91c0a5ffab9a148dba68fc7d00ee3 (diff) | |
| download | rust-2f04a793ae0b9331f6d853a971a670c8ea99b0b7.tar.gz rust-2f04a793ae0b9331f6d853a971a670c8ea99b0b7.zip | |
Revert `Vec::spare_capacity_mut` impl to prevent pointers invalidation
Diffstat (limited to 'library/alloc/src/vec/mod.rs')
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index b1b26194283..e5545d2a77f 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1879,7 +1879,15 @@ impl<T, A: Allocator> Vec<T, A> { #[unstable(feature = "vec_spare_capacity", issue = "75017")] #[inline] pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] { - self.split_at_spare_mut().1 + // Note: + // This method is not implemented in terms of `split_at_spare_mut`, + // to prevent invalidation of pointers to the buffer. + unsafe { + slice::from_raw_parts_mut( + self.as_mut_ptr().add(self.len) as *mut MaybeUninit<T>, + self.buf.capacity() - self.len, + ) + } } /// Returns vector content as a slice of `T`, along with the remaining spare |
