1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use std::any::{Any, TypeId};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use crate::actor_id::ActorID;
use crate::exit_handler::ExitHandler;

const DEFAULT_MSG_INBOX_SIZE: usize = 1024;
const DEFAULT_SIG_INBOX_SIZE: usize = 16;

/// Options with which an actor will be spawned.
///
/// It is possible to specify:
/// - the set of [actor-ids](crate::actor_id::ActorID) the newly spawned actor will be immediately
///   linked to;
/// - the sizes for msg-inbox and signal-inbox;
/// - [exit-handler](crate::exit_handler::ExitHandler);
/// - a "bag" of arbitrary properties (identified by their types).
#[derive(Debug)]
pub struct SpawnOpts {
    links: HashSet<ActorID>,
    msg_inbox_size: usize,
    sig_inbox_size: usize,
    exit_handler: Option<Arc<dyn ExitHandler>>,
    data: HashMap<TypeId, Box<dyn Any + Send + Sync + 'static>>,
}

impl Default for SpawnOpts {
    fn default() -> Self {
        Self {
            links: Default::default(),
            msg_inbox_size: DEFAULT_MSG_INBOX_SIZE,
            sig_inbox_size: DEFAULT_SIG_INBOX_SIZE,
            exit_handler: None,
            data: Default::default(),
        }
    }
}

impl SpawnOpts {
    /// create new [SpawnOpts](SpawnOpts)
    pub fn new() -> Self {
        Default::default()
    }
}

impl SpawnOpts {
    /// add a linked actor
    pub fn with_link(mut self, with: ActorID) -> Self {
        self.links.insert(with);
        self
    }
    /// iterator of linked actors
    pub fn links(&self) -> impl Iterator<Item = ActorID> + '_ {
        self.links.iter().copied()
    }
}

impl SpawnOpts {
    /// specify the capacity limit for msg-inbox
    pub fn with_msg_inbox_size(mut self, sz: usize) -> Self {
        self.msg_inbox_size = sz;
        self
    }

    /// the capacity limit for msg-inbox
    pub fn msg_inbox_size(&self) -> usize {
        self.msg_inbox_size
    }
}

impl SpawnOpts {
    /// specify the capacity limit for signal-inbox
    pub fn with_sig_inbox_size(mut self, sz: usize) -> Self {
        self.sig_inbox_size = sz;
        self
    }

    /// the capacity limit for signal-inbox
    pub fn sig_inbox_size(&self) -> usize {
        self.sig_inbox_size
    }
}

impl SpawnOpts {
    /// add arbitrary data into the [`Context`](crate::context::Context)
    pub fn with_data<D>(mut self, data: D) -> Self
    where
        D: Any + Send + Sync + 'static,
    {
        let type_id = data.type_id();
        let boxed = Box::new(data);
        self.data.insert(type_id, boxed);
        self
    }

    pub(crate) fn take_data(&mut self) -> HashMap<TypeId, Box<dyn Any + Send + Sync + 'static>> {
        std::mem::take(&mut self.data)
    }
}

impl SpawnOpts {
    /// Specify the [exit-handler](crate::exit_handler::ExitHandler) for the spawned actor
    pub fn with_exit_handler(mut self, exit_handler: Arc<dyn ExitHandler>) -> Self {
        let _ = self.exit_handler.replace(exit_handler);
        self
    }
    pub(crate) fn take_exit_handler(&mut self) -> Option<Arc<dyn ExitHandler>> {
        self.exit_handler.take()
    }
}