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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/// Core async utilities providing fundamental building blocks for asynchronous operations.
///
/// This module includes basic async helpers like sleep, race, join, cancellation tokens,
/// and mutex creation, leveraging smol runtime for optimal performance.
///
/// # Examples
///
/// Basic sleep and join:
/// ```rust
/// use trash_utilities::async::core::{sleep_for, join};
/// use smol;
/// use std::time::Duration;
///
/// # smol::block_on(async {
/// let (result1, result2) = join(
/// async { sleep_for(Duration::from_millis(10)).await; 1 },
/// async { 2 }
/// ).await;
/// assert_eq!((result1, result2), (1, 2));
/// # });
/// ```
// Standard library imports
use Duration;
// External crate imports
use futures_lite;
use Mutex;
use Timer;
use CancellationToken;
/// Async sleep helper.
///
/// Pauses execution for the specified duration using the smol runtime.
///
/// # Parameters
///
/// * `duration` - The amount of time to sleep.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::core::sleep_for;
/// use smol;
/// use std::time::Duration;
///
/// # smol::block_on(async {
/// sleep_for(Duration::from_millis(100)).await;
/// println!("Slept for 100ms");
/// # });
/// ```
pub async
/// Futures-lite helper: race two futures.
///
/// Returns the result of the first future to complete, cancelling the other.
///
/// # Parameters
///
/// * `f1` - The first future.
/// * `f2` - The second future.
///
/// # Type Parameters
///
/// * `T` - The output type of both futures.
/// * `F1` - The type of the first future.
/// * `F2` - The type of the second future.
///
/// # Returns
///
/// The result of whichever future completes first.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::core::{race, sleep_for};
/// use smol;
/// use std::time::Duration;
///
/// # smol::block_on(async {
/// let result = race(
/// async { sleep_for(Duration::from_millis(10)).await; "slow" },
/// async { "fast" }
/// ).await;
/// assert_eq!(result, "fast");
/// # });
/// ```
pub async
/// Futures-lite helper: join two futures.
///
/// Runs both futures concurrently and returns their results as a tuple.
///
/// # Parameters
///
/// * `f1` - The first future.
/// * `f2` - The second future.
///
/// # Type Parameters
///
/// * `T1` - The output type of the first future.
/// * `T2` - The output type of the second future.
/// * `F1` - The type of the first future.
/// * `F2` - The type of the second future.
///
/// # Returns
///
/// A tuple containing the results of both futures.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::core::join;
/// use smol;
///
/// # smol::block_on(async {
/// let (result1, result2) = join(
/// async { 1 },
/// async { 2 }
/// ).await;
/// assert_eq!((result1, result2), (1, 2));
/// # });
/// ```
pub async
/// Cancellation token helper.
///
/// Creates a new cancellation token that can be used to cancel async operations.
///
/// # Returns
///
/// A new `CancellationToken` instance.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::core::create_cancellation_token;
///
/// let token = create_cancellation_token();
/// // Use token to cancel operations
/// ```
/// Run a future with cancellation.
///
/// Executes the future, but returns `None` if the token is cancelled before completion.
///
/// # Parameters
///
/// * `token` - The cancellation token to monitor.
/// * `future` - The future to execute.
///
/// # Type Parameters
///
/// * `T` - The output type of the future.
/// * `F` - The type of the future.
///
/// # Returns
///
/// `Some(result)` if the future completes, or `None` if cancelled.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::core::{create_cancellation_token, with_cancellation};
/// use smol;
/// use std::time::Duration;
///
/// # smol::block_on(async {
/// let token = create_cancellation_token();
/// let result = with_cancellation(&token, async {
/// smol::Timer::after(Duration::from_millis(10)).await;
/// 42
/// }).await;
/// assert_eq!(result, Some(42));
/// # });
/// ```
pub async
/// Parking lot mutex helper.
///
/// Creates a new mutex with the given initial value.
///
/// # Parameters
///
/// * `value` - The initial value to store in the mutex.
///
/// # Type Parameters
///
/// * `T` - The type of the value.
///
/// # Returns
///
/// A new `Mutex<T>` instance.
///
/// # Examples
///
/// ```rust
/// use trash_utilities::async::core::create_mutex;
///
/// let mutex = create_mutex(42);
/// let value = mutex.lock();
/// assert_eq!(*value, 42);
/// ```