Made vec_deque::Drain, hash_map::Drain, and hash_set::Drain covariant
This commit is contained in:
parent
40f3ee2a01
commit
bf592cefde
5 changed files with 23 additions and 9 deletions
|
|
@ -2036,6 +2036,8 @@ fn assert_covariance() {
|
|||
fn keys_val<'a, 'new>(v: Keys<'a, u8, &'static str>) -> Keys<'a, u8, &'new str> { v }
|
||||
fn values_key<'a, 'new>(v: Values<'a, &'static str, u8>) -> Values<'a, &'new str, u8> { v }
|
||||
fn values_val<'a, 'new>(v: Values<'a, u8, &'static str>) -> Values<'a, u8, &'new str> { v }
|
||||
fn drain<'new>(d: Drain<'static, &'static str, &'static str>)
|
||||
-> Drain<'new, &'new str, &'new str> { d }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
|
|
@ -1036,6 +1036,7 @@ fn assert_covariance() {
|
|||
-> Intersection<'a, &'new str, RandomState> { v }
|
||||
fn union<'a, 'new>(v: Union<'a, &'static str, RandomState>)
|
||||
-> Union<'a, &'new str, RandomState> { v }
|
||||
fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> { d }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ use marker;
|
|||
use mem::{align_of, size_of};
|
||||
use mem;
|
||||
use ops::{Deref, DerefMut};
|
||||
use ptr::{self, Unique};
|
||||
use ptr::{self, Unique, Shared};
|
||||
|
||||
use self::BucketState::*;
|
||||
|
||||
|
|
@ -754,7 +754,8 @@ impl<K, V> RawTable<K, V> {
|
|||
hashes_end: hashes_end,
|
||||
marker: marker::PhantomData,
|
||||
},
|
||||
table: self,
|
||||
table: unsafe { Shared::new(self) },
|
||||
marker: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -897,8 +898,9 @@ unsafe impl<K: Send, V: Send> Send for IntoIter<K, V> {}
|
|||
|
||||
/// Iterator over the entries in a table, clearing the table.
|
||||
pub struct Drain<'a, K: 'a, V: 'a> {
|
||||
table: &'a mut RawTable<K, V>,
|
||||
table: Shared<RawTable<K, V>>,
|
||||
iter: RawBuckets<'static, K, V>,
|
||||
marker: marker::PhantomData<&'a RawTable<K, V>>,
|
||||
}
|
||||
|
||||
unsafe impl<'a, K: Sync, V: Sync> Sync for Drain<'a, K, V> {}
|
||||
|
|
@ -973,8 +975,8 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> {
|
|||
#[inline]
|
||||
fn next(&mut self) -> Option<(SafeHash, K, V)> {
|
||||
self.iter.next().map(|bucket| {
|
||||
self.table.size -= 1;
|
||||
unsafe {
|
||||
(**self.table).size -= 1;
|
||||
(SafeHash { hash: ptr::replace(bucket.hash, EMPTY_BUCKET) },
|
||||
ptr::read(bucket.key),
|
||||
ptr::read(bucket.val))
|
||||
|
|
@ -983,13 +985,15 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> {
|
|||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
let size = self.table.size();
|
||||
let size = unsafe { (**self.table).size() };
|
||||
(size, Some(size))
|
||||
}
|
||||
}
|
||||
impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
|
||||
fn len(&self) -> usize {
|
||||
self.table.size()
|
||||
unsafe {
|
||||
(**self.table).size()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue