Skip to content

Commit 9e08ce7

Browse files
committed
refactor: moved InPlaceDrop into in_place_drop.rs
1 parent a3f3fc5 commit 9e08ce7

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use core::ptr::{self};
2+
use core::slice::{self};
3+
4+
// A helper struct for in-place iteration that drops the destination slice of iteration,
5+
// i.e. the head. The source slice (the tail) is dropped by IntoIter.
6+
pub (super) struct InPlaceDrop<T> {
7+
pub (super) inner: *mut T,
8+
pub (super) dst: *mut T,
9+
}
10+
11+
impl<T> InPlaceDrop<T> {
12+
fn len(&self) -> usize {
13+
unsafe { self.dst.offset_from(self.inner) as usize }
14+
}
15+
}
16+
17+
impl<T> Drop for InPlaceDrop<T> {
18+
#[inline]
19+
fn drop(&mut self) {
20+
unsafe {
21+
ptr::drop_in_place(slice::from_raw_parts_mut(self.inner, self.len()));
22+
}
23+
}
24+
}

library/alloc/src/vec/mod.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ use self::set_len_on_drop::SetLenOnDrop;
113113

114114
mod set_len_on_drop;
115115

116+
use self::in_place_drop::InPlaceDrop;
117+
118+
mod in_place_drop;
119+
116120
/// A contiguous growable array type, written `Vec<T>` but pronounced 'vector'.
117121
///
118122
/// # Examples
@@ -2233,28 +2237,6 @@ where
22332237
}
22342238
}
22352239

2236-
// A helper struct for in-place iteration that drops the destination slice of iteration,
2237-
// i.e. the head. The source slice (the tail) is dropped by IntoIter.
2238-
struct InPlaceDrop<T> {
2239-
inner: *mut T,
2240-
dst: *mut T,
2241-
}
2242-
2243-
impl<T> InPlaceDrop<T> {
2244-
fn len(&self) -> usize {
2245-
unsafe { self.dst.offset_from(self.inner) as usize }
2246-
}
2247-
}
2248-
2249-
impl<T> Drop for InPlaceDrop<T> {
2250-
#[inline]
2251-
fn drop(&mut self) {
2252-
unsafe {
2253-
ptr::drop_in_place(slice::from_raw_parts_mut(self.inner, self.len()));
2254-
}
2255-
}
2256-
}
2257-
22582240
impl<T> SpecFromIter<T, IntoIter<T>> for Vec<T> {
22592241
fn from_iter(iterator: IntoIter<T>) -> Self {
22602242
// A common case is passing a vector into a function which immediately

0 commit comments

Comments
 (0)