Echo/Task/
Task.rs

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