seal_rs/actors/
local_actor_ref.rs

1//! Reference to instance of some actor. It is an interface for interacting with the internal
2//! logic of the actor cell.
3
4use crate::common::tsafe::TSafe;
5use crate::actors::actor_cell::ActorCell;
6use crate::actors::abstract_actor_ref::{AbstractActorRef, ActorRef};
7use crate::actors::actor_path::ActorPath;
8use std::fmt;
9use std::any::Any;
10
11pub struct LocalActorRef {
12    pub cell: TSafe<ActorCell>,
13    pub path: TSafe<ActorPath>
14}
15
16impl LocalActorRef {
17
18    /// Creates a new reference. This method should never be invoked by application code. This
19    /// constructor is used by internal API. Direct use from the user code is prohibited.
20    pub fn new(cell: TSafe<ActorCell>, path: TSafe<ActorPath>) -> LocalActorRef {
21        LocalActorRef {
22            cell,
23            path
24        }
25    }
26
27    fn inner_clone(self: &Self) -> Box<LocalActorRef> {
28        Box::new(LocalActorRef {
29            cell: self.cell.clone(),
30            path: self.path.clone(),
31        })
32    }
33}
34
35impl AbstractActorRef for LocalActorRef {
36
37    /// Send message to the actor behind the reference. In the first argument passed message for
38     /// send, and in the second argument specified sender reference. This reference will be injected
39     /// to ctx.sender field the actor context object. If sender was does not specified, ctx.sender
40     /// will be filled with the deadLetter actor reference. Setting up None as sender reference is
41     /// useful in case, when tell operation is
42     /// called from outside of the actor system.
43     ///
44     /// # Examples
45     ///
46     /// ```
47     ///
48     /// ```
49    fn tell(self: &mut Self, msg: Box<Any + Send + 'static>, rself: Option<Box<AbstractActorRef + Send>>) {
50        let cell_cloned = self.cell.clone();
51        let path_cloned = self.path.clone();
52        let toref = Box::new(LocalActorRef::new(cell_cloned, path_cloned));
53        let mut cell = self.cell.lock().unwrap();
54        cell.send(&self.cell, msg, rself, toref)    }
55
56    /// Return copy of the actor path object
57    fn path(self: &mut Self) -> ActorPath {
58        self.path.lock().unwrap().clone()
59    }
60
61    fn cell(self: &mut Self) -> TSafe<ActorCell> {
62        self.cell.clone()
63    }
64
65    fn clone(self: &Self) -> ActorRef {
66        self.inner_clone()
67    }
68
69    fn as_any(self: &Self) -> Box<Any> {
70        Box::new(self.inner_clone())
71    }
72}
73
74impl fmt::Display for LocalActorRef {
75    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76        write!(f, "ActorRef ({})", self.path.lock().unwrap())
77    }
78}
79
80/*impl Clone for LocalActorRef {
81    fn clone(&self) -> LocalActorRef {
82        LocalActorRef {
83            cell: self.cell.clone(),
84            path: self.path.clone()
85        }
86    }
87}*/
88
89impl PartialEq for LocalActorRef {
90    fn eq(&self, _other: &Self) -> bool {
91        true
92        //self.path == other.path
93    }
94}
95
96impl Eq for LocalActorRef {}
97