mm1_sup/mixed/
spec_builder.rs

1use std::sync::Arc;
2
3use crate::common::factory::ActorFactory;
4use crate::mixed::{ChildType, ErasedActorFactory, MixedSup};
5type ChildSpec<F> = crate::common::child_spec::ChildSpec<F, ChildType>;
6
7pub trait Append<K, F> {
8    type Out;
9    fn append(self, key: K, child_spec: ChildSpec<F>) -> Self::Out;
10}
11
12pub trait CollectInto<K, R> {
13    fn collect_into(self, into: &mut impl Extend<(K, ChildSpec<ErasedActorFactory<R>>)>);
14}
15
16pub struct KV<K, R> {
17    key:        K,
18    child_spec: ChildSpec<ErasedActorFactory<R>>,
19}
20
21impl<K, F, T> Append<K, F> for T
22where
23    F: ActorFactory<Args = ()>,
24{
25    type Out = (Self, KV<K, F::Runnable>);
26
27    fn append(self, key: K, child_spec: ChildSpec<F>) -> Self::Out {
28        let child_spec = child_spec.map_launcher::<ErasedActorFactory<_>, _>(|f| Arc::pin(f));
29        (self, KV { key, child_spec })
30    }
31}
32
33impl<K, R> CollectInto<K, R> for () {
34    fn collect_into(self, _into: &mut impl Extend<(K, ChildSpec<ErasedActorFactory<R>>)>) {}
35}
36impl<K, R> CollectInto<K, R> for KV<K, R> {
37    fn collect_into(self, into: &mut impl Extend<(K, ChildSpec<ErasedActorFactory<R>>)>) {
38        let Self { key, child_spec } = self;
39        into.extend([(key, child_spec)]);
40    }
41}
42impl<K, R, Le, Ri> CollectInto<K, R> for (Le, Ri)
43where
44    Le: CollectInto<K, R>,
45    Ri: CollectInto<K, R>,
46{
47    fn collect_into(self, into: &mut impl Extend<(K, ChildSpec<ErasedActorFactory<R>>)>) {
48        let (le, ri) = self;
49        le.collect_into(into);
50        ri.collect_into(into);
51    }
52}
53
54impl<RS, C> MixedSup<RS, C> {
55    pub fn with_child<K, F>(
56        self,
57        key: K,
58        child_spec: ChildSpec<F>,
59    ) -> MixedSup<RS, <C as Append<K, F>>::Out>
60    where
61        C: Append<K, F>,
62    {
63        let Self {
64            restart_strategy,
65            children,
66        } = self;
67        let children = children.append(key, child_spec);
68        MixedSup {
69            restart_strategy,
70            children,
71        }
72    }
73}
74
75impl<RS, C> Clone for MixedSup<RS, C>
76where
77    RS: Clone,
78    C: Clone,
79{
80    fn clone(&self) -> Self {
81        Self {
82            restart_strategy: self.restart_strategy.clone(),
83            children:         self.children.clone(),
84        }
85    }
86}