agner_actors/
spawn_opts.rs1use std::any::{Any, TypeId};
2use std::collections::{HashMap, HashSet};
3use std::sync::Arc;
4
5use crate::actor_id::ActorID;
6use crate::exit_handler::ExitHandler;
7
8const DEFAULT_MSG_INBOX_SIZE: usize = 1024;
9const DEFAULT_SIG_INBOX_SIZE: usize = 16;
10
11#[derive(Debug)]
20pub struct SpawnOpts {
21 links: HashSet<ActorID>,
22 msg_inbox_size: usize,
23 sig_inbox_size: usize,
24 exit_handler: Option<Arc<dyn ExitHandler>>,
25 data: HashMap<TypeId, Box<dyn Any + Send + Sync + 'static>>,
26}
27
28impl Default for SpawnOpts {
29 fn default() -> Self {
30 Self {
31 links: Default::default(),
32 msg_inbox_size: DEFAULT_MSG_INBOX_SIZE,
33 sig_inbox_size: DEFAULT_SIG_INBOX_SIZE,
34 exit_handler: None,
35 data: Default::default(),
36 }
37 }
38}
39
40impl SpawnOpts {
41 pub fn new() -> Self {
43 Default::default()
44 }
45}
46
47impl SpawnOpts {
48 pub fn with_link(mut self, with: ActorID) -> Self {
50 self.links.insert(with);
51 self
52 }
53 pub fn links(&self) -> impl Iterator<Item = ActorID> + '_ {
55 self.links.iter().copied()
56 }
57}
58
59impl SpawnOpts {
60 pub fn with_msg_inbox_size(mut self, sz: usize) -> Self {
62 self.msg_inbox_size = sz;
63 self
64 }
65
66 pub fn msg_inbox_size(&self) -> usize {
68 self.msg_inbox_size
69 }
70}
71
72impl SpawnOpts {
73 pub fn with_sig_inbox_size(mut self, sz: usize) -> Self {
75 self.sig_inbox_size = sz;
76 self
77 }
78
79 pub fn sig_inbox_size(&self) -> usize {
81 self.sig_inbox_size
82 }
83}
84
85impl SpawnOpts {
86 pub fn with_data<D>(mut self, data: D) -> Self
88 where
89 D: Any + Send + Sync + 'static,
90 {
91 let type_id = data.type_id();
92 let boxed = Box::new(data);
93 self.data.insert(type_id, boxed);
94 self
95 }
96
97 pub(crate) fn take_data(&mut self) -> HashMap<TypeId, Box<dyn Any + Send + Sync + 'static>> {
98 std::mem::take(&mut self.data)
99 }
100}
101
102impl SpawnOpts {
103 pub fn with_exit_handler(mut self, exit_handler: Arc<dyn ExitHandler>) -> Self {
105 let _ = self.exit_handler.replace(exit_handler);
106 self
107 }
108 pub(crate) fn take_exit_handler(&mut self) -> Option<Arc<dyn ExitHandler>> {
109 self.exit_handler.take()
110 }
111}