rivetkit_core/actor/
factory.rs1use std::fmt;
2
3use anyhow::Result;
4
5use crate::ActorConfig;
6use crate::actor::lifecycle_hooks::ActorStart;
7use crate::runtime::RuntimeBoxFuture;
8
9#[cfg(feature = "wasm-runtime")]
10pub type ActorEntryFn = dyn Fn(ActorStart) -> RuntimeBoxFuture<Result<()>>;
11
12#[cfg(not(feature = "wasm-runtime"))]
13pub type ActorEntryFn = dyn Fn(ActorStart) -> RuntimeBoxFuture<Result<()>> + Send + Sync;
14
15pub struct ActorFactory {
17 config: ActorConfig,
18 entry: Box<ActorEntryFn>,
19 manual_startup_ready: bool,
20}
21
22#[cfg(feature = "wasm-runtime")]
23unsafe impl Send for ActorFactory {}
24
25#[cfg(feature = "wasm-runtime")]
26unsafe impl Sync for ActorFactory {}
27
28impl ActorFactory {
29 pub fn new<F>(config: ActorConfig, entry: F) -> Self
30 where
31 F: ActorEntry,
32 {
33 Self {
34 config,
35 entry: Box::new(entry),
36 manual_startup_ready: false,
37 }
38 }
39
40 pub fn new_with_manual_startup_ready<F>(config: ActorConfig, entry: F) -> Self
43 where
44 F: ActorEntry,
45 {
46 Self {
47 config,
48 entry: Box::new(entry),
49 manual_startup_ready: true,
50 }
51 }
52
53 pub fn config(&self) -> &ActorConfig {
54 &self.config
55 }
56
57 pub(crate) fn requires_manual_startup_ready(&self) -> bool {
58 self.manual_startup_ready
59 }
60
61 pub async fn start(&self, start: ActorStart) -> Result<()> {
62 (self.entry)(start).await
63 }
64}
65
66#[cfg(feature = "wasm-runtime")]
67pub trait ActorEntry: Fn(ActorStart) -> RuntimeBoxFuture<Result<()>> + 'static {}
68
69#[cfg(feature = "wasm-runtime")]
70impl<F> ActorEntry for F where F: Fn(ActorStart) -> RuntimeBoxFuture<Result<()>> + 'static {}
71
72#[cfg(not(feature = "wasm-runtime"))]
73pub trait ActorEntry:
74 Fn(ActorStart) -> RuntimeBoxFuture<Result<()>> + Send + Sync + 'static
75{
76}
77
78#[cfg(not(feature = "wasm-runtime"))]
79impl<F> ActorEntry for F where
80 F: Fn(ActorStart) -> RuntimeBoxFuture<Result<()>> + Send + Sync + 'static
81{
82}
83
84impl fmt::Debug for ActorFactory {
85 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86 f.debug_struct("ActorFactory")
87 .field("config", &self.config)
88 .field("manual_startup_ready", &self.manual_startup_ready)
89 .field("entry", &"<boxed entry>")
90 .finish()
91 }
92}