Skip to content

Commit

Permalink
Add #[may_dangle] to RawIntoIter
Browse files Browse the repository at this point in the history
Also adds PhantomData<T> to RawTable and RawIntoIter
  • Loading branch information
Amanieu committed Mar 9, 2019
1 parent d73b3b2 commit 2678637
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/external_trait_impls/rayon/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub struct RawParDrain<'a, T> {
// We don't use a &'a mut RawTable<T> because we want RawParDrain to be
// covariant over T.
table: NonNull<RawTable<T>>,
_marker: PhantomData<&'a RawTable<T>>,
marker: PhantomData<&'a RawTable<T>>,
}

unsafe impl<T> Send for RawParDrain<'_, T> {}
Expand Down Expand Up @@ -187,7 +187,7 @@ impl<T> RawTable<T> {
pub fn par_drain(&mut self) -> RawParDrain<'_, T> {
RawParDrain {
table: NonNull::from(self),
_marker: PhantomData,
marker: PhantomData,
}
}
}
16 changes: 8 additions & 8 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ impl<K, V, S> HashMap<K, V, S> {
unsafe {
Iter {
inner: self.table.iter(),
_marker: PhantomData,
marker: PhantomData,
}
}
}
Expand Down Expand Up @@ -381,7 +381,7 @@ impl<K, V, S> HashMap<K, V, S> {
unsafe {
IterMut {
inner: self.table.iter(),
_marker: PhantomData,
marker: PhantomData,
}
}
}
Expand Down Expand Up @@ -1092,7 +1092,7 @@ where
/// [`HashMap`]: struct.HashMap.html
pub struct Iter<'a, K, V> {
inner: RawIter<(K, V)>,
_marker: PhantomData<(&'a K, &'a V)>,
marker: PhantomData<(&'a K, &'a V)>,
}

// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
Expand All @@ -1101,7 +1101,7 @@ impl<K, V> Clone for Iter<'_, K, V> {
fn clone(&self) -> Self {
Iter {
inner: self.inner.clone(),
_marker: PhantomData,
marker: PhantomData,
}
}
}
Expand All @@ -1122,7 +1122,7 @@ impl<K: Debug, V: Debug> fmt::Debug for Iter<'_, K, V> {
pub struct IterMut<'a, K, V> {
inner: RawIter<(K, V)>,
// To ensure invariance with respect to V
_marker: PhantomData<(&'a K, &'a mut V)>,
marker: PhantomData<(&'a K, &'a mut V)>,
}

impl<K, V> IterMut<'_, K, V> {
Expand All @@ -1131,7 +1131,7 @@ impl<K, V> IterMut<'_, K, V> {
pub(super) fn iter(&self) -> Iter<'_, K, V> {
Iter {
inner: self.inner.clone(),
_marker: PhantomData,
marker: PhantomData,
}
}
}
Expand All @@ -1153,7 +1153,7 @@ impl<K, V> IntoIter<K, V> {
pub(super) fn iter(&self) -> Iter<'_, K, V> {
Iter {
inner: self.inner.iter(),
_marker: PhantomData,
marker: PhantomData,
}
}
}
Expand Down Expand Up @@ -1229,7 +1229,7 @@ impl<K, V> Drain<'_, K, V> {
pub(super) fn iter(&self) -> Iter<'_, K, V> {
Iter {
inner: self.inner.iter(),
_marker: PhantomData,
marker: PhantomData,
}
}
}
Expand Down
36 changes: 33 additions & 3 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ pub struct RawTable<T> {

// Number of elements in the table, only really used by len()
items: usize,

// Tell dropck that we own instances of T.
marker: PhantomData<T>,
}

impl<T> RawTable<T> {
Expand All @@ -352,6 +355,7 @@ impl<T> RawTable<T> {
bucket_mask: 0,
items: 0,
growth_left: 0,
marker: PhantomData,
}
}

Expand All @@ -373,6 +377,7 @@ impl<T> RawTable<T> {
bucket_mask: buckets - 1,
items: 0,
growth_left: bucket_mask_to_capacity(buckets - 1),
marker: PhantomData,
})
}

Expand Down Expand Up @@ -915,7 +920,7 @@ impl<T> RawTable<T> {
iter: self.iter(),
table: ManuallyDrop::new(mem::replace(self, Self::new())),
orig_table: NonNull::from(self),
_marker: PhantomData,
marker: PhantomData,
}
}

Expand Down Expand Up @@ -1032,7 +1037,11 @@ impl<T> IntoIterator for RawTable<T> {
unsafe {
let iter = self.iter();
let alloc = self.into_alloc();
RawIntoIter { iter, alloc }
RawIntoIter {
iter,
alloc,
marker: PhantomData,
}
}
}
}
Expand Down Expand Up @@ -1225,6 +1234,7 @@ impl<T> FusedIterator for RawIter<T> {}
pub struct RawIntoIter<T> {
iter: RawIter<T>,
alloc: Option<(NonNull<u8>, Layout)>,
marker: PhantomData<T>,
}

impl<T> RawIntoIter<T> {
Expand All @@ -1237,6 +1247,26 @@ impl<T> RawIntoIter<T> {
unsafe impl<T> Send for RawIntoIter<T> where T: Send {}
unsafe impl<T> Sync for RawIntoIter<T> where T: Sync {}

#[cfg(feature = "nightly")]
unsafe impl<#[may_dangle] T> Drop for RawIntoIter<T> {
#[inline]
fn drop(&mut self) {
unsafe {
// Drop all remaining elements
if mem::needs_drop::<T>() {
while let Some(item) = self.iter.next() {
item.drop();
}
}

// Free the table
if let Some((ptr, layout)) = self.alloc {
dealloc(ptr.as_ptr(), layout);
}
}
}
}
#[cfg(not(feature = "nightly"))]
impl<T> Drop for RawIntoIter<T> {
#[inline]
fn drop(&mut self) {
Expand Down Expand Up @@ -1285,7 +1315,7 @@ pub struct RawDrain<'a, T> {

// We don't use a &'a mut RawTable<T> because we want RawDrain to be
// covariant over T.
_marker: PhantomData<&'a RawTable<T>>,
marker: PhantomData<&'a RawTable<T>>,
}

impl<T> RawDrain<'_, T> {
Expand Down

0 comments on commit 2678637

Please sign in to comment.