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
//! A simple, single-threaded async runtime
//!
//! Takyon is an async runtime for running futures and doing asynchronous IO on a single thread.
//! It is designed to be simple, lightweight and intended for CLIs, GUI desktop apps, games,
//! ie: any use case where a heavy, sophisticated multithreaded runtime is not required
//!
//! This crate is still under development and currently only the following features are supported:
//! - Linux support (using `io_uring`)
//! - Spawning and joining child tasks
//! - Sleeping
//! - TCP and UDP network IO
//!
//! The following features are planned for the future:
//! - File IO
//! - Windows, BSD and MacOS support
//! - Inter-task communication such as channels, watch, notify, etc
//!
//! # Examples
//! An async TCP server:
//! ```
//! use takyon::net::TcpListener;
//!
//! takyon::init().unwrap();
//!
//! takyon::run(async {
//! // Create a TcpListener
//! let listener = TcpListener::bind("127.0.0.1:5000").await.unwrap();
//!
//! loop {
//! // Wait for incoming connections
//! let (stream, src_addr) = listener.accept().await.unwrap();
//! println!("New connection from {:?}\n", src_addr);
//!
//! // Spawn task to handle connection
//! takyon::spawn(async move {
//! let mut buf = [0; 1024];
//!
//! // Read data from the TcpStream
//! loop {
//! let bytes = stream.read(&mut buf).await.unwrap();
//!
//! if bytes == 0 {
//! println!("Address {:?} disconnected\n", src_addr);
//! break;
//! }
//!
//! println!("Read {:?} bytes from address {:?}", bytes, src_addr);
//! println!("Data: {:02X?}\n", &buf[..bytes]);
//! }
//! });
//! }
//! });
//! ```
//!
//! An async TCP client:
//! ```
//! use std::net::Shutdown;
//! use takyon::{time::sleep_secs, net::TcpStream};
//!
//! takyon::init().unwrap();
//!
//! takyon::run(async {
//! loop {
//! // Connect to the server
//! sleep_secs(1).await;
//! let stream = TcpStream::connect("127.0.0.1:5000").await.unwrap();
//!
//! // Write some data
//! sleep_secs(1).await;
//! stream.write(&[0xAA, 0xBB, 0xCC]).await.unwrap();
//!
//! // Shut down the connection
//! sleep_secs(1).await;
//! stream.shutdown(Shutdown::Both).await.unwrap();
//! }
//! });
//! ```
pub use InitError;
pub use JoinHandle;
use ptr;
use pin;
use Future;
use ;
use ;
use ;
thread_local!
static WAKER_VTABLE: RawWakerVTable = new;
/// Initializes the thread-local runtime
///
/// This must be called at least once before calling [`run()`] on a thread
/// Runs a future on the current thread, blocking it whenever waiting for IO
///
/// The passed future will be considered the "root task". The root task can
/// use [`spawn()`] to spawn child tasks. The function returns the root task's
/// result as soon as it has finished, and all pending child tasks will be dropped.
///
/// Use the child tasks' [`JoinHandle`]s if you want to wait for them to complete
/// before returning. Remember to call [`init()`] atleast once on a thread before
/// using [`run()`]
///
/// # Examples
/// ```
/// use takyon::time::sleep_secs;
///
/// // Initialize the thread-local runtime
/// takyon::init()?;
///
/// // Run a future
/// let result = takyon::run(async {
/// sleep_secs(1).await;
/// println!("1 second passed");
///
/// sleep_secs(1).await;
/// println!("2 seconds passed");
///
/// let result = do_something().await;
///
/// result
/// });
///
/// // Use the result returned by the future
/// println!("{result}");
/// ```
/// Spawns a new task and returns it's [`JoinHandle`]
///
/// The new task immediately runs concurrently with the current task without needing to
/// `await` it. The returned [`JoinHandle`] can be used to wait for the task to finish
///
/// See the [`JoinHandle`] docs for an example of using this function
///
/// # Panics
/// This can only be used within a [`run()`] call and will panic if used outside it