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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! `meio` - lightweight async actor framework for Rust.
//! The main benefit of this framework is that gives you
//! tiny extension over `tokio` with full control.
//!
//! It's designed for professional applications where you
//! can't have strong restrictions and have keep flexibility.
//!
//! Also this crate has zero-cost runtime. It just calls
//! `async` method of your type.

#![warn(missing_docs)]
#![recursion_limit = "256"]
#![feature(generators)]

mod actor_runtime;
mod lite_runtime;

pub mod address;
pub mod channel;
pub mod handlers;
pub mod performers;
pub mod recipients;
pub mod signal;
pub mod terminator;

pub use actor_runtime::{Actor, Context};
pub use address::Address;
pub use channel::{Controller, Status, Supervisor};
use channel::{Operator, Signal};
use handlers::Envelope;
pub use handlers::{Action, ActionHandler, Interaction, InteractionHandler};
pub use lite_runtime::{LiteStatus, LiteTask, ShutdownReceiver};
pub use performers::{ActionPerformer, InteractionPerformer};
pub use recipients::{ActionRecipient, InteractionRecipient};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
pub use terminator::{Stage, TerminationProgress, Terminator};

/// Unique Id of Actor's runtime that used to identify
/// all senders for that actor.
#[derive(Clone)]
pub struct Id(Arc<String>);

impl Id {
    /// Generated new `Id` for `Actor`.
    fn of_actor<T: Actor>(entity: &T) -> Self {
        let name = entity.name();
        Self(Arc::new(name))
    }

    /// Generated new `Id` for `Actor`.
    fn of_task<T: LiteTask>(entity: &T) -> Self {
        let name = entity.name();
        Self(Arc::new(name))
    }
}

impl fmt::Display for Id {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.0.as_ref().fmt(f)
    }
}

impl fmt::Debug for Id {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple(self.0.as_ref()).finish()
    }
}

impl PartialEq<Id> for Id {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for Id {}

impl Hash for Id {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.as_ref().hash(state);
    }
}

/// Returns a `Link` to an `Actor`.
/// `Link` is a convenient concept for creating wrappers for
/// `Address` that provides methods instead of using message types
/// directly. It allows also to use private message types opaquely.
pub trait Link<T> {
    /// Gets a `Link` to an `Actor`.
    fn link(&self) -> T;
}

impl<T, A> Link<T> for Address<A>
where
    T: From<Address<A>>,
    A: Actor,
{
    fn link(&self) -> T {
        T::from(self.clone())
    }
}

impl<T, I> Link<T> for ActionRecipient<I>
where
    T: From<ActionRecipient<I>>,
    I: Action,
{
    fn link(&self) -> T {
        T::from(self.clone())
    }
}

impl<T, I> Link<T> for InteractionRecipient<I>
where
    T: From<InteractionRecipient<I>>,
    I: Interaction,
{
    fn link(&self) -> T {
        T::from(self.clone())
    }
}

// %%%%%%%%%%%%%%%%%%%%%% TESTS %%%%%%%%%%%%%%%%%%%%%

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Error;
    use async_trait::async_trait;
    use futures::stream;
    use std::time::Duration;
    use tokio::time::delay_for;

    struct MyActor;

    struct MsgOne;

    impl Action for MsgOne {}

    struct MsgTwo;

    impl Interaction for MsgTwo {
        type Output = u8;
    }

    #[async_trait]
    impl Actor for MyActor {
        async fn terminate(&mut self) {
            delay_for(Duration::from_secs(3)).await;
        }
    }

    #[async_trait]
    impl ActionHandler<MsgOne> for MyActor {
        async fn handle(&mut self, _: MsgOne, _ctx: &mut Context<Self>) -> Result<(), Error> {
            log::info!("Received MsgOne");
            Ok(())
        }
    }

    #[async_trait]
    impl InteractionHandler<MsgTwo> for MyActor {
        async fn handle(&mut self, _: MsgTwo, _ctx: &mut Context<Self>) -> Result<u8, Error> {
            log::info!("Received MsgTwo");
            Ok(1)
        }
    }

    #[async_trait]
    impl ActionHandler<signal::CtrlC> for MyActor {
        async fn handle(
            &mut self,
            _: signal::CtrlC,
            _ctx: &mut Context<Self>,
        ) -> Result<(), Error> {
            log::info!("Received CtrlC");
            Ok(())
        }
    }

    #[tokio::test]
    async fn start_and_terminate() -> Result<(), Error> {
        env_logger::try_init().ok();
        let mut address = MyActor.start(Supervisor::None);
        address.act(MsgOne).await?;
        let res = address.interact(MsgTwo).await?;
        assert_eq!(res, 1);
        address.shutdown();
        address.join().await;
        Ok(())
    }

    #[tokio::test]
    async fn test_recipient() -> Result<(), Error> {
        env_logger::try_init().ok();
        let mut address = MyActor.start(Supervisor::None);
        let action_recipient = address.action_recipient();
        action_recipient.clone().act(MsgOne).await?;
        let interaction_recipient = address.interaction_recipient();
        let res = interaction_recipient.clone().interact(MsgTwo).await?;
        assert_eq!(res, 1);
        address.shutdown();
        address.join().await;
        Ok(())
    }

    #[tokio::test]
    async fn test_attach() -> Result<(), Error> {
        env_logger::try_init().ok();
        let mut address = MyActor.start(Supervisor::None);
        let stream = stream::iter(vec![MsgOne, MsgOne, MsgOne]);
        address.attach(stream).await?;
        // If you activeate this line the test will wait for the `Ctrl+C` signal.
        //address.attach(signal::CtrlC::stream()).await?;
        address.shutdown();
        Ok(())
    }
}