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
//! The way in which a task is executed.
use crate::task::{Task, TaskOutput};
pub mod runtime;
/// An abstract execution mode. Represents various ways in which code executes,
/// such as in a single thread, on a thread pool, or on an async runtime.
pub trait Mode<'src> {
/// The output of this mode for a type.
type Output<O>: Send
where
O: Send + 'src;
/// Execute the given task with this mode.
fn invoke<I, O, T>(&'src self, task: T, input: I) -> TaskOutput<'src, Self, O>
where
T: Task<'src, I, O>,
O: Send + 'src;
/// Bind the result of the closure into an output, preferring to execute the
/// closure on a blocking thread pool if available.
fn spawn_blocking<O>(&self, f: impl FnOnce() -> O + Send + 'src) -> Self::Output<O>
where
O: Send + 'src;
/// Bind the result of the closure into an output, preferring to execute the
/// closure on a work-stealing thread pool if available.
fn spawn_cpu<O>(&self, f: impl FnOnce() -> O + Send + 'src) -> Self::Output<O>
where
O: Send + 'src;
/// Bind the result of the future into an output, preferring to execute the
/// future on an async runtime if the Mode provides one.
fn spawn_io<O, Fut>(&self, f: impl FnOnce() -> Fut + Send + 'src) -> Self::Output<O>
where
O: Send + 'src,
Fut: Future<Output = O> + Send + 'src;
/// Dispatch multiple [`Output`](Self::Output), potentially in parallel.
fn join<AO, BO>(
&'src self,
a: impl FnOnce(&'src Self) -> Self::Output<AO> + Send + 'src,
b: impl FnOnce(&'src Self) -> Self::Output<BO> + Send + 'src,
) -> Self::Output<(AO, BO)>
where
AO: Send + 'src,
BO: Send + 'src;
/// Given an [`Output`](Self::Output), apply the closure and generate a new
/// output.
fn then<I, O>(
&'src self,
input: Self::Output<I>,
f: impl FnOnce(&'src Self, I) -> Self::Output<O> + Send + 'src,
) -> Self::Output<O>
where
I: Send + 'src,
O: Send + 'src;
/// Given an [`Output`](Self::Output) wrapped in a [`Result`], apply the
/// closure only if the Result is [`Ok`] and generate a new output.
fn map_result<I, E, O>(
&'src self,
input: Self::Output<Result<I, E>>,
f: impl FnOnce(&'src Self, I) -> Self::Output<O> + Send + 'src,
) -> Self::Output<Result<O, E>>
where
I: Send + 'src,
E: Send + 'src,
O: Send + 'src;
/// Given an [`Output`](Self::Output) wrapped in an [`Option`], apply the
/// closure only if the Option is [`Some`] and generate a new output.
fn map_option<I, O>(
&'src self,
input: Self::Output<Option<I>>,
f: impl FnOnce(&'src Self, I) -> Self::Output<O> + Send + 'src,
) -> Self::Output<Option<O>>
where
I: Send + 'src,
O: Send + 'src;
/// TODO doc iter
fn map_iterator<I, O, II>(
&'src self,
input: Self::Output<II>,
f: impl Fn(&'src Self, I) -> Self::Output<O> + Send + 'src,
) -> Self::Output<impl Iterator<Item = O> + Send + 'src>
where
O: Send + 'src,
II: Iterator<Item = I> + Send + 'src;
/// Given an [`Output`](Self::Output) wrapped in a [`Result`], apply the
/// closure only if the Result is [`Ok`] and generate a new [`Result`]
/// output.
fn and_then_result<I, E, O>(
&'src self,
input: Self::Output<Result<I, E>>,
f: impl FnOnce(&'src Self, I) -> Self::Output<Result<O, E>> + Send + 'src,
) -> Self::Output<Result<O, E>>
where
I: Send + 'src,
E: Send + 'src,
O: Send + 'src;
/// Given an [`Output`](Self::Output) wrapped in a [`Result`], apply the
/// closure only if the Result is [`Ok`] and generate a new [`Result`]
/// output.
fn and_then_option<I, O>(
&'src self,
input: Self::Output<Option<I>>,
f: impl FnOnce(&'src Self, I) -> Self::Output<Option<O>> + Send + 'src,
) -> Self::Output<Option<O>>
where
I: Send + 'src,
O: Send + 'src;
}