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
ParallelExtend
for tuples of references
#1089
Comments
Yes, since (I feel like the blanket impl must have been discussed somewhere, but I can't find it...) |
For now, as a workaround, I'm using a simple wrapper like this: /// Adds method `by_ref()` to `ParallelExtend` that returns a wrapper around `&mut A`
/// that "inherits" the implementation of `ParallelExtend` from `A`.
pub trait ParExtendByRefTrait<T: Send>: ParallelExtend<T> {
fn by_ref(&mut self) -> ExtendRef<'_, T, Self> {
ExtendRef::new(self)
}
}
impl<T: Send, A: ParallelExtend<T> + ?Sized> ParExtendByRefTrait<T> for A {}
/// A wrapper around `&mut A` that "inherits" the implementation of `ParallelExtend` from `A`.
///
/// The type parameter `T` is necessary for Rust to infer which implementation of `by_ref()` to use.
pub struct ExtendRef<'a, T, A: ?Sized> {
inner: &'a mut A,
_phantom: PhantomData<T>,
}
impl<'a, T, A: ?Sized> ExtendRef<'a, T, A> {
pub fn new(inner: &'a mut A) -> Self {
Self {
inner,
_phantom: PhantomData,
}
}
}
impl<'a, T: Send, A: ParallelExtend<T> + ?Sized> ParallelExtend<T> for ExtendRef<'a, T, A> {
fn par_extend<I>(&mut self, par_iter: I)
where
I: IntoParallelIterator<Item = T>,
{
self.inner.par_extend(par_iter)
}
} That can later used like this: (vec1.by_ref(), vec2.by_ref()).par_extend(...) Do you think it could make sense to add a similar |
I think Maybe the wrapper should just be a tuple struct, Or if this is really only useful for tuples, then something like |
ParallelExtend
implemented for tuples allows extending multiple collections from oneParallelIterator
, which is really useful. This code works as expected:However, the way it is implemented now, the two collections actually have to be stored as a tuple. This code doesn't compile:
This looks like a natural use-case and I encountered it at least a couple of times while using
rayon
. Am I missing some other tools to implement what I want? If not, would it be possible to add support for it? Perhaps, with a slightly different API as implementingParallelExtend
for(&mut FromA, &mut FromB)
would probably conflict with the original implementation for(FromA, FromB)
.The text was updated successfully, but these errors were encountered: