1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
use crate::Par;
/// Transforms a parallel iterator yielding &T into one that yields T by cloning each element.
///
/// Transformation is via the `cloned` method.
///
/// # Examples
/// ```rust
/// use orx_parallel::*;
///
/// fn warn(mut name: String) -> String {
/// name.push('!');
/// name
/// }
///
/// let names = vec![String::from("john"), String::from("doe")];
///
/// let new_names = names.par().cloned().map(warn).collect_vec();
///
/// assert_eq!(new_names, &[String::from("john!"), String::from("doe!")]);
/// ```
pub trait ParIntoCloned<'a, T>: Par<Item = &'a T>
where
T: Send + Sync + Clone + 'a,
{
/// Transforms a parallel iterator yielding &T into one that yields T by cloning each element.
///
/// Transformation is via the `cloned` method.
///
/// # Examples
/// ```rust
/// use orx_parallel::*;
///
/// fn warn(mut name: String) -> String {
/// name.push('!');
/// name
/// }
///
/// let names = vec![String::from("john"), String::from("doe")];
///
/// let new_names = names.par().cloned().map(warn).collect_vec();
///
/// assert_eq!(new_names, &[String::from("john!"), String::from("doe!")]);
/// ```
fn cloned(self) -> impl Par<Item = T> {
self.map(|x| x.clone())
}
}
impl<'a, T, P> ParIntoCloned<'a, T> for P
where
T: Send + Sync + Clone + 'a,
P: Par<Item = &'a T>,
{
}
/// Transforms a parallel iterator yielding &T into one that yields T by copying each element.
///
/// Transformation is via the `copied` method.
///
/// # Examples
/// ```rust
/// use orx_parallel::*;
///
/// let numbers = vec![1, 2, 3, 4];
///
/// let sum = numbers.par().copied().sum();
/// let product = numbers.par().copied().fold(|| 1, |x, y| x * y);
///
/// assert_eq!(sum, 10);
/// assert_eq!(product, 24);
/// ```
pub trait ParIntoCopied<'a, T>: Par<Item = &'a T>
where
T: Send + Sync + Copy + 'a,
{
/// Transforms a parallel iterator yielding &T into one that yields T by copying each element.
///
/// Transformation is via the `copied` method.
///
/// # Examples
/// ```rust
/// use orx_parallel::*;
///
/// let numbers = vec![1, 2, 3, 4];
///
/// let sum = numbers.par().copied().sum();
/// let product = numbers.par().copied().fold(|| 1, |x, y| x * y);
///
/// assert_eq!(sum, 10);
/// assert_eq!(product, 24);
/// ```
fn copied(self) -> impl Par<Item = T> {
self.map(|x| *x)
}
}
impl<'a, T, P> ParIntoCopied<'a, T> for P
where
T: Send + Sync + Copy + 'a,
P: Par<Item = &'a T>,
{
}