Echo/Task/Task.rs
1//! # Task Struct
2//!
3//! Defines the structure of a schedulable task to be executed by a `Worker`.
4
5#![allow(non_snake_case, non_camel_case_types)]
6
7use std::{future::Future, pin::Pin};
8
9use crate::{
10 Queue::StealingQueue::{Prioritized, Priority as QueuePriority},
11 Task::Priority::Priority,
12};
13
14/// Defines a dynamic, asynchronous operation that can be sent between threads.
15/// This is a type alias for a boxed, pinned, send-safe future.
16pub type Operation = Pin<Box<dyn Future<Output = ()> + Send>>;
17
18/// Represents a single, schedulable unit of work for the `Echo` scheduler.
19///
20/// This struct encapsulates an asynchronous operation along with metadata,
21
22/// such as its `Priority`, that the scheduler uses to determine execution
23/// order.
24pub struct Task {
25 /// The asynchronous operation to be executed by a worker thread.
26 pub Operation:Operation,
27
28 /// The priority level of this task.
29 pub Priority:Priority,
30}
31
32impl Task {
33 /// Creates a new `Task` from a given future and priority level.
34 pub fn Create<F>(Operation:F, Priority:Priority) -> Self
35 where
36 F: Future<Output = ()> + Send + 'static, {
37 Self { Operation:Box::pin(Operation), Priority }
38 }
39}
40
41/// Implements the `Prioritized` trait required by the generic `StealingQueue`.
42///
43/// This implementation provides the bridge between the application-specific
44/// `Task::Priority` and the generic `Queue::StealingQueue::Priority` used
45/// internally by the queue system.
46impl Prioritized for Task {
47 /// The kind of priority used by the queue.
48 type Kind = QueuePriority;
49
50 /// Translates this task's specific priority into the queue's generic
51 /// priority enum, allowing the queue to place it in the correct deque.
52 fn Rank(&self) -> Self::Kind {
53 match self.Priority {
54 Priority::High => QueuePriority::High,
55
56 Priority::Normal => QueuePriority::Normal,
57
58 Priority::Low => QueuePriority::Low,
59 }
60 }
61}