actix/
context_items.rs

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
use std::{
    future::Future,
    pin::Pin,
    task::{self, Poll},
    time::Duration,
};

use futures_core::{ready, stream::Stream};
use pin_project_lite::pin_project;

use crate::{
    actor::{Actor, ActorContext, AsyncContext},
    clock::Sleep,
    fut::ActorFuture,
    handler::{Handler, Message, MessageResponse},
};

pub(crate) struct ActorWaitItem<A: Actor>(Pin<Box<dyn ActorFuture<A, Output = ()>>>);

impl<A> ActorWaitItem<A>
where
    A: Actor,
    A::Context: ActorContext + AsyncContext<A>,
{
    #[inline]
    pub fn new<F>(fut: F) -> Self
    where
        F: ActorFuture<A, Output = ()> + 'static,
    {
        ActorWaitItem(Box::pin(fut))
    }

    pub fn poll(
        mut self: Pin<&mut Self>,
        act: &mut A,
        ctx: &mut A::Context,
        task: &mut task::Context<'_>,
    ) -> Poll<()> {
        match self.0.as_mut().poll(act, ctx, task) {
            Poll::Pending => {
                if ctx.state().alive() {
                    Poll::Pending
                } else {
                    Poll::Ready(())
                }
            }
            Poll::Ready(_) => Poll::Ready(()),
        }
    }
}

pin_project! {
    pub(crate) struct ActorDelayedMessageItem<M: Message>{
        msg: Option<M>,
        #[pin]
        timeout: Sleep,
    }
}

impl<M: Message> ActorDelayedMessageItem<M> {
    pub fn new(msg: M, timeout: Duration) -> Self {
        Self {
            msg: Some(msg),
            timeout: actix_rt::time::sleep(timeout),
        }
    }
}

impl<A, M> ActorFuture<A> for ActorDelayedMessageItem<M>
where
    A: Actor + Handler<M>,
    A::Context: AsyncContext<A>,
    M: Message + 'static,
{
    type Output = ();

    fn poll(
        self: Pin<&mut Self>,
        act: &mut A,
        ctx: &mut A::Context,
        task: &mut task::Context<'_>,
    ) -> Poll<Self::Output> {
        let this = self.project();
        ready!(this.timeout.poll(task));
        let fut = A::handle(act, this.msg.take().unwrap(), ctx);
        fut.handle(ctx, None);
        Poll::Ready(())
    }
}

pub(crate) struct ActorMessageItem<M: Message> {
    msg: Option<M>,
}

impl<M: Message> Unpin for ActorMessageItem<M> {}

impl<M: Message> ActorMessageItem<M> {
    pub fn new(msg: M) -> Self {
        Self { msg: Some(msg) }
    }
}

impl<A, M> ActorFuture<A> for ActorMessageItem<M>
where
    A: Actor + Handler<M>,
    A::Context: AsyncContext<A>,
    M: Message + 'static,
{
    type Output = ();

    fn poll(
        self: Pin<&mut Self>,
        act: &mut A,
        ctx: &mut A::Context,
        _: &mut task::Context<'_>,
    ) -> Poll<Self::Output> {
        let this = self.get_mut();
        let fut = Handler::handle(act, this.msg.take().unwrap(), ctx);
        fut.handle(ctx, None);
        Poll::Ready(())
    }
}

pin_project! {
    pub(crate) struct ActorMessageStreamItem<S>{
        #[pin]
        stream: S,
    }
}

impl<S> ActorMessageStreamItem<S> {
    pub fn new(st: S) -> Self {
        Self { stream: st }
    }
}

impl<A, S> ActorFuture<A> for ActorMessageStreamItem<S>
where
    S: Stream,
    A: Actor + Handler<S::Item>,
    A::Context: AsyncContext<A>,
    S::Item: Message + 'static,
{
    type Output = ();

    fn poll(
        self: Pin<&mut Self>,
        act: &mut A,
        ctx: &mut A::Context,
        task: &mut task::Context<'_>,
    ) -> Poll<Self::Output> {
        let mut this = self.project();

        while let Some(msg) = ready!(this.stream.as_mut().poll_next(task)) {
            let fut = Handler::handle(act, msg, ctx);
            fut.handle(ctx, None);
            if ctx.waiting() {
                return Poll::Pending;
            }
        }

        Poll::Ready(())
    }
}