mm1_sup/common/
child_spec.rs1use std::time::Duration;
2
3#[derive(Debug)]
4pub struct ChildSpec<F, T> {
5 pub launcher: F,
6 pub child_type: T,
7 pub init_type: InitType,
8 pub stop_timeout: Duration,
9 pub announce_parent: bool,
10}
11
12#[derive(Debug, Clone, Copy)]
13pub enum InitType {
14 NoAck,
15 WithAck { start_timeout: Duration },
16}
17
18impl<F> ChildSpec<F, ()> {
19 pub fn new(launcher: F) -> Self {
20 Self {
21 launcher,
22 child_type: (),
23 init_type: InitType::NoAck,
24 stop_timeout: Duration::from_secs(1),
25 announce_parent: false,
26 }
27 }
28}
29
30impl<F, T> ChildSpec<F, T> {
31 pub fn with_child_type<T1>(self, child_type: T1) -> ChildSpec<F, T1> {
32 let Self {
33 launcher,
34 child_type: _,
35 init_type,
36 stop_timeout,
37 announce_parent,
38 } = self;
39 ChildSpec {
40 launcher,
41 child_type,
42 init_type,
43 stop_timeout,
44 announce_parent,
45 }
46 }
47
48 pub fn with_init_type(self, init_type: InitType) -> Self {
49 Self { init_type, ..self }
50 }
51
52 pub fn with_stop_timeout(self, stop_timeout: Duration) -> Self {
53 Self {
54 stop_timeout,
55 ..self
56 }
57 }
58
59 pub fn with_announce_parent(self, announce_parent: bool) -> Self {
60 Self {
61 announce_parent,
62 ..self
63 }
64 }
65
66 pub fn announce_parent(self) -> Self {
67 self.with_announce_parent(true)
68 }
69
70 pub fn map_launcher<F1, M>(self, map: M) -> ChildSpec<F1, T>
71 where
72 M: FnOnce(F) -> F1,
73 {
74 let Self {
75 launcher: factory,
76 child_type,
77 init_type,
78 stop_timeout,
79 announce_parent,
80 } = self;
81 ChildSpec {
82 launcher: map(factory),
83 child_type,
84 init_type,
85 stop_timeout,
86 announce_parent,
87 }
88 }
89}
90
91impl<F, T> Clone for ChildSpec<F, T>
92where
93 F: Clone,
94 T: Clone,
95{
96 fn clone(&self) -> Self {
97 Self {
98 launcher: self.launcher.clone(),
99 child_type: self.child_type.clone(),
100 init_type: self.init_type,
101 stop_timeout: self.stop_timeout,
102 announce_parent: self.announce_parent,
103 }
104 }
105}