tokio_fusion/task/
task_impl.rs

1use std::future::Future;
2
3use futures::future::BoxFuture;
4
5use crate::error::ThreadPoolResult;
6
7/// A task that can be submitted to the thread pool
8pub struct Task<T> {
9    pub(crate) future: BoxFuture<'static, ThreadPoolResult<T>>,
10    pub(crate) priority: usize,
11}
12
13impl<T: Send + 'static> Task<T> {
14    /// Create a new task with the given future and priority
15    pub fn new<F>(future: F, priority: usize) -> Self
16    where
17        F: Future<Output = ThreadPoolResult<T>> + Send + 'static,
18    {
19        Self {
20            future: Box::pin(future),
21            priority,
22        }
23    }
24
25    /// Create a new task with the given closure and priority
26    pub fn from_fn<F>(f: F, priority: usize) -> Self
27    where
28        F: FnOnce() -> ThreadPoolResult<T> + Send + 'static,
29    {
30        Self::new(async move { f() }, priority)
31    }
32}