rart_rs/common/
task.rs

1extern crate alloc;
2
3use core::cell::UnsafeCell;
4use core::future::Future;
5use core::pin::Pin;
6use crate::common::UnsafeOption;
7use alloc::boxed::Box;
8use crate::RARTError;
9
10pub type TaskId = usize;
11
12pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output=T> + 'a>>;
13pub type TaskResult = Result<(), RARTError>;
14
15pub struct Task {
16    id: TaskId,
17    future: UnsafeOption<BoxFuture<'static, TaskResult>>,
18}
19
20// TODO Explain why this is safe
21unsafe impl Sync for Task {}
22
23impl Task {
24    pub fn new(id: TaskId, future: impl Future<Output=TaskResult> + 'static) -> Task {
25        let box_ = Box::pin(future);
26
27        let task = Task {
28            id,
29            future: UnsafeCell::new(Some(box_)),
30        };
31
32        task
33    }
34
35    pub fn id(&self) -> TaskId {
36        self.id
37    }
38
39    pub fn future(&self) -> &UnsafeOption<BoxFuture<'static, TaskResult>> {
40        &self.future
41    }
42}