zestors/actor_reference/
child_type.rs

1#[allow(unused)]
2use crate::all::*;
3use tokio::task::JoinHandle;
4
5/// The parameter `C` in a [`Child<_, _, C>`] that specifies what kind of child it is:
6/// - [`SingleProcess`] -> The actor consists of a single process: [`Child<_, _>`].
7/// - [`MultiProcess`] -> The actor consists of multiple processes: [`ChildPool<_, _>`].
8pub trait ChildType {
9    type JoinHandles<E: Send + 'static>: Send + 'static;
10    fn abort<E: Send + 'static>(handles: &Self::JoinHandles<E>);
11    fn is_finished<E: Send + 'static>(handles: &Self::JoinHandles<E>) -> bool;
12}
13
14/// The default [`ChildType`].
15#[derive(Debug)]
16pub struct SingleProcess;
17
18impl ChildType for SingleProcess {
19    type JoinHandles<E: Send + 'static> = JoinHandle<E>;
20
21    fn abort<E: Send + 'static>(handles: &Self::JoinHandles<E>) {
22        handles.abort()
23    }
24
25    fn is_finished<E: Send + 'static>(handles: &Self::JoinHandles<E>) -> bool {
26        handles.is_finished()
27    }
28}
29
30/// The pooled [`ChildType`].
31#[derive(Debug)]
32pub struct MultiProcess;
33
34impl ChildType for MultiProcess {
35    type JoinHandles<E: Send + 'static> = Vec<JoinHandle<E>>;
36
37    fn abort<E: Send + 'static>(handles: &Self::JoinHandles<E>) {
38        for handle in handles {
39            handle.abort()
40        }
41    }
42
43    fn is_finished<E: Send + 'static>(handles: &Self::JoinHandles<E>) -> bool {
44        handles.iter().all(|handle| handle.is_finished())
45    }
46}