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
#![no_std]

extern crate no_std_compat as std;
extern crate alloc;

use std::prelude::v1::*;


#[cfg(feature = "exec_tokio")]
mod tokio;
#[cfg(feature = "exec_tokio")]
pub use tokio::TokioExecutor;

#[cfg(feature = "exec_smol")]
mod smol;

#[cfg(feature = "exec_smol")]
pub use smol::SmolExecutor;


#[cfg(feature = "exec_async_std")]
mod async_std;

pub mod read_write;
pub mod sleep;
pub mod moment;

#[cfg(feature = "exec_async_std")]
pub use async_std::AsyncStdExecutor;

use async_trait::async_trait;
use std::sync::Arc;
use std::future::Future;

pub use chrono::*;
pub use ioslice::IoSlice;

pub type BoxError = Box<dyn core_error::Error + Send + Sync>;


#[async_trait]
pub trait Executor<X>: Send + Sync
{
    async fn context() -> Result<Self, futures_task::SpawnError>
    where
        Self: Sized;

    async fn set_context(&mut self, executor: X);

    async fn enter_context(&self);

    async fn get_executor(&self) -> &X;

    fn context_sync() -> Result<Self, futures_task::SpawnError>
        where
            Self: Sized;

    fn set_context_sync(&mut self, executor: X);

    fn enter_context_sync(&self);

    fn get_executor_sync(&self) -> &X;
}


#[async_trait]
pub trait ExecutorPerform<X>: Send + Sync
{
    async fn spawn_in_context<F>(future: F) -> Result<Arc<dyn Task<F::Output, Output = F::Output>>, futures_task::SpawnError>
        where
            F: Future + Send + 'static,
            F::Output: Send + 'static;

    async fn spawn_from_executor<E, F>(executor: &E, future: F) -> Result<Arc<dyn Task<F::Output, Output = F::Output>>, futures_task::SpawnError>
        where
            E: Executor<X>,
            F: Future + Send + 'static,
            F::Output: Send + 'static;

    async fn block_from_executor<E, F>(executor: &E, future: F) -> F::Output
        where
            E: Executor<X>,
            F: Future + Send + 'static,
            F::Output: Send + 'static;

    fn spawn_in_context_sync<F>(future: F) -> Result<Arc<dyn Task<F::Output, Output = F::Output>>, futures_task::SpawnError>
        where
            F: Future + Send + 'static,
            F::Output: Send + 'static;

    fn spawn_from_executor_sync<E, F>(executor: &E, future: F) -> Result<Arc<dyn Task<F::Output, Output = F::Output>>, futures_task::SpawnError>
        where
            E: Executor<X>,
            F: Future + Send + 'static,
            F::Output: Send + 'static;

    fn block_from_executor_sync<E, F>(executor: &E, future: F) -> F::Output
        where
            E: Executor<X>,
            F: Future + Send + 'static,
            F::Output: Send + 'static;
}



#[async_trait]
pub trait Timer: Send + Sync {
    async fn once(duration_millis: u32) -> Self;
    async fn interval(duration_millis: u32) -> Self;

    async fn cancel(&mut self);

    fn once_sync(duration_millis: u32) -> Self;
    fn interval_sync(duration_millis: u32) -> Self;

    fn cancel_sync(&mut self);

    async fn tick(&mut self) -> u32;
}


#[async_trait]
pub trait Task<Out>: Future
where
    Out: Send + 'static
{
    async fn output(self) -> Out;
    fn detach(self);
    fn drop(self);
}