Skip to content

Commit

Permalink
refactor: moved InPlaceDrop into in_place_drop.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperC286 committed Dec 29, 2020
1 parent a3f3fc5 commit 9e08ce7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
24 changes: 24 additions & 0 deletions 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()));
}
}
}
26 changes: 4 additions & 22 deletions library/alloc/src/vec/mod.rs
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9e08ce7

Please sign in to comment.