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
use std::pin::Pin;
use std::task;
use std::task::Poll;

use aktoro_channel as channel;
use aktoro_channel::error::TrySendError;
use aktoro_raw as raw;
use futures_core::Stream;
use futures_util::FutureExt;

use crate::action::Action;

/// An actor's control channel sender, used
/// by [`Context`].
///
/// [`Context`]: struct.Context.html
pub struct Controller<A: raw::Actor>(channel::Sender<Box<dyn raw::Action<Actor = A>>>);

/// An actor's control channel receiver,
/// used by [`Context`].
///
/// [`Context`]: struct.Context.html
pub struct Controlled<A: raw::Actor>(channel::Receiver<Box<dyn raw::Action<Actor = A>>>);

/// Creates a new control channel for the
/// specified actor type, returning a sender
/// and receiver connected to it.
pub(crate) fn new<A: raw::Actor>() -> (Controller<A>, Controlled<A>) {
    // TODO: maybe allow the channel's configuration
    // to be specified.
    let (sender, recver) = channel::Builder::new()
        .unbounded()
        .unlimited_msgs()
        .unlimited_senders()
        .unlimited_receivers()
        .build();

    (Controller(sender), Controlled(recver))
}

impl<A> raw::Controller<A> for Controller<A>
where
    A: raw::Actor + 'static,
{
    type Controlled = Controlled<A>;

    type Error = TrySendError<Box<dyn raw::Action<Actor = A>>>;

    fn try_send<D>(&mut self, action: D) -> raw::ControllerRes<A::Output, Self::Error>
    where
        A: raw::ActionHandler<D>,
        D: Send + 'static,
    {
        let (action, recv) = Action::new(action);

        self.0.try_send(Box::new(action))?;

        Ok(recv.map(Ok).boxed())
    }
}

impl<A: raw::Actor> raw::Controlled<A> for Controlled<A> {}

impl<A> Stream for Controlled<A>
where
    A: raw::Actor,
{
    type Item = Box<dyn raw::Action<Actor = A>>;

    fn poll_next(
        self: Pin<&mut Self>,
        ctx: &mut task::Context,
    ) -> Poll<Option<Box<dyn raw::Action<Actor = A>>>> {
        Pin::new(&mut self.get_mut().0).poll_next(ctx)
    }
}

impl<A> Clone for Controller<A>
where
    A: raw::Actor,
{
    fn clone(&self) -> Self {
        Controller(self.0.try_clone().unwrap())
    }
}