Hide the implementation of Task
(#22009)
The `Option<T>` within `Ready` is confusing and using `None` for it can cause crashes. There was actually one instance of this! Release Notes: - N/A
This commit is contained in:
parent
1ac60289fe
commit
c5fe6ef100
6 changed files with 28 additions and 16 deletions
|
@ -50,7 +50,10 @@ pub struct ForegroundExecutor {
|
|||
/// the task to continue running, but with no way to return a value.
|
||||
#[must_use]
|
||||
#[derive(Debug)]
|
||||
pub enum Task<T> {
|
||||
pub struct Task<T>(TaskState<T>);
|
||||
|
||||
#[derive(Debug)]
|
||||
enum TaskState<T> {
|
||||
/// A task that is ready to return a value
|
||||
Ready(Option<T>),
|
||||
|
||||
|
@ -61,14 +64,23 @@ pub enum Task<T> {
|
|||
impl<T> Task<T> {
|
||||
/// Creates a new task that will resolve with the value
|
||||
pub fn ready(val: T) -> Self {
|
||||
Task::Ready(Some(val))
|
||||
Task(TaskState::Ready(Some(val)))
|
||||
}
|
||||
|
||||
/// Returns the task's result if it is already know. The only known usecase for this is for
|
||||
/// skipping spawning another task that awaits on this one.
|
||||
pub fn get_ready(self) -> Option<T> {
|
||||
match self {
|
||||
Task(TaskState::Ready(val)) => val,
|
||||
Task(TaskState::Spawned(_)) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Detaching a task runs it to completion in the background
|
||||
pub fn detach(self) {
|
||||
match self {
|
||||
Task::Ready(_) => {}
|
||||
Task::Spawned(task) => task.detach(),
|
||||
Task(TaskState::Ready(_)) => {}
|
||||
Task(TaskState::Spawned(task)) => task.detach(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -94,8 +106,8 @@ impl<T> Future for Task<T> {
|
|||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
match unsafe { self.get_unchecked_mut() } {
|
||||
Task::Ready(val) => Poll::Ready(val.take().unwrap()),
|
||||
Task::Spawned(task) => task.poll(cx),
|
||||
Task(TaskState::Ready(val)) => Poll::Ready(val.take().unwrap()),
|
||||
Task(TaskState::Spawned(task)) => task.poll(cx),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -163,7 +175,7 @@ impl BackgroundExecutor {
|
|||
let (runnable, task) =
|
||||
async_task::spawn(future, move |runnable| dispatcher.dispatch(runnable, label));
|
||||
runnable.schedule();
|
||||
Task::Spawned(task)
|
||||
Task(TaskState::Spawned(task))
|
||||
}
|
||||
|
||||
/// Used by the test harness to run an async test in a synchronous fashion.
|
||||
|
@ -340,7 +352,7 @@ impl BackgroundExecutor {
|
|||
move |runnable| dispatcher.dispatch_after(duration, runnable)
|
||||
});
|
||||
runnable.schedule();
|
||||
Task::Spawned(task)
|
||||
Task(TaskState::Spawned(task))
|
||||
}
|
||||
|
||||
/// in tests, start_waiting lets you indicate which task is waiting (for debugging only)
|
||||
|
@ -460,7 +472,7 @@ impl ForegroundExecutor {
|
|||
dispatcher.dispatch_on_main_thread(runnable)
|
||||
});
|
||||
runnable.schedule();
|
||||
Task::Spawned(task)
|
||||
Task(TaskState::Spawned(task))
|
||||
}
|
||||
inner::<R>(dispatcher, Box::pin(future))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue