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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use std::task::Waker;
use observed::Sink;
use crate::{Executor, ExecutorCore};
/// Builds an instance of [`Executor`].
#[derive(Debug)]
#[must_use]
pub struct ExecutorBuilder {
owner_waker: Waker,
sink: Sink,
}
impl ExecutorBuilder {
/// Creates a new [`ExecutorBuilder`] with default settings.
pub fn new() -> Self {
Self {
owner_waker: Waker::noop().clone(),
sink: Sink::noop(),
}
}
/// The waker that will wake up the executor's owner.
///
/// The `owner_waker` is used by the executor to wake up its owner when more work has
/// arrived for the executor and calling [`execute_cycle()`][Executor::execute_cycle]
/// is desirable.
///
/// The default is not to wake the owner even if there is more work the executor could do.
pub fn owner_waker(mut self, owner_waker: Waker) -> Self {
self.owner_waker = owner_waker;
self
}
/// Configures the observed sink used for task lifecycle events.
///
/// The default sink is a no-op.
pub fn sink(mut self, sink: Sink) -> Self {
self.sink = sink;
self
}
/// Builds the executor with the configured settings.
#[must_use]
pub fn build(self) -> Executor {
Executor::new(ExecutorCore::new(self.owner_waker, self.sink))
}
}
impl Default for ExecutorBuilder {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_builder() {
_ = ExecutorBuilder::default();
}
}