some_executor 0.6.3

A trait for libraries that abstract over any executor
Documentation
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Task configuration types for the some_executor framework.
//!
//! This module provides configuration types that allow fine-grained control over task execution
//! behavior. Configurations convey scheduling hints, priorities, and timing constraints to
//! executors, enabling them to make optimal scheduling decisions based on task characteristics.
//!
//! # Overview
//!
//! The configuration system consists of two main types:
//!
//! - [`Configuration`]: The immutable configuration data that accompanies a task
//! - [`ConfigurationBuilder`]: A builder pattern for constructing configurations fluently
//!
//! Configurations influence how executors schedule and prioritize tasks but do not guarantee
//! specific behavior - executors may interpret these hints based on their implementation.
//!
//! # Examples
//!
//! ## Basic Configuration
//!
//! ```
//! use some_executor::task::{Configuration, ConfigurationBuilder};
//! use some_executor::hint::Hint;
//! use some_executor::Priority;
//! use some_executor::Instant;
//!
//! // Direct construction when all values are known
//! let config = Configuration::new(
//!     Hint::CPU,
//!     Priority::unit_test(),
//!     Instant::now()
//! );
//!
//! // Builder pattern for flexible construction
//! let config = ConfigurationBuilder::new()
//!     .hint(Hint::IO)
//!     .priority(Priority::unit_test())
//!     .build();
//! ```
//!
//! ## Delayed Task Execution
//!
//! ```
//! use some_executor::task::ConfigurationBuilder;
//! use some_executor::Instant;
//! use std::time::Duration;
//!
//! // Schedule a task to run after a delay
//! let delayed_config = ConfigurationBuilder::new()
//!     .poll_after(Instant::now() + Duration::from_secs(5))
//!     .build();
//! ```

use crate::Priority;
use crate::hint::Hint;

/// Configuration for task execution.
///
/// `Configuration` encapsulates metadata that guides executor scheduling decisions.
/// Each configuration consists of:
///
/// - **Hint**: Indicates the expected behavior of the task (CPU-bound, I/O-bound, etc.)
/// - **Priority**: Determines scheduling precedence when multiple tasks are ready
/// - **Poll After**: Specifies the earliest time a task should be polled
///
/// Configurations are immutable once created and can be shared across multiple tasks.
/// Executors use this information as hints for optimization but are not required to
/// strictly follow them.
///
/// # Examples
///
/// ## Creating a configuration for a CPU-intensive task
///
/// ```
/// use some_executor::task::Configuration;
/// use some_executor::hint::Hint;
/// use some_executor::Priority;
/// use some_executor::Instant;
///
/// let config = Configuration::new(
///     Hint::CPU,
///     Priority::unit_test(),
///     Instant::now()
/// );
///
/// assert_eq!(config.hint(), Hint::CPU);
/// assert_eq!(config.priority(), Priority::unit_test());
/// ```
///
/// ## Using default configuration
///
/// ```
/// use some_executor::task::Configuration;
/// use some_executor::hint::Hint;
/// use some_executor::Priority;
///
/// let config = Configuration::default();
///
/// // Default values are sensible for general use
/// assert_eq!(config.hint(), Hint::Unknown);
/// assert_eq!(config.priority(), Priority::Unknown);
/// ```
///
/// ## Comparing configurations
///
/// ```
/// use some_executor::task::Configuration;
/// use some_executor::hint::Hint;
/// use some_executor::Priority;
/// use some_executor::Instant;
///
/// let config1 = Configuration::new(
///     Hint::IO,
///     Priority::unit_test(),
///     Instant::now()
/// );
///
/// let config2 = config1.clone();
/// assert_eq!(config1, config2);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Configuration {
    hint: Hint,
    priority: priority::Priority,
    poll_after: crate::sys::Instant,
}

/// A builder for creating [`Configuration`] instances.
///
/// `ConfigurationBuilder` provides a fluent API for constructing task configurations
/// with optional settings. This pattern allows for flexible configuration creation
/// where only the relevant parameters need to be specified.
///
/// # Default Values
///
/// When built, any unspecified values will use these defaults:
/// - `hint`: [`Hint::Unknown`]
/// - `priority`: [`Priority::Unknown`]
/// - `poll_after`: The current instant (via `Instant::now()`)
///
/// # Examples
///
/// ## Building with all parameters
///
/// ```
/// use some_executor::task::ConfigurationBuilder;
/// use some_executor::hint::Hint;
/// use some_executor::Priority;
/// use some_executor::Instant;
/// use std::time::Duration;
///
/// let config = ConfigurationBuilder::new()
///     .hint(Hint::CPU)
///     .priority(Priority::unit_test())
///     .poll_after(Instant::now() + Duration::from_secs(2))
///     .build();
///
/// assert_eq!(config.hint(), Hint::CPU);
/// assert_eq!(config.priority(), Priority::unit_test());
/// ```
///
/// ## Building with partial parameters
///
/// ```
/// use some_executor::task::ConfigurationBuilder;
/// use some_executor::hint::Hint;
/// use some_executor::Priority;
///
/// // Only set what's needed; others use defaults
/// let config = ConfigurationBuilder::new()
///     .hint(Hint::IO)
///     .build();
///
/// assert_eq!(config.hint(), Hint::IO);
/// assert_eq!(config.priority(), Priority::Unknown); // Default
/// ```
///
/// ## Using the Default trait
///
/// ```
/// use some_executor::task::ConfigurationBuilder;
/// use some_executor::hint::Hint;
///
/// // ConfigurationBuilder implements Default
/// let config = ConfigurationBuilder::default()
///     .hint(Hint::CPU)
///     .build();
///
/// assert_eq!(config.hint(), Hint::CPU);
/// ```
///
/// ## Chaining multiple builder calls
///
/// ```
/// use some_executor::task::ConfigurationBuilder;
/// use some_executor::hint::Hint;
/// use some_executor::Priority;
/// use some_executor::Instant;
/// use std::time::Duration;
///
/// let base_time = Instant::now();
/// let config = ConfigurationBuilder::new()
///     .hint(Hint::IO)
///     .priority(Priority::unit_test())
///     .poll_after(base_time + Duration::from_millis(100))
///     .build();
///
/// // All values are set as expected
/// assert_eq!(config.hint(), Hint::IO);
/// assert_eq!(config.priority(), Priority::unit_test());
/// assert!(config.poll_after() >= base_time);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ConfigurationBuilder {
    hint: Option<Hint>,
    priority: Option<Priority>,
    poll_after: Option<crate::sys::Instant>,
}

impl ConfigurationBuilder {
    /// Creates a new `ConfigurationBuilder` with no values set.
    ///
    /// All fields will be `None` until explicitly set via the builder methods.
    /// This is equivalent to calling [`ConfigurationBuilder::default()`].
    ///
    /// # Examples
    ///
    /// ```
    /// use some_executor::task::ConfigurationBuilder;
    ///
    /// let builder = ConfigurationBuilder::new();
    /// let config = builder.build();
    ///
    /// // All fields will have default values
    /// use some_executor::hint::Hint;
    /// use some_executor::Priority;
    /// assert_eq!(config.hint(), Hint::Unknown);
    /// assert_eq!(config.priority(), Priority::Unknown);
    /// ```
    pub fn new() -> Self {
        ConfigurationBuilder {
            hint: None,
            priority: None,
            poll_after: None,
        }
    }

    /// Sets the execution hint for the task.
    ///
    /// Hints provide guidance to executors about the expected behavior of the task,
    /// allowing for more efficient scheduling decisions.
    ///
    /// # Arguments
    ///
    /// * `hint` - The execution hint for the task
    ///
    /// # Examples
    ///
    /// ```
    /// use some_executor::task::ConfigurationBuilder;
    /// use some_executor::hint::Hint;
    ///
    /// let config = ConfigurationBuilder::new()
    ///     .hint(Hint::IO)
    ///     .build();
    /// ```
    pub fn hint(mut self, hint: Hint) -> Self {
        self.hint = Some(hint);
        self
    }

    /// Sets the priority for the task.
    ///
    /// Priority influences scheduling when multiple tasks are ready to run.
    /// Higher priority tasks are generally scheduled before lower priority ones.
    ///
    /// # Arguments
    ///
    /// * `priority` - The priority level for the task
    ///
    /// # Examples
    ///
    /// ```
    /// use some_executor::task::ConfigurationBuilder;
    /// use some_executor::Priority;
    ///
    /// let config = ConfigurationBuilder::new()
    ///     .priority(Priority::unit_test())
    ///     .build();
    /// ```
    pub fn priority(mut self, priority: priority::Priority) -> Self {
        self.priority = Some(priority);
        self
    }

    /// Sets the earliest time the task should be polled.
    ///
    /// This can be used to delay task execution or implement scheduled tasks.
    /// Executors will not poll the task before this time.
    ///
    /// # Arguments
    ///
    /// * `poll_after` - The instant after which the task can be polled
    ///
    /// # Examples
    ///
    /// ```
    /// use some_executor::task::ConfigurationBuilder;
    /// use some_executor::Instant;
    /// use std::time::Duration;
    ///
    /// // Delay task execution by 5 seconds
    /// let config = ConfigurationBuilder::new()
    ///     .poll_after(Instant::now() + Duration::from_secs(5))
    ///     .build();
    /// ```
    pub fn poll_after(mut self, poll_after: crate::sys::Instant) -> Self {
        self.poll_after = Some(poll_after);
        self
    }

    /// Builds the final [`Configuration`] instance.
    ///
    /// Consumes the builder and produces an immutable `Configuration`.
    /// Any unset values will use their defaults:
    /// - `hint`: [`Hint::Unknown`] (via `Hint::default()`)
    /// - `priority`: [`Priority::Unknown`]
    /// - `poll_after`: The current instant (via `Instant::now()`)
    ///
    /// # Examples
    ///
    /// ```
    /// use some_executor::task::ConfigurationBuilder;
    /// use some_executor::hint::Hint;
    /// use some_executor::Priority;
    ///
    /// // Building with no values set uses all defaults
    /// let config = ConfigurationBuilder::new().build();
    /// assert_eq!(config.hint(), Hint::Unknown);
    /// assert_eq!(config.priority(), Priority::Unknown);
    ///
    /// // Building with some values set
    /// let config = ConfigurationBuilder::new()
    ///     .hint(Hint::CPU)
    ///     .build();
    /// assert_eq!(config.hint(), Hint::CPU);
    /// assert_eq!(config.priority(), Priority::Unknown); // Still default
    /// ```
    pub fn build(self) -> Configuration {
        Configuration {
            hint: self.hint.unwrap_or_default(),
            priority: self.priority.unwrap_or(priority::Priority::Unknown),
            poll_after: self.poll_after.unwrap_or_else(crate::sys::Instant::now),
        }
    }
}

impl Configuration {
    /// Creates a new `Configuration` with the specified values.
    ///
    /// This is an alternative to using [`ConfigurationBuilder`] when you have all
    /// values available upfront.
    ///
    /// # Arguments
    ///
    /// * `hint` - The execution hint for the task
    /// * `priority` - The priority level for the task
    /// * `poll_after` - The earliest time the task should be polled
    ///
    /// # Examples
    ///
    /// ```
    /// use some_executor::task::Configuration;
    /// use some_executor::hint::Hint;
    /// use some_executor::Priority;
    /// use some_executor::Instant;
    ///
    /// let config = Configuration::new(
    ///     Hint::CPU,
    ///     Priority::unit_test(),
    ///     Instant::now()
    /// );
    /// ```
    pub fn new(hint: Hint, priority: priority::Priority, poll_after: crate::sys::Instant) -> Self {
        Configuration {
            hint,
            priority,
            poll_after,
        }
    }

    /// Returns the execution hint for this configuration.
    ///
    /// The hint indicates the expected runtime characteristics of the task,
    /// such as whether it's CPU-bound or I/O-bound.
    ///
    /// # Examples
    ///
    /// ```
    /// use some_executor::task::Configuration;
    /// use some_executor::hint::Hint;
    /// use some_executor::Priority;
    /// use some_executor::Instant;
    ///
    /// let config = Configuration::new(
    ///     Hint::IO,
    ///     Priority::unit_test(),
    ///     Instant::now()
    /// );
    ///
    /// assert_eq!(config.hint(), Hint::IO);
    /// ```
    pub fn hint(&self) -> Hint {
        self.hint
    }

    /// Returns the priority for this configuration.
    ///
    /// The priority influences task scheduling order when multiple tasks
    /// are ready to execute.
    ///
    /// # Examples
    ///
    /// ```
    /// use some_executor::task::Configuration;
    /// use some_executor::hint::Hint;
    /// use some_executor::Priority;
    /// use some_executor::Instant;
    ///
    /// let config = Configuration::new(
    ///     Hint::CPU,
    ///     Priority::unit_test(),
    ///     Instant::now()
    /// );
    ///
    /// assert_eq!(config.priority(), Priority::unit_test());
    /// ```
    pub fn priority(&self) -> priority::Priority {
        self.priority
    }

    /// Returns the earliest time the task should be polled.
    ///
    /// Tasks will not be scheduled for execution before this instant,
    /// allowing for delayed or scheduled task execution.
    ///
    /// # Examples
    ///
    /// ```
    /// use some_executor::task::Configuration;
    /// use some_executor::hint::Hint;
    /// use some_executor::Priority;
    /// use some_executor::Instant;
    /// use std::time::Duration;
    ///
    /// let future_time = Instant::now() + Duration::from_secs(10);
    /// let config = Configuration::new(
    ///     Hint::Unknown,
    ///     Priority::Unknown,
    ///     future_time
    /// );
    ///
    /// assert!(config.poll_after() >= Instant::now());
    /// ```
    pub fn poll_after(&self) -> crate::sys::Instant {
        self.poll_after
    }
}

impl Default for Configuration {
    /// Creates a default configuration with sensible defaults.
    ///
    /// The default configuration uses:
    /// - `hint`: [`Hint::Unknown`]
    /// - `priority`: [`Priority::Unknown`]
    /// - `poll_after`: The current instant
    ///
    /// # Examples
    ///
    /// ```
    /// use some_executor::task::Configuration;
    /// use some_executor::hint::Hint;
    /// use some_executor::Priority;
    /// use some_executor::Instant;
    ///
    /// let config = Configuration::default();
    ///
    /// assert_eq!(config.hint(), Hint::Unknown);
    /// assert_eq!(config.priority(), Priority::Unknown);
    /// // poll_after will be approximately now
    /// assert!(config.poll_after() <= Instant::now() + std::time::Duration::from_millis(100));
    /// ```
    fn default() -> Self {
        Configuration {
            hint: Hint::default(),
            priority: priority::Priority::Unknown,
            poll_after: crate::sys::Instant::now(),
        }
    }
}