Skip to content
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

Minimal indexmap support for rustc-rayon #14

Merged
merged 2 commits into from
Jan 18, 2025
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rustc-rayon"
version = "0.5.0"
version = "0.5.1"
authors = ["Niko Matsakis <niko@alum.mit.edu>",
"Josh Stone <cuviper@gmail.com>"]
description = "Simple work-stealing parallelism for Rust - fork for rustc"
Expand All @@ -22,6 +22,7 @@ exclude = ["ci"]
name = "rayon"

[dependencies]
indexmap = { version = "2", optional = true }
rustc-rayon-core = { version = "0.5", path = "rayon-core" }

# This is a public dependency!
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub mod vec;

mod math;
mod par_either;
mod par_indexmap;

mod compile_fail;

Expand Down
230 changes: 230 additions & 0 deletions src/par_indexmap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
#![cfg(feature = "indexmap")]
//! Minimal `indexmap` support for `rustc-rayon`

use crate::iter::plumbing::{bridge, Consumer, Producer, ProducerCallback, UnindexedConsumer};
use crate::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator};

mod map {
use super::*;
use indexmap::map::{IndexMap, Iter, IterMut, Slice};

impl<'a, K, V, S> IntoParallelIterator for &'a IndexMap<K, V, S>
where
K: Sync,
V: Sync,
{
type Item = (&'a K, &'a V);
type Iter = ParIter<'a, K, V>;

fn into_par_iter(self) -> Self::Iter {
ParIter {
slice: self.as_slice(),
}
}
}

#[derive(Debug)]
pub struct ParIter<'a, K, V> {
slice: &'a Slice<K, V>,
}

impl<'a, K: Sync, V: Sync> ParallelIterator for ParIter<'a, K, V> {
type Item = (&'a K, &'a V);

fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
bridge(self, consumer)
}

fn opt_len(&self) -> Option<usize> {
Some(self.slice.len())
}
}

impl<K: Sync, V: Sync> IndexedParallelIterator for ParIter<'_, K, V> {
fn drive<C>(self, consumer: C) -> C::Result
where
C: Consumer<Self::Item>,
{
bridge(self, consumer)
}

fn len(&self) -> usize {
self.slice.len()
}

fn with_producer<CB>(self, callback: CB) -> CB::Output
where
CB: ProducerCallback<Self::Item>,
{
callback.callback(IterProducer { slice: self.slice })
}
}

struct IterProducer<'a, K, V> {
slice: &'a Slice<K, V>,
}

impl<'a, K: Sync, V: Sync> Producer for IterProducer<'a, K, V> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;

fn into_iter(self) -> Self::IntoIter {
self.slice.iter()
}

fn split_at(self, index: usize) -> (Self, Self) {
let (left, right) = self.slice.split_at(index);
(Self { slice: left }, Self { slice: right })
}
}

impl<'a, K, V, S> IntoParallelIterator for &'a mut IndexMap<K, V, S>
where
K: Sync + Send,
V: Send,
{
type Item = (&'a K, &'a mut V);
type Iter = ParIterMut<'a, K, V>;

fn into_par_iter(self) -> Self::Iter {
ParIterMut {
slice: self.as_mut_slice(),
}
}
}

#[derive(Debug)]
pub struct ParIterMut<'a, K, V> {
slice: &'a mut Slice<K, V>,
}

impl<'a, K: Sync + Send, V: Send> ParallelIterator for ParIterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);

fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
bridge(self, consumer)
}

fn opt_len(&self) -> Option<usize> {
Some(self.slice.len())
}
}

impl<K: Sync + Send, V: Send> IndexedParallelIterator for ParIterMut<'_, K, V> {
fn drive<C>(self, consumer: C) -> C::Result
where
C: Consumer<Self::Item>,
{
bridge(self, consumer)
}

fn len(&self) -> usize {
self.slice.len()
}

fn with_producer<CB>(self, callback: CB) -> CB::Output
where
CB: ProducerCallback<Self::Item>,
{
callback.callback(IterMutProducer { slice: self.slice })
}
}

struct IterMutProducer<'a, K, V> {
slice: &'a mut Slice<K, V>,
}

impl<'a, K: Sync + Send, V: Send> Producer for IterMutProducer<'a, K, V> {
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;

fn into_iter(self) -> Self::IntoIter {
self.slice.iter_mut()
}

fn split_at(self, index: usize) -> (Self, Self) {
let (left, right) = self.slice.split_at_mut(index);
(Self { slice: left }, Self { slice: right })
}
}
}

mod set {
use super::*;
use indexmap::set::{IndexSet, Iter, Slice};

impl<'a, T: Sync, S> IntoParallelIterator for &'a IndexSet<T, S> {
type Item = &'a T;
type Iter = ParIter<'a, T>;

fn into_par_iter(self) -> Self::Iter {
ParIter {
slice: self.as_slice(),
}
}
}

#[derive(Debug)]
pub struct ParIter<'a, T> {
slice: &'a Slice<T>,
}

impl<'a, T: Sync> ParallelIterator for ParIter<'a, T> {
type Item = &'a T;

fn drive_unindexed<C>(self, consumer: C) -> C::Result
where
C: UnindexedConsumer<Self::Item>,
{
bridge(self, consumer)
}

fn opt_len(&self) -> Option<usize> {
Some(self.slice.len())
}
}

impl<T: Sync> IndexedParallelIterator for ParIter<'_, T> {
fn drive<C>(self, consumer: C) -> C::Result
where
C: Consumer<Self::Item>,
{
bridge(self, consumer)
}

fn len(&self) -> usize {
self.slice.len()
}

fn with_producer<CB>(self, callback: CB) -> CB::Output
where
CB: ProducerCallback<Self::Item>,
{
callback.callback(IterProducer { slice: self.slice })
}
}

struct IterProducer<'a, T> {
slice: &'a Slice<T>,
}

impl<'a, T: Sync> Producer for IterProducer<'a, T> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;

fn into_iter(self) -> Self::IntoIter {
self.slice.iter()
}

fn split_at(self, index: usize) -> (Self, Self) {
let (left, right) = self.slice.split_at(index);
(Self { slice: left }, Self { slice: right })
}
}
}
Loading