mirror of
https://github.com/servo/servo.git
synced 2025-08-10 16:05:43 +01:00
Kill unique and shared
This commit is contained in:
parent
17bcb443cb
commit
2b99611291
4 changed files with 99 additions and 35 deletions
|
@ -1035,7 +1035,7 @@ impl<K, V, S> HashMap<K, V, S>
|
||||||
/// assert!(a.is_empty());
|
/// assert!(a.is_empty());
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[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() }
|
Drain { inner: self.table.drain() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1053,7 +1053,7 @@ impl<K, V, S> HashMap<K, V, S>
|
||||||
/// assert!(a.is_empty());
|
/// assert!(a.is_empty());
|
||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) where K: 'static, V: 'static {
|
||||||
self.drain();
|
self.drain();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1409,7 +1409,7 @@ impl<'a, K, V: Debug> fmt::Debug for Values<'a, K, V> {
|
||||||
///
|
///
|
||||||
/// [`drain`]: struct.HashMap.html#method.drain
|
/// [`drain`]: struct.HashMap.html#method.drain
|
||||||
/// [`HashMap`]: struct.HashMap.html
|
/// [`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>,
|
pub(super) inner: table::Drain<'a, K, V>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -470,7 +470,7 @@ impl<T, S> HashSet<T, S>
|
||||||
/// v.clear();
|
/// v.clear();
|
||||||
/// assert!(v.is_empty());
|
/// assert!(v.is_empty());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) where T: 'static {
|
||||||
self.map.clear()
|
self.map.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -892,7 +892,7 @@ pub struct IntoIter<K> {
|
||||||
///
|
///
|
||||||
/// [`HashSet`]: struct.HashSet.html
|
/// [`HashSet`]: struct.HashSet.html
|
||||||
/// [`drain`]: struct.HashSet.html#method.drain
|
/// [`drain`]: struct.HashSet.html#method.drain
|
||||||
pub struct Drain<'a, K: 'a> {
|
pub struct Drain<'a, K: 'static> {
|
||||||
iter: map::Drain<'a, K, ()>,
|
iter: map::Drain<'a, K, ()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,8 @@ use marker;
|
||||||
use mem::{align_of, size_of};
|
use mem::{align_of, size_of};
|
||||||
use mem;
|
use mem;
|
||||||
use ops::{Deref, DerefMut};
|
use ops::{Deref, DerefMut};
|
||||||
use ptr::{self, Unique, Shared};
|
use ptr;
|
||||||
|
use shim::{Unique, Shared};
|
||||||
|
|
||||||
use self::BucketState::*;
|
use self::BucketState::*;
|
||||||
|
|
||||||
|
@ -997,7 +998,7 @@ impl<K, V> IntoIter<K, V> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterator over the entries in a table, clearing the table.
|
/// 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>>,
|
table: Shared<RawTable<K, V>>,
|
||||||
iter: RawBuckets<'static, K, V>,
|
iter: RawBuckets<'static, K, V>,
|
||||||
marker: marker::PhantomData<&'a RawTable<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) {
|
fn drop(&mut self) {
|
||||||
for _ in self {}
|
for _ in self {}
|
||||||
}
|
}
|
||||||
|
|
65
src/lib.rs
65
src/lib.rs
|
@ -1,5 +1,5 @@
|
||||||
#![feature(allocator_api)]
|
#![feature(allocator_api)]
|
||||||
#![feature(alloc, shared, unique)]
|
#![feature(alloc)]
|
||||||
|
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
|
|
||||||
|
@ -16,3 +16,66 @@ pub mod hash_map {
|
||||||
pub mod hash_set {
|
pub mod hash_set {
|
||||||
pub use super::impls::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) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue