-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathpar_methods.rs
More file actions
101 lines (97 loc) · 3.25 KB
/
par_methods.rs
File metadata and controls
101 lines (97 loc) · 3.25 KB
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
use super::{ConversionError, Reflection};
use crate::{data_tree::DataTree, size};
use orx_parallel::*;
use std::{ffi::OsStr, iter::once};
impl<Name, Size> Reflection<Name, Size>
where
Name: Send + Sync,
Size: size::Size + Send + Sync,
{
/// Attempting to convert a [`Reflection`] into a valid [`DataTree`].
pub fn par_try_into_tree(self) -> Result<DataTree<Name, Size>, ConversionError<Name, Size>> {
let Reflection {
name,
size,
children,
} = self;
let excess_child = children
.iter()
.enumerate()
.find(|(_, child)| child.size > size);
if let Some((index, _)) = excess_child {
let path = once(name).collect();
let child = keep_one(children, index).expect("excess child");
return Err(ConversionError::ExcessiveChildren { path, size, child });
}
let children: Result<Vec<_>, _> = children
.into_par()
.map(Self::par_try_into_tree)
.collect::<Vec<Result<_, _>>>() // TODO: request orx-parallel to make collecting Result possible
.into_iter()
.collect();
let children = match children {
Ok(children) => children,
Err(ConversionError::ExcessiveChildren {
mut path,
size,
child,
}) => {
path.push_front(name);
return Err(ConversionError::ExcessiveChildren { path, size, child });
}
};
Ok(DataTree {
name,
size,
children,
})
}
/// Attempt to transform names and sizes.
pub fn par_try_map<TargetName, TargetSize, Error, Transform>(
self,
transform: Transform,
) -> Result<Reflection<TargetName, TargetSize>, Error>
where
TargetName: Send + Sync,
TargetSize: size::Size + Send + Sync,
Error: Send + Sync,
Transform: Fn(Name, Size) -> Result<(TargetName, TargetSize), Error> + Copy + Sync,
{
let Reflection {
name,
size,
children,
} = self;
let children = children
.into_par()
.map(|child| child.par_try_map(transform))
.collect::<Vec<Result<_, _>>>() // TODO: request orx-parallel to make collecting Result possible
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
let (name, size) = transform(name, size)?;
Ok(Reflection {
name,
size,
children,
})
}
/// Attempt to convert all names from `OsString` to `String`.
pub fn par_convert_names_to_utf8(self) -> Result<Reflection<String, Size>, Name>
where
Name: AsRef<OsStr>,
Size: Sync,
{
self.par_try_map(|name, size| {
name.as_ref()
.to_str()
.map(|name| (name.to_string(), size))
.ok_or(name)
})
}
}
/// Extract an item at `index` if it exists. Then drop all remaining items.
#[inline]
fn keep_one<Item>(vec: Vec<Item>, index: usize) -> Option<Item> {
// Worry not about performance, for `std::vec::IntoIter::advanced_by` is overridden with O(1) algorithm!
vec.into_iter().nth(index)
}