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
use serde::de::Deserialize;
use serde::ser::Serialize;

use super::bridge::ReactorBridge;
use super::scope::ReactorScoped;
use super::traits::Reactor;
use super::worker::ReactorWorker;
use crate::actor::WorkerSpawner;
use crate::codec::{Bincode, Codec};

/// A spawner to create oneshot workers.
#[derive(Debug, Default)]
pub struct ReactorSpawner<R, CODEC = Bincode>
where
    R: Reactor + 'static,
    CODEC: Codec,
{
    inner: WorkerSpawner<ReactorWorker<R>, CODEC>,
}

impl<R, CODEC> ReactorSpawner<R, CODEC>
where
    R: Reactor + 'static,
    CODEC: Codec,
{
    /// Creates a ReactorSpawner.
    pub const fn new() -> Self {
        Self {
            inner: WorkerSpawner::<ReactorWorker<R>, CODEC>::new(),
        }
    }

    /// Sets a new message encoding.
    pub const fn encoding<C>(&self) -> ReactorSpawner<R, C>
    where
        C: Codec,
    {
        ReactorSpawner {
            inner: WorkerSpawner::<ReactorWorker<R>, C>::new(),
        }
    }

    /// Spawns a reactor worker.
    pub fn spawn(mut self, path: &str) -> ReactorBridge<R>
    where
        <R::Scope as ReactorScoped>::Input: Serialize + for<'de> Deserialize<'de>,
        <R::Scope as ReactorScoped>::Output: Serialize + for<'de> Deserialize<'de>,
    {
        let rx = ReactorBridge::register_callback(&mut self.inner);

        let inner = self.inner.spawn(path);

        ReactorBridge::new(inner, rx)
    }

    /// Spawns a Reactor Worker with a loader shim script.
    pub fn spawn_with_loader(mut self, loader_path: &str) -> ReactorBridge<R>
    where
        <R::Scope as ReactorScoped>::Input: Serialize + for<'de> Deserialize<'de>,
        <R::Scope as ReactorScoped>::Output: Serialize + for<'de> Deserialize<'de>,
    {
        let rx = ReactorBridge::register_callback(&mut self.inner);

        let inner = self.inner.spawn_with_loader(loader_path);

        ReactorBridge::new(inner, rx)
    }
}