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 30, 2019
1 parent b11f8cd commit f9bcc42
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 @@ -421,7 +421,7 @@ impl<K, V, S> HashMap<K, V, S> {
unsafe {
Iter {
inner: self.table.iter(),
_marker: PhantomData,
marker: PhantomData,
}
}
}
Expand Down Expand Up @@ -455,7 +455,7 @@ impl<K, V, S> HashMap<K, V, S> {
unsafe {
IterMut {
inner: self.table.iter(),
_marker: PhantomData,
marker: PhantomData,
}
}
}
Expand Down Expand Up @@ -1089,7 +1089,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 @@ -1098,7 +1098,7 @@ impl<K, V> Clone for Iter<'_, K, V> {
fn clone(&self) -> Self {
Iter {
inner: self.inner.clone(),
_marker: PhantomData,
marker: PhantomData,
}
}
}
Expand All @@ -1119,7 +1119,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 @@ -1128,7 +1128,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 @@ -1150,7 +1150,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 @@ -1226,7 +1226,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 @@ -343,6 +343,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 @@ -359,6 +362,7 @@ impl<T> RawTable<T> {
bucket_mask: 0,
items: 0,
growth_left: 0,
marker: PhantomData,
}
}

Expand All @@ -380,6 +384,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 @@ -922,7 +927,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 @@ -1039,7 +1044,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 @@ -1229,6 +1238,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 @@ -1241,6 +1251,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 @@ -1289,7 +1319,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 f9bcc42

Please sign in to comment.