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
// actor.rs -- theater actor definitions and helpers
// Copyright (C) 2015 Alex Iadicicco

//! Actors form the core of the actor model. This module provides the
//! definitions used throughout the project for describing and managing actors.

use rustc_serialize;
use rustc_serialize::{Encodable, Decodable};

/// Actor behavior is defined by an actor definition, which is any type
/// implementing the `Actor` trait.
pub trait Actor {
    /// The type of messages this actor expects to receive. Incoming messages
    /// are automatically decoded to this type. Similarly, messages sent to this
    /// actor are encoded using this type. Decode or encode failures are
    /// considered a kind of message delivery failure.
    type Message: Encodable + Decodable;

    /// The heart of the actor, where messages are processed internally as state
    /// changes and produce zero or more actions.
    fn recv(&mut self, m: Self::Message);
}

trait ActorIntrinsics: Actor {
    fn deliver<D>(&mut self, d: &mut D)
    where D: rustc_serialize::Decoder {
        match Self::Message::decode(d) {
            Ok(m) => self.recv(m),
            Err(_) => { }
        }
    }
}

impl<A: Actor> ActorIntrinsics for A { }

/// A reference to an actor instance. Contains information about the location
/// of the actor.
#[derive(Copy, Clone)]
pub enum ActorRef {
    Local(LocalActorRef),
    Remote(RemoteActorRef),
}

/// A reference to an actor running on the current host. Messages to these
/// instances can be sent relatively quickly, and are less likely to be lost.
#[derive(Copy, Clone)]
pub struct LocalActorRef {
    thread: u32,
    ident: u32,
}

/// A reference to an actor running on a remote host. Messages to these
/// instances are much slower and more error prone to deliver.
#[derive(Copy, Clone)]
pub struct RemoteActorRef;

#[cfg(test)]
mod tests {
    use actor::Actor;
    use actor::ActorIntrinsics;

    #[derive(RustcEncodable, RustcDecodable)]
    struct DumyMessage(i32);

    struct DumyActor {
        value: i32
    }

    impl DumyActor {
        fn new_with_value(val: i32) -> DumyActor {
            DumyActor { value: val }
        }
    }

    impl Actor for DumyActor {
        type Message = DumyMessage;

        fn recv(&mut self, DumyMessage(val): DumyMessage) {
            self.value = val;
        }
    }

    #[test]
    fn test_encode_decode_deliver_works() {
        use rustc_serialize::json;

        let mut actor = DumyActor::new_with_value(5);
        let encoded = match json::encode(&DumyMessage(7)) {
            Ok(v) => v,
            Err(_) => panic!("encode failed")
        };
        let parsed = match json::Json::from_str(&encoded[..]) {
            Ok(v) => v,
            Err(_) => panic!("parse failed")
        };

        actor.deliver(&mut json::Decoder::new(parsed));

        assert_eq!(actor.value, 7);
    }
}