robots/actors/
props.rs

1use std::sync::Arc;
2
3use actors::{Actor, Arguments};
4
5/// Public interface of a Props.
6pub trait ActorFactory: Send + Sync {
7    /// Creates an Actor instance.
8    fn create(&self) ->  Arc<Actor>;
9}
10
11/// Props is the current only ActorFactory.
12///
13/// It is used to generate actors in a reliable way, calling `create` will always create the same
14/// actor insatance, it is thus the way used to create actors through this crate.
15pub struct Props<Args: Arguments, A: Actor> {
16    creator: Arc<Fn(Args) -> A + Sync + Send>,
17    args: Args,
18}
19
20impl<Args: Arguments, A: Actor> Props<Args, A> {
21    /// Creates a `Props` which is a factory for `A` with the `creator` function and `args` args.
22    pub fn new(creator: Arc<Fn(Args) -> A + Sync + Send>, args: Args) -> Arc<ActorFactory> {
23        Arc::new(Props::<Args, A> {
24            creator: creator,
25            args: args,
26        })
27    }
28}
29
30impl<Args: Arguments, A: Actor> ActorFactory for Props<Args, A> {
31    /// Creates an Actor instance with the `creator` function and the `args` args.
32    ///
33    /// This is meant to allow to respawn an Actor when it fails.
34    fn create(&self) -> Arc<Actor> {
35        // FIXME(gamazeps): reopen https://github.com/rust-lang/rust/issues/18343 with an example.
36        let args = self.args.clone();
37        Arc::new((self.creator)(args))
38    }
39}
40
41impl<Args: Arguments, A: Actor> Clone for Props<Args, A> {
42    fn clone(&self) -> Props<Args, A> {
43        Props::<Args, A> {
44            creator: self.creator.clone(),
45            args: self.args.clone(),
46        }
47    }
48}