Skip to content

Commit a4e4233

Browse files
Add Send/Sync traits on Iter struct in hash/table
1 parent a5d33d8 commit a4e4233

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/libstd/collections/hash/set.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use borrow::Borrow;
1212
use clone::Clone;
1313
use cmp::{Eq, PartialEq};
14-
use core::marker::Sized;
14+
use core::marker::{Sized, Send, Sync};
1515
use default::Default;
1616
use fmt::Debug;
1717
use fmt;
@@ -764,18 +764,27 @@ pub struct Iter<'a, K: 'a> {
764764
iter: Keys<'a, K, ()>
765765
}
766766

767+
unsafe impl<'a, K: Send> Send for Iter<'a, K> {}
768+
unsafe impl<'a, K: Sync> Sync for Iter<'a, K> {}
769+
767770
/// HashSet move iterator
768771
#[stable(feature = "rust1", since = "1.0.0")]
769772
pub struct IntoIter<K> {
770773
iter: Map<map::IntoIter<K, ()>, fn((K, ())) -> K>
771774
}
772775

776+
unsafe impl<K: Send> Send for IntoIter<K> {}
777+
unsafe impl<K: Sync> Sync for IntoIter<K> {}
778+
773779
/// HashSet drain iterator
774780
#[stable(feature = "rust1", since = "1.0.0")]
775781
pub struct Drain<'a, K: 'a> {
776782
iter: Map<map::Drain<'a, K, ()>, fn((K, ())) -> K>,
777783
}
778784

785+
unsafe impl<'a, K: Send> Send for Drain<'a, K> {}
786+
unsafe impl<'a, K: Sync> Sync for Drain<'a, K> {}
787+
779788
/// Intersection iterator
780789
#[stable(feature = "rust1", since = "1.0.0")]
781790
pub struct Intersection<'a, T: 'a, S: 'a> {
@@ -785,6 +794,9 @@ pub struct Intersection<'a, T: 'a, S: 'a> {
785794
other: &'a HashSet<T, S>,
786795
}
787796

797+
unsafe impl<'a, K: Send, S: Send> Send for Intersection<'a, K, S> {}
798+
unsafe impl<'a, K: Sync, S: Send> Sync for Intersection<'a, K, S> {}
799+
788800
/// Difference iterator
789801
#[stable(feature = "rust1", since = "1.0.0")]
790802
pub struct Difference<'a, T: 'a, S: 'a> {
@@ -794,18 +806,27 @@ pub struct Difference<'a, T: 'a, S: 'a> {
794806
other: &'a HashSet<T, S>,
795807
}
796808

809+
unsafe impl<'a, K: Send, S: Send> Send for Difference<'a, K, S> {}
810+
unsafe impl<'a, K: Sync, S: Send> Sync for Difference<'a, K, S> {}
811+
797812
/// Symmetric difference iterator.
798813
#[stable(feature = "rust1", since = "1.0.0")]
799814
pub struct SymmetricDifference<'a, T: 'a, S: 'a> {
800815
iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>
801816
}
802817

818+
unsafe impl<'a, K: Send, S: Send> Send for SymmetricDifference<'a, K, S> {}
819+
unsafe impl<'a, K: Sync, S: Send> Sync for SymmetricDifference<'a, K, S> {}
820+
803821
/// Set union iterator.
804822
#[stable(feature = "rust1", since = "1.0.0")]
805823
pub struct Union<'a, T: 'a, S: 'a> {
806824
iter: Chain<Iter<'a, T>, Difference<'a, T, S>>
807825
}
808826

827+
unsafe impl<'a, K: Send, S: Send> Send for Union<'a, K, S> {}
828+
unsafe impl<'a, K: Sync, S: Send> Sync for Union<'a, K, S> {}
829+
809830
#[stable(feature = "rust1", since = "1.0.0")]
810831
impl<'a, T, S> IntoIterator for &'a HashSet<T, S>
811832
where T: Eq + Hash, S: HashState

src/libstd/collections/hash/table.rs

+3
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,9 @@ struct RawBuckets<'a, K, V> {
741741
marker: marker::PhantomData<&'a ()>,
742742
}
743743

744+
unsafe impl<'a, K: Send, V: Send> Send for RawBuckets<'a, K, V> {}
745+
unsafe impl<'a, K: Sync, V: Sync> Sync for RawBuckets<'a, K, V> {}
746+
744747
// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
745748
impl<'a, K, V> Clone for RawBuckets<'a, K, V> {
746749
fn clone(&self) -> RawBuckets<'a, K, V> {

src/test/run-pass/sync-send-iterators-in-libcollections.rs

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use collections::String;
2525
use collections::Vec;
2626
use collections::VecDeque;
2727
use collections::VecMap;
28+
use std::collections::HashMap;
2829

2930
use collections::Bound::Included;
3031
use collections::enum_set::CLike;
@@ -77,6 +78,8 @@ fn main() {
7778
is_sync_send!(BTreeSet::<usize>::new(), intersection(&BTreeSet::<usize>::new()));
7879
is_sync_send!(BTreeSet::<usize>::new(), union(&BTreeSet::<usize>::new()));
7980

81+
all_sync_send!(HashMap::<usize, usize>::new(), keys, values, iter, iter_mut);
82+
8083
all_sync_send!(LinkedList::<usize>::new(), iter, iter_mut, into_iter);
8184

8285
#[derive(Copy, Clone)]

0 commit comments

Comments
 (0)