gloo_worker/oneshot/
spawner.rs1use serde::de::Deserialize;
2use serde::ser::Serialize;
3
4use super::bridge::OneshotBridge;
5use super::traits::Oneshot;
6use super::worker::OneshotWorker;
7use crate::actor::WorkerSpawner;
8use crate::codec::{Bincode, Codec};
9
10#[derive(Debug, Default)]
12pub struct OneshotSpawner<N, CODEC = Bincode>
13where
14 N: Oneshot + 'static,
15 CODEC: Codec,
16{
17 inner: WorkerSpawner<OneshotWorker<N>, CODEC>,
18}
19
20impl<N, CODEC> OneshotSpawner<N, CODEC>
21where
22 N: Oneshot + 'static,
23 CODEC: Codec,
24{
25 pub const fn new() -> Self {
27 Self {
28 inner: WorkerSpawner::<OneshotWorker<N>, CODEC>::new(),
29 }
30 }
31
32 pub const fn encoding<C>(&self) -> OneshotSpawner<N, C>
34 where
35 C: Codec,
36 {
37 OneshotSpawner {
38 inner: WorkerSpawner::<OneshotWorker<N>, C>::new(),
39 }
40 }
41
42 pub fn spawn(mut self, path: &str) -> OneshotBridge<N>
44 where
45 N::Input: Serialize + for<'de> Deserialize<'de>,
46 N::Output: Serialize + for<'de> Deserialize<'de>,
47 {
48 let rx = OneshotBridge::register_callback(&mut self.inner);
49
50 let inner = self.inner.spawn(path);
51
52 OneshotBridge::new(inner, rx)
53 }
54
55 pub fn spawn_with_loader(mut self, loader_path: &str) -> OneshotBridge<N>
57 where
58 N::Input: Serialize + for<'de> Deserialize<'de>,
59 N::Output: Serialize + for<'de> Deserialize<'de>,
60 {
61 let rx = OneshotBridge::register_callback(&mut self.inner);
62
63 let inner = self.inner.spawn_with_loader(loader_path);
64
65 OneshotBridge::new(inner, rx)
66 }
67}