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
use async_trait::async_trait;

use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
};

pub use builder::*;
pub use end::End;
pub use event_loop::EventLoop;
pub use init::Init;
pub use launcher::*;
pub use name::Name;
pub use passthrough::Passthrough;
pub use preparer::Preparer;
pub use start::StartActor;
pub use starter::Starter;
pub use supervisor::*;
pub use terminating::Terminating;

#[async_trait]
pub trait Actor<H: AknShutdown<Self> + 'static>: StartActor<H> + Name {
    fn spawn<T>(task: T) -> tokio::task::JoinHandle<T::Output>
    where
        T: core::future::Future + Send + 'static,
        T::Output: Send + 'static,
    {
        tokio::spawn(task)
    }
    fn sleep(duration: core::time::Duration) -> tokio::time::Sleep {
        tokio::time::sleep(duration)
    }
    fn sleep_until(deadline: tokio::time::Instant) -> tokio::time::Sleep {
        tokio::time::sleep_until(deadline)
    }
    fn yield_now() -> YieldNow {
        YieldNow::default()
    }
}

/// Global Tokio YieldNow struct
#[derive(Default)]
pub struct YieldNow {
    yielded: bool,
}

impl Future for YieldNow {
    type Output = ();

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        if self.yielded {
            return Poll::Ready(());
        }

        self.yielded = true;
        cx.waker().wake_by_ref();
        Poll::Pending
    }
}

impl<T: super::Name + super::EventLoop<H> + super::Init<H> + super::Terminating<H>, H: Send + 'static + AknShutdown<Self>> Actor<H> for T {}

impl<T, H: 'static> StartActor<H> for T
where
    T: Actor<H>,
    H: AknShutdown<T>,
{
}

impl<T, H: 'static> End<H> for T
where
    T: Actor<H>,
    H: AknShutdown<T>,
{
}

mod builder;
mod end;
mod event_loop;
mod init;
mod launcher;
mod name;
mod passthrough;
mod preparer;
mod start;
mod starter;
mod supervisor;
mod terminating;