Skip to content

Commit 8c65dba

Browse files
committed
wip: move smart_iter to its own module
1 parent 988b0b2 commit 8c65dba

File tree

3 files changed

+51
-48
lines changed

3 files changed

+51
-48
lines changed

src/core/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub(crate) mod common;
44
pub mod owner_resolver;
55
pub(crate) mod parse;
66
pub mod parser;
7+
pub(crate) mod smart_iter;
78
pub mod tag_resolver;
89
pub mod types;
910

src/core/owner_resolver.rs

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::smart_iter::SmartIter;
12
use crate::utils::error::{Error, Result};
23
use ignore::overrides::{Override, OverrideBuilder};
34
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
@@ -401,51 +402,3 @@ mod tests {
401402
assert_eq!(result[0].identifier, "@rust-team");
402403
}
403404
}
404-
405-
trait SmartIter<T: Send + Sync> {
406-
fn smart_iter(&self, n: usize) -> SmartIterator<T>;
407-
}
408-
409-
impl<'a, T: Send + Sync> SmartIter<T> for [T] {
410-
fn smart_iter(&self, n: usize) -> SmartIterator<T> {
411-
if self.len() <= n {
412-
SmartIterator::Sequential(self.iter())
413-
} else {
414-
SmartIterator::Parallel(self.par_iter())
415-
}
416-
}
417-
}
418-
419-
enum SmartIterator<'a, T: Send + Sync> {
420-
Sequential(std::slice::Iter<'a, T>),
421-
Parallel(rayon::slice::Iter<'a, T>),
422-
}
423-
424-
enum SmartFilterMap<'a, T: Send + Sync, F> {
425-
Parallel(rayon::iter::FilterMap<rayon::slice::Iter<'a, T>, F>),
426-
Sequential(std::iter::FilterMap<std::slice::Iter<'a, T>, F>),
427-
}
428-
429-
impl<'a, T: Send + Sync> SmartIterator<'a, T> {
430-
fn filter_map<B: Send + Sync, F>(self, f: F) -> SmartFilterMap<'a, T, F>
431-
where
432-
F: Fn(&'a T) -> Option<B> + Send + Sync,
433-
{
434-
match self {
435-
SmartIterator::Parallel(iter) => SmartFilterMap::Parallel(iter.filter_map(f)),
436-
SmartIterator::Sequential(iter) => SmartFilterMap::Sequential(iter.filter_map(f)),
437-
}
438-
}
439-
}
440-
441-
impl<'a, T: Send + Sync, B: Send + Sync, F> SmartFilterMap<'a, T, F>
442-
where
443-
F: Fn(&'a T) -> Option<B> + Send + Sync,
444-
{
445-
fn collect(self) -> Vec<B> {
446-
match self {
447-
SmartFilterMap::Parallel(iter) => iter.collect(),
448-
SmartFilterMap::Sequential(iter) => iter.collect(),
449-
}
450-
}
451-
}

src/core/smart_iter.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
2+
3+
pub(crate) trait SmartIter<T: Send + Sync> {
4+
fn smart_iter(&self, n: usize) -> SmartIterator<T>;
5+
}
6+
7+
impl<T: Send + Sync> SmartIter<T> for [T] {
8+
fn smart_iter(&self, n: usize) -> SmartIterator<T> {
9+
if self.len() <= n {
10+
SmartIterator::Sequential(self.iter())
11+
} else {
12+
SmartIterator::Parallel(self.par_iter())
13+
}
14+
}
15+
}
16+
17+
pub(crate) enum SmartIterator<'a, T: Send + Sync> {
18+
Sequential(std::slice::Iter<'a, T>),
19+
Parallel(rayon::slice::Iter<'a, T>),
20+
}
21+
22+
pub(crate) enum SmartFilterMap<'a, T: Send + Sync, F> {
23+
Parallel(rayon::iter::FilterMap<rayon::slice::Iter<'a, T>, F>),
24+
Sequential(std::iter::FilterMap<std::slice::Iter<'a, T>, F>),
25+
}
26+
27+
impl<'a, T: Send + Sync> SmartIterator<'a, T> {
28+
pub(crate) fn filter_map<B: Send + Sync, F>(self, f: F) -> SmartFilterMap<'a, T, F>
29+
where
30+
F: Fn(&'a T) -> Option<B> + Send + Sync,
31+
{
32+
match self {
33+
SmartIterator::Parallel(iter) => SmartFilterMap::Parallel(iter.filter_map(f)),
34+
SmartIterator::Sequential(iter) => SmartFilterMap::Sequential(iter.filter_map(f)),
35+
}
36+
}
37+
}
38+
39+
impl<'a, T: Send + Sync, B: Send + Sync, F> SmartFilterMap<'a, T, F>
40+
where
41+
F: Fn(&'a T) -> Option<B> + Send + Sync,
42+
{
43+
pub(crate) fn collect(self) -> Vec<B> {
44+
match self {
45+
SmartFilterMap::Parallel(iter) => iter.collect(),
46+
SmartFilterMap::Sequential(iter) => iter.collect(),
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)