diff options
| author | C <DeveloperC@protonmail.com> | 2020-12-05 01:26:52 +0000 |
|---|---|---|
| committer | C <DeveloperC@protonmail.com> | 2020-12-29 14:03:30 +0000 |
| commit | 9e08ce7190dd63afc3e5a12f89134c760566513a (patch) | |
| tree | c4fb66820c9b9f782c3466860cb01263eef98fb4 /library/alloc/src/vec | |
| parent | a3f3fc5aedc9636ea580d8241746e06db6031270 (diff) | |
| download | rust-9e08ce7190dd63afc3e5a12f89134c760566513a.tar.gz rust-9e08ce7190dd63afc3e5a12f89134c760566513a.zip | |
refactor: moved InPlaceDrop into in_place_drop.rs
Diffstat (limited to 'library/alloc/src/vec')
| -rw-r--r-- | library/alloc/src/vec/in_place_drop.rs | 24 | ||||
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 26 |
2 files changed, 28 insertions, 22 deletions
diff --git a/library/alloc/src/vec/in_place_drop.rs b/library/alloc/src/vec/in_place_drop.rs new file mode 100644 index 00000000000..3a0ecc529c0 --- /dev/null +++ b/library/alloc/src/vec/in_place_drop.rs @@ -0,0 +1,24 @@ +use core::ptr::{self}; +use core::slice::{self}; + +// A helper struct for in-place iteration that drops the destination slice of iteration, +// i.e. the head. The source slice (the tail) is dropped by IntoIter. +pub (super) struct InPlaceDrop<T> { + pub (super) inner: *mut T, + pub (super) dst: *mut T, +} + +impl<T> InPlaceDrop<T> { + fn len(&self) -> usize { + unsafe { self.dst.offset_from(self.inner) as usize } + } +} + +impl<T> Drop for InPlaceDrop<T> { + #[inline] + fn drop(&mut self) { + unsafe { + ptr::drop_in_place(slice::from_raw_parts_mut(self.inner, self.len())); + } + } +} diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 62ca5b950ce..08a920a9c60 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -113,6 +113,10 @@ use self::set_len_on_drop::SetLenOnDrop; mod set_len_on_drop; +use self::in_place_drop::InPlaceDrop; + +mod in_place_drop; + /// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'. /// /// # Examples @@ -2233,28 +2237,6 @@ where } } -// A helper struct for in-place iteration that drops the destination slice of iteration, -// i.e. the head. The source slice (the tail) is dropped by IntoIter. -struct InPlaceDrop<T> { - inner: *mut T, - dst: *mut T, -} - -impl<T> InPlaceDrop<T> { - fn len(&self) -> usize { - unsafe { self.dst.offset_from(self.inner) as usize } - } -} - -impl<T> Drop for InPlaceDrop<T> { - #[inline] - fn drop(&mut self) { - unsafe { - ptr::drop_in_place(slice::from_raw_parts_mut(self.inner, self.len())); - } - } -} - impl<T> SpecFromIter<T, IntoIter<T>> for Vec<T> { fn from_iter(iterator: IntoIter<T>) -> Self { // A common case is passing a vector into a function which immediately |
