Kill unique and shared

This commit is contained in:
Manish Goregaokar 2017-08-25 16:22:11 -07:00
parent 17bcb443cb
commit 2b99611291
4 changed files with 99 additions and 35 deletions

View file

@ -1035,7 +1035,7 @@ impl<K, V, S> HashMap<K, V, S>
/// assert!(a.is_empty());
/// ```
#[inline]
pub fn drain(&mut self) -> Drain<K, V> {
pub fn drain(&mut self) -> Drain<K, V> where K: 'static, V: 'static {
Drain { inner: self.table.drain() }
}
@ -1053,7 +1053,7 @@ impl<K, V, S> HashMap<K, V, S>
/// assert!(a.is_empty());
/// ```
#[inline]
pub fn clear(&mut self) {
pub fn clear(&mut self) where K: 'static, V: 'static {
self.drain();
}
@ -1409,7 +1409,7 @@ impl<'a, K, V: Debug> fmt::Debug for Values<'a, K, V> {
///
/// [`drain`]: struct.HashMap.html#method.drain
/// [`HashMap`]: struct.HashMap.html
pub struct Drain<'a, K: 'a, V: 'a> {
pub struct Drain<'a, K: 'static, V: 'static> {
pub(super) inner: table::Drain<'a, K, V>,
}

View file

@ -470,7 +470,7 @@ impl<T, S> HashSet<T, S>
/// v.clear();
/// assert!(v.is_empty());
/// ```
pub fn clear(&mut self) {
pub fn clear(&mut self) where T: 'static {
self.map.clear()
}
@ -892,7 +892,7 @@ pub struct IntoIter<K> {
///
/// [`HashSet`]: struct.HashSet.html
/// [`drain`]: struct.HashSet.html#method.drain
pub struct Drain<'a, K: 'a> {
pub struct Drain<'a, K: 'static> {
iter: map::Drain<'a, K, ()>,
}

View file

@ -16,7 +16,8 @@ use marker;
use mem::{align_of, size_of};
use mem;
use ops::{Deref, DerefMut};
use ptr::{self, Unique, Shared};
use ptr;
use shim::{Unique, Shared};
use self::BucketState::*;
@ -997,7 +998,7 @@ impl<K, V> IntoIter<K, V> {
}
/// Iterator over the entries in a table, clearing the table.
pub struct Drain<'a, K: 'a, V: 'a> {
pub struct Drain<'a, K: 'static, V: 'static> {
table: Shared<RawTable<K, V>>,
iter: RawBuckets<'static, K, V>,
marker: marker::PhantomData<&'a RawTable<K, V>>,
@ -1105,7 +1106,7 @@ impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
}
}
impl<'a, K: 'a, V: 'a> Drop for Drain<'a, K, V> {
impl<'a, K: 'static, V: 'static> Drop for Drain<'a, K, V> {
fn drop(&mut self) {
for _ in self {}
}

View file

@ -1,5 +1,5 @@
#![feature(allocator_api)]
#![feature(alloc, shared, unique)]
#![feature(alloc)]
extern crate alloc;
@ -16,3 +16,66 @@ pub mod hash_map {
pub mod hash_set {
pub use super::impls::set::*;
}
mod shim {
use std::marker::PhantomData;
pub struct NonZeroPtr<T: 'static>(&'static T);
impl<T: 'static> NonZeroPtr<T> {
pub unsafe fn new_unchecked(ptr: *mut T) -> Self {
NonZeroPtr(&*ptr)
}
pub fn as_ptr(&self) -> *mut T {
self.0 as *const T as *mut T
}
}
pub struct Unique<T: 'static> {
ptr: NonZeroPtr<T>,
_marker: PhantomData<T>,
}
impl<T: 'static> Unique<T> {
pub unsafe fn new_unchecked(ptr: *mut T) -> Self {
Unique {
ptr: NonZeroPtr::new_unchecked(ptr),
_marker: PhantomData,
}
}
pub fn as_ptr(&self) -> *mut T {
self.ptr.as_ptr()
}
}
unsafe impl<T: Send + 'static> Send for Unique<T> { }
unsafe impl<T: Sync + 'static> Sync for Unique<T> { }
pub struct Shared<T: 'static> {
ptr: NonZeroPtr<T>,
_marker: PhantomData<T>,
// force it to be !Send/!Sync
_marker2: PhantomData<*const u8>,
}
impl<T: 'static> Shared<T> {
pub unsafe fn new_unchecked(ptr: *mut T) -> Self {
Shared {
ptr: NonZeroPtr::new_unchecked(ptr),
_marker: PhantomData,
_marker2: PhantomData,
}
}
pub unsafe fn as_mut(&self) -> &mut T {
&mut *self.ptr.as_ptr()
}
}
impl<'a, T> From<&'a mut T> for Shared<T> {
fn from(reference: &'a mut T) -> Self {
unsafe { Shared::new_unchecked(reference) }
}
}
}