Skip to main content

palladium_actor/
spec.rs

1use crate::actor::Actor;
2use crate::policy::{NamespacePolicy, RestartPolicy, ShutdownPolicy};
3use std::fmt;
4use std::sync::Arc;
5
6/// Specification provided at spawn time that governs a child actor's lifecycle.
7///
8/// `factory` is called by the supervisor whenever it needs to (re)create the
9/// child actor — at initial spawn and after each restart.  It must be
10/// `Send + Sync` so the supervisor task can share the spec across threads.
11#[derive(Clone)]
12pub struct ChildSpec<R: crate::bridge::Reactor> {
13    pub name: String,
14    pub restart: RestartPolicy,
15    pub shutdown: ShutdownPolicy,
16    pub namespace: NamespacePolicy,
17    pub mailbox_capacity: usize,
18    /// Factory function that produces a fresh `Box<dyn Actor<R>>` on each
19    /// (re)spawn.  The closure must be cheap to call repeatedly.
20    pub factory: Arc<dyn Fn() -> Box<dyn Actor<R>> + Send + Sync>,
21}
22
23impl<R: crate::bridge::Reactor> fmt::Debug for ChildSpec<R> {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        f.debug_struct("ChildSpec")
26            .field("name", &self.name)
27            .field("restart", &self.restart)
28            .field("shutdown", &self.shutdown)
29            .field("namespace", &self.namespace)
30            .field("mailbox_capacity", &self.mailbox_capacity)
31            .field("factory", &"<factory fn>")
32            .finish()
33    }
34}
35
36impl<R: crate::bridge::Reactor> ChildSpec<R> {
37    pub fn new(
38        name: impl Into<String>,
39        restart: RestartPolicy,
40        shutdown: ShutdownPolicy,
41        namespace: NamespacePolicy,
42        factory: impl Fn() -> Box<dyn Actor<R>> + Send + Sync + 'static,
43    ) -> Self {
44        Self {
45            name: name.into(),
46            restart,
47            shutdown,
48            namespace,
49            mailbox_capacity: 1024, // Default capacity
50            factory: Arc::new(factory),
51        }
52    }
53
54    /// Builder-style method to override the default mailbox capacity.
55    pub fn with_mailbox_capacity(mut self, capacity: usize) -> Self {
56        self.mailbox_capacity = capacity;
57        self
58    }
59}