Skip to content

Commit 41dae31

Browse files
committed
Implement rayon functionality, feature flag
1 parent 1cc551f commit 41dae31

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ keywords = ["newtype", "alternative", "classification"]
99
categories = ["data-structures", "rust-patterns"]
1010

1111
[dependencies]
12+
rayon = { version = "1.5.1", optional = true }

src/lib.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ where
112112
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
113113
f.debug_struct("Usage")
114114
.field("data", &self.data)
115-
.field("_phantom", &format!("PhantomData<{}>", std::any::type_name::<U>()))
115+
.field(
116+
"_phantom",
117+
&format!("PhantomData<{}>", std::any::type_name::<U>()),
118+
)
116119
.finish()
117120
}
118121
}
@@ -129,11 +132,7 @@ where
129132
}
130133
}
131134

132-
impl<U, T> Copy for Usage<U, T>
133-
where
134-
T: Copy,
135-
{
136-
}
135+
impl<U, T> Copy for Usage<U, T> where T: Copy {}
137136

138137
impl<U, T> Clone for Usage<U, T>
139138
where
@@ -199,12 +198,31 @@ impl<U, T> From<T> for Usage<U, T> {
199198
}
200199
}
201200

202-
impl<U, T, V> FromIterator<V> for Usage<U, T> where T: FromIterator<V> {
201+
impl<U, T, V> FromIterator<V> for Usage<U, T>
202+
where
203+
T: FromIterator<V>,
204+
{
203205
fn from_iter<I: IntoIterator<Item = V>>(iter: I) -> Self {
204206
U::as_usage(iter.into_iter().collect())
205207
}
206208
}
207209

210+
#[cfg(feature = "rayon")]
211+
mod rayon_impl {
212+
use super::*;
213+
use rayon::iter::{ParallelIterator, FromParallelIterator};
214+
215+
impl<U, T, V> FromParallelIterator<V> for Usage<U, T>
216+
where
217+
T: FromParallelIterator<V>,
218+
V: Send,
219+
{
220+
fn from_par_iter<I: rayon::iter::IntoParallelIterator<Item = V>>(par_iter: I) -> Self {
221+
U::as_usage(par_iter.into_par_iter().collect())
222+
}
223+
}
224+
}
225+
208226
// Data access traits
209227
impl<U, T> Borrow<T> for Usage<U, T> {
210228
fn borrow(&self) -> &T {

0 commit comments

Comments
 (0)