refactor: moved InPlaceDrop into in_place_drop.rs
This commit is contained in:
parent
a3f3fc5aed
commit
9e08ce7190
24
library/alloc/src/vec/in_place_drop.rs
Normal file
24
library/alloc/src/vec/in_place_drop.rs
Normal file
@ -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()));
|
||||
}
|
||||
}
|
||||
}
|
@ -113,6 +113,10 @@
|
||||
|
||||
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 @@ impl<T, I> SpecFromIter<T, I> for Vec<T>
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
|
Loading…
Reference in New Issue
Block a user