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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//! Data structures that can be compacted and separated in parallel.
//!
//! ### Examples
//!
//! ```
//! use fp_library::{
//! brands::*,
//! functions::*,
//! };
//!
//! let v = vec![Some(1), None, Some(3)];
//! let result: Vec<i32> = par_compact::<VecBrand, _>(v);
//! assert_eq!(result, vec![1, 3]);
//! ```
#[fp_macros::document_module]
mod inner {
use {
crate::{
brands::*,
kinds::*,
},
fp_macros::*,
};
/// A type class for data structures that can be compacted and separated in parallel.
///
/// `ParCompactable` is the parallel counterpart to [`Compactable`](crate::classes::Compactable).
/// Implementors define [`par_compact`][ParCompactable::par_compact] and
/// [`par_separate`][ParCompactable::par_separate] directly: there is no intermediate `Vec`
/// conversion imposed by the interface.
///
/// ### Thread Safety
///
/// All `par_*` functions require `Send` bounds on element types.
/// These bounds apply even when the `rayon` feature is disabled, so that code compiles
/// identically in both configurations.
///
/// **Note: The `rayon` feature must be enabled to use actual parallel execution. Without
/// it, all `par_*` functions fall back to equivalent sequential operations.**
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::VecBrand,
/// functions::*,
/// };
///
/// let v = vec![Some(1), None, Some(3)];
/// let result: Vec<i32> = par_compact::<VecBrand, _>(v);
/// assert_eq!(result, vec![1, 3]);
///
/// let v2: Vec<Result<i32, &str>> = vec![Ok(1), Err("e"), Ok(3)];
/// let (errs, oks): (Vec<&str>, Vec<i32>) = par_separate::<VecBrand, _, _>(v2);
/// assert_eq!(errs, vec!["e"]);
/// assert_eq!(oks, vec![1, 3]);
/// ```
#[kind(type Of<'a, A: 'a>: 'a;)]
pub trait ParCompactable {
/// Compacts a data structure of [`Option`]s in parallel, discarding [`None`] values and
/// keeping [`Some`] values.
///
/// When the `rayon` feature is enabled, elements are processed across multiple threads.
/// Otherwise falls back to sequential compaction.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The type of the elements in the [`Option`]."
)]
///
#[document_parameters("The data structure containing [`Option`] values.")]
///
#[document_returns(
"A new data structure containing only the values from the [`Some`] variants."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::VecBrand,
/// classes::par_compactable::ParCompactable,
/// };
///
/// let v = vec![Some(1), None, Some(3)];
/// let result: Vec<i32> = VecBrand::par_compact(v);
/// assert_eq!(result, vec![1, 3]);
/// ```
fn par_compact<'a, A: 'a + Send>(
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
'a,
Apply!(<OptionBrand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
>)
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>);
/// Separates a data structure of [`Result`]s into two data structures in parallel.
///
/// When the `rayon` feature is enabled, elements are processed across multiple threads.
/// Otherwise falls back to sequential separation.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The type of the error values.",
"The type of the success values."
)]
///
#[document_parameters("The data structure containing [`Result`] values.")]
///
#[document_returns(
"A pair of data structures: the first containing the [`Err`] values, and the second containing the [`Ok`] values."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::VecBrand,
/// classes::par_compactable::ParCompactable,
/// };
///
/// let v: Vec<Result<i32, &str>> = vec![Ok(1), Err("e"), Ok(3)];
/// let (errs, oks): (Vec<&str>, Vec<i32>) = VecBrand::par_separate(v);
/// assert_eq!(errs, vec!["e"]);
/// assert_eq!(oks, vec![1, 3]);
/// ```
fn par_separate<'a, E: 'a + Send, O: 'a + Send>(
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>)
) -> (
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
);
}
/// Compacts a data structure of [`Option`]s in parallel, discarding [`None`] values and
/// keeping [`Some`] values.
///
/// Free function version that dispatches to [`ParCompactable::par_compact`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The brand of the compactable structure.",
"The type of the elements in the [`Option`]."
)]
///
#[document_parameters("The data structure containing [`Option`] values.")]
///
#[document_returns(
"A new data structure containing only the values from the [`Some`] variants."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::*,
/// };
///
/// let v = vec![Some(1), None, Some(3)];
/// let result: Vec<i32> = par_compact::<VecBrand, _>(v);
/// assert_eq!(result, vec![1, 3]);
/// ```
pub fn par_compact<'a, Brand: ParCompactable, A: 'a + Send>(
fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
'a,
Apply!(<OptionBrand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
>)
) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
Brand::par_compact(fa)
}
/// Separates a data structure of [`Result`]s into two data structures in parallel.
///
/// Free function version that dispatches to [`ParCompactable::par_separate`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The brand of the compactable structure.",
"The type of the error values.",
"The type of the success values."
)]
///
#[document_parameters("The data structure containing [`Result`] values.")]
///
#[document_returns(
"A pair of data structures: the first containing the [`Err`] values, and the second containing the [`Ok`] values."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::*,
/// };
///
/// let v: Vec<Result<i32, &str>> = vec![Ok(1), Err("e"), Ok(3)];
/// let (errs, oks): (Vec<&str>, Vec<i32>) = par_separate::<VecBrand, _, _>(v);
/// assert_eq!(errs, vec!["e"]);
/// assert_eq!(oks, vec![1, 3]);
/// ```
pub fn par_separate<'a, Brand: ParCompactable, E: 'a + Send, O: 'a + Send>(
fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>)
) -> (
Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
) {
Brand::par_separate::<E, O>(fa)
}
}
pub use inner::*;