Skip to content

Send sync implementation on iterators #27615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/libstd/collections/hash/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,9 @@ pub struct Iter<'a, K: 'a, V: 'a> {
elems_left: usize,
}

unsafe impl<'a, K: Sync, V: Sync> Sync for Iter<'a, K, V> {}
unsafe impl<'a, K: Sync, V: Sync> Send for Iter<'a, K, V> {}

// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
impl<'a, K, V> Clone for Iter<'a, K, V> {
fn clone(&self) -> Iter<'a, K, V> {
Expand All @@ -835,18 +838,29 @@ pub struct IterMut<'a, K: 'a, V: 'a> {
elems_left: usize,
}

unsafe impl<'a, K: Sync, V: Sync> Sync for IterMut<'a, K, V> {}
// Both K: Sync and K: Send are correct for IterMut's Send impl,
// but Send is the more useful bound
unsafe impl<'a, K: Send, V: Send> Send for IterMut<'a, K, V> {}

/// Iterator over the entries in a table, consuming the table.
pub struct IntoIter<K, V> {
table: RawTable<K, V>,
iter: RawBuckets<'static, K, V>
}

unsafe impl<K: Sync, V: Sync> Sync for IntoIter<K, V> {}
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>,
iter: RawBuckets<'static, K, V>,
}

unsafe impl<'a, K: Sync, V: Sync> Sync for Drain<'a, K, V> {}
unsafe impl<'a, K: Send, V: Send> Send for Drain<'a, K, V> {}

impl<'a, K, V> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);

Expand Down
9 changes: 9 additions & 0 deletions src/test/run-pass/sync-send-iterators-in-libcollections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use collections::String;
use collections::Vec;
use collections::VecDeque;
use collections::VecMap;
use std::collections::HashMap;
use std::collections::HashSet;

use collections::Bound::Included;
use collections::enum_set::CLike;
Expand Down Expand Up @@ -77,6 +79,13 @@ fn main() {
is_sync_send!(BTreeSet::<usize>::new(), intersection(&BTreeSet::<usize>::new()));
is_sync_send!(BTreeSet::<usize>::new(), union(&BTreeSet::<usize>::new()));

all_sync_send!(HashMap::<usize, usize>::new(), iter, iter_mut, drain, into_iter, keys, values);
all_sync_send!(HashSet::<usize>::new(), iter, drain, into_iter);
is_sync_send!(HashSet::<usize>::new(), difference(&HashSet::<usize>::new()));
is_sync_send!(HashSet::<usize>::new(), symmetric_difference(&HashSet::<usize>::new()));
is_sync_send!(HashSet::<usize>::new(), intersection(&HashSet::<usize>::new()));
is_sync_send!(HashSet::<usize>::new(), union(&HashSet::<usize>::new()));

all_sync_send!(LinkedList::<usize>::new(), iter, iter_mut, into_iter);

#[derive(Copy, Clone)]
Expand Down