1use std::{collections::HashMap, sync::Arc};
2
3use anyhow::Result;
4use hash_map_id::HashMapId;
5use tokio::sync::{
6 mpsc::{UnboundedReceiver, UnboundedSender},
7 Mutex, RwLock,
8};
9use wasmtime::Linker;
10
11use crate::{
12 config::ProcessConfig,
13 mailbox::MessageMailbox,
14 runtimes::wasmtime::{WasmtimeCompiledModule, WasmtimeRuntime},
15 Signal,
16};
17
18pub type ConfigResources<T> = HashMapId<T>;
19pub type SignalSender = UnboundedSender<Signal>;
20pub type SignalReceiver = Arc<Mutex<UnboundedReceiver<Signal>>>;
21
22pub trait ProcessState: Sized {
28 type Config: ProcessConfig + Default + Send + Sync;
29
30 fn new_state(
34 &self,
35 module: Arc<WasmtimeCompiledModule<Self>>,
36 config: Arc<Self::Config>,
37 ) -> Result<Self>;
38
39 fn register(linker: &mut Linker<Self>) -> Result<()>;
41 fn initialize(&mut self);
43 fn is_initialized(&self) -> bool;
45
46 fn runtime(&self) -> &WasmtimeRuntime;
48 fn module(&self) -> &Arc<WasmtimeCompiledModule<Self>>;
50 fn config(&self) -> &Arc<Self::Config>;
52
53 fn id(&self) -> u64;
55 fn signal_mailbox(&self) -> &(SignalSender, SignalReceiver);
57 fn message_mailbox(&self) -> &MessageMailbox;
59
60 fn config_resources(&self) -> &ConfigResources<Self::Config>;
62 fn config_resources_mut(&mut self) -> &mut ConfigResources<Self::Config>;
63
64 fn registry(&self) -> &Arc<RwLock<HashMap<String, (u64, u64)>>>;
66}