Skip to content

Commit 542d56e

Browse files
committed
Auto merge of #27615 - GuillaumeGomez:send_sync, r=huonw
Part of #22709. cc @Veedrac r? @bluss
2 parents 58b0aa5 + f2f4a5c commit 542d56e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/libstd/collections/hash/table.rs

+14
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,9 @@ pub struct Iter<'a, K: 'a, V: 'a> {
818818
elems_left: usize,
819819
}
820820

821+
unsafe impl<'a, K: Sync, V: Sync> Sync for Iter<'a, K, V> {}
822+
unsafe impl<'a, K: Sync, V: Sync> Send for Iter<'a, K, V> {}
823+
821824
// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
822825
impl<'a, K, V> Clone for Iter<'a, K, V> {
823826
fn clone(&self) -> Iter<'a, K, V> {
@@ -835,18 +838,29 @@ pub struct IterMut<'a, K: 'a, V: 'a> {
835838
elems_left: usize,
836839
}
837840

841+
unsafe impl<'a, K: Sync, V: Sync> Sync for IterMut<'a, K, V> {}
842+
// Both K: Sync and K: Send are correct for IterMut's Send impl,
843+
// but Send is the more useful bound
844+
unsafe impl<'a, K: Send, V: Send> Send for IterMut<'a, K, V> {}
845+
838846
/// Iterator over the entries in a table, consuming the table.
839847
pub struct IntoIter<K, V> {
840848
table: RawTable<K, V>,
841849
iter: RawBuckets<'static, K, V>
842850
}
843851

852+
unsafe impl<K: Sync, V: Sync> Sync for IntoIter<K, V> {}
853+
unsafe impl<K: Send, V: Send> Send for IntoIter<K, V> {}
854+
844855
/// Iterator over the entries in a table, clearing the table.
845856
pub struct Drain<'a, K: 'a, V: 'a> {
846857
table: &'a mut RawTable<K, V>,
847858
iter: RawBuckets<'static, K, V>,
848859
}
849860

861+
unsafe impl<'a, K: Sync, V: Sync> Sync for Drain<'a, K, V> {}
862+
unsafe impl<'a, K: Send, V: Send> Send for Drain<'a, K, V> {}
863+
850864
impl<'a, K, V> Iterator for Iter<'a, K, V> {
851865
type Item = (&'a K, &'a V);
852866

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

+9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use collections::String;
2525
use collections::Vec;
2626
use collections::VecDeque;
2727
use collections::VecMap;
28+
use std::collections::HashMap;
29+
use std::collections::HashSet;
2830

2931
use collections::Bound::Included;
3032
use collections::enum_set::CLike;
@@ -77,6 +79,13 @@ fn main() {
7779
is_sync_send!(BTreeSet::<usize>::new(), intersection(&BTreeSet::<usize>::new()));
7880
is_sync_send!(BTreeSet::<usize>::new(), union(&BTreeSet::<usize>::new()));
7981

82+
all_sync_send!(HashMap::<usize, usize>::new(), iter, iter_mut, drain, into_iter, keys, values);
83+
all_sync_send!(HashSet::<usize>::new(), iter, drain, into_iter);
84+
is_sync_send!(HashSet::<usize>::new(), difference(&HashSet::<usize>::new()));
85+
is_sync_send!(HashSet::<usize>::new(), symmetric_difference(&HashSet::<usize>::new()));
86+
is_sync_send!(HashSet::<usize>::new(), intersection(&HashSet::<usize>::new()));
87+
is_sync_send!(HashSet::<usize>::new(), union(&HashSet::<usize>::new()));
88+
8089
all_sync_send!(LinkedList::<usize>::new(), iter, iter_mut, into_iter);
8190

8291
#[derive(Copy, Clone)]

0 commit comments

Comments
 (0)