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
use futures::sync::oneshot::Sender as SyncSender;
use futures::unsync::oneshot::Sender as UnsyncSender;
use std::marker::PhantomData;

use super::{MessageDestination, MessageDestinationTransport, Syn, Unsync};
use actor::{Actor, AsyncContext};
use context::Context;
use handler::{Handler, Message, MessageResponse};

/// Converter trait, packs message to suitable envelope
pub trait ToEnvelope<T: MessageDestination<A, M>, A, M: Message + 'static>
where
    T::Transport: MessageDestinationTransport<T, A, M>,
    A: Actor + Handler<M>,
    A::Context: ToEnvelope<T, A, M>,
{
    /// Pack message into suitable envelope
    fn pack(msg: M, tx: Option<T::ResultSender>) -> T::Envelope;
}

pub trait EnvelopeProxy {
    type Actor: Actor;

    /// handle message within new actor and context
    fn handle(
        &mut self, act: &mut Self::Actor, ctx: &mut <Self::Actor as Actor>::Context
    );
}

pub struct MessageEnvelope<M: Message> {
    msg: M,
}

impl<M: Message> MessageEnvelope<M> {
    pub fn into_inner(self) -> M {
        self.msg
    }
}

impl<M: Message> From<M> for MessageEnvelope<M> {
    fn from(msg: M) -> MessageEnvelope<M> {
        MessageEnvelope { msg }
    }
}

pub struct SyncMessageEnvelope<M: Message + Send>
where
    M::Result: Send,
{
    msg: M,
}

impl<M: Message + Send> SyncMessageEnvelope<M>
where
    M::Result: Send,
{
    pub fn into_inner(self) -> M {
        self.msg
    }
}

impl<M: Message + Send> From<M> for SyncMessageEnvelope<M>
where
    M::Result: Send,
{
    fn from(msg: M) -> SyncMessageEnvelope<M> {
        SyncMessageEnvelope { msg }
    }
}

impl<A, M> ToEnvelope<Syn, A, M> for Context<A>
where
    A: Actor<Context = Context<A>> + Handler<M>,
    M: Message + Send + 'static,
    M::Result: Send,
{
    fn pack(msg: M, tx: Option<SyncSender<M::Result>>) -> SyncEnvelope<A> {
        SyncEnvelope::new(msg, tx)
    }
}

impl<T, A, M> ToEnvelope<Unsync, A, M> for T
where
    A: Actor<Context = T> + Handler<M>,
    M: Message + 'static,
    T: AsyncContext<A>,
{
    fn pack(msg: M, tx: Option<UnsyncSender<M::Result>>) -> UnsyncEnvelope<A> {
        UnsyncEnvelope::new(msg, tx)
    }
}

pub struct SyncEnvelope<A: Actor>(Box<EnvelopeProxy<Actor = A> + Send>);

unsafe impl<A: Actor> Send for SyncEnvelope<A> {}

impl<A: Actor> SyncEnvelope<A> {
    pub fn new<M>(msg: M, tx: Option<SyncSender<M::Result>>) -> SyncEnvelope<A>
    where
        A: Handler<M>,
        A::Context: AsyncContext<A>,
        M: Message + Send + 'static,
        M::Result: Send,
    {
        SyncEnvelope(Box::new(SyncEnvelopeProxy {
            tx,
            msg: Some(msg),
            act: PhantomData,
        }))
    }

    pub fn with_proxy(proxy: Box<EnvelopeProxy<Actor = A> + Send>) -> SyncEnvelope<A> {
        SyncEnvelope(proxy)
    }
}

impl<A: Actor> EnvelopeProxy for SyncEnvelope<A> {
    type Actor = A;

    fn handle(
        &mut self, act: &mut Self::Actor, ctx: &mut <Self::Actor as Actor>::Context
    ) {
        self.0.handle(act, ctx)
    }
}

pub struct SyncEnvelopeProxy<A, M>
where
    M: Message + Send,
{
    act: PhantomData<A>,
    msg: Option<M>,
    tx: Option<SyncSender<M::Result>>,
}

unsafe impl<A, M: Message + Send> Send for SyncEnvelopeProxy<A, M> {}

impl<A, M> EnvelopeProxy for SyncEnvelopeProxy<A, M>
where
    M: Message + Send + 'static,
    A: Actor + Handler<M>,
    A::Context: AsyncContext<A>,
{
    type Actor = A;

    fn handle(
        &mut self, act: &mut Self::Actor, ctx: &mut <Self::Actor as Actor>::Context
    ) {
        let tx = self.tx.take();
        if tx.is_some() && tx.as_ref().unwrap().is_canceled() {
            return;
        }

        if let Some(msg) = self.msg.take() {
            let fut = <Self::Actor as Handler<M>>::handle(act, msg, ctx);
            fut.handle(ctx, tx)
        }
    }
}

pub struct UnsyncEnvelope<A: Actor>(Box<EnvelopeProxy<Actor = A>>);

impl<A: Actor> UnsyncEnvelope<A> {
    pub fn new<M>(msg: M, tx: Option<UnsyncSender<M::Result>>) -> UnsyncEnvelope<A>
    where
        A: Handler<M>,
        A::Context: AsyncContext<A>,
        M: Message + 'static,
    {
        UnsyncEnvelope(Box::new(UnsyncEnvelopeProxy {
            tx,
            msg: Some(msg),
            act: PhantomData,
        }))
    }
}

impl<A: Actor> EnvelopeProxy for UnsyncEnvelope<A> {
    type Actor = A;

    #[inline]
    fn handle(
        &mut self, act: &mut Self::Actor, ctx: &mut <Self::Actor as Actor>::Context
    ) {
        self.0.handle(act, ctx)
    }
}

struct UnsyncEnvelopeProxy<A, M>
where
    M: Message,
{
    msg: Option<M>,
    act: PhantomData<A>,
    tx: Option<UnsyncSender<M::Result>>,
}

impl<A, M> EnvelopeProxy for UnsyncEnvelopeProxy<A, M>
where
    M: Message + 'static,
    A: Actor + Handler<M>,
    A::Context: AsyncContext<A>,
{
    type Actor = A;

    fn handle(
        &mut self, act: &mut Self::Actor, ctx: &mut <Self::Actor as Actor>::Context
    ) {
        let tx = self.tx.take();
        if tx.is_some() && tx.as_ref().unwrap().is_canceled() {
            return;
        }
        if let Some(msg) = self.msg.take() {
            <Self::Actor as Handler<M>>::handle(act, msg, ctx).handle(ctx, tx)
        }
    }
}