use crate::{Scheduler, SessionId, Timer}; use std::{ future::Future, marker::PhantomData, pin::Pin, rc::Rc, sync::Arc, task::{Context, Poll}, time::Duration, }; #[derive(Clone)] pub struct ForegroundExecutor { session_id: SessionId, scheduler: Arc, not_send: PhantomData>, } impl ForegroundExecutor { pub fn spawn(&self, future: F) -> Task where F: Future + 'static, F::Output: 'static, { let session_id = self.session_id; let scheduler = Arc::clone(&self.scheduler); let (runnable, task) = async_task::spawn_local(future, move |runnable| { scheduler.schedule_foreground(session_id, runnable); }); runnable.schedule(); Task(TaskState::Spawned(task)) } pub fn timer(&self, duration: Duration) -> Timer { self.scheduler.timer(duration) } } impl ForegroundExecutor { pub fn new(session_id: SessionId, scheduler: Arc) -> Self { assert!( scheduler.is_main_thread(), "ForegroundExecutor must be created on the same thread as the Scheduler" ); Self { session_id, scheduler, not_send: PhantomData, } } } impl BackgroundExecutor { pub fn new(scheduler: Arc) -> Self { Self { scheduler } } } pub struct BackgroundExecutor { scheduler: Arc, } impl BackgroundExecutor { pub fn spawn(&self, future: F) -> Task where F: Future + Send + 'static, F::Output: Send + 'static, { let scheduler = Arc::clone(&self.scheduler); let (runnable, task) = async_task::spawn(future, move |runnable| { scheduler.schedule_background(runnable); }); runnable.schedule(); Task(TaskState::Spawned(task)) } pub fn block_on(&self, future: Fut) -> Fut::Output { self.scheduler.block_on(future) } pub fn block_with_timeout( &self, future: &mut Fut, timeout: Duration, ) -> Option { self.scheduler.block_with_timeout(future, timeout) } pub fn timer(&self, duration: Duration) -> Timer { self.scheduler.timer(duration) } } /// Task is a primitive that allows work to happen in the background. /// /// It implements [`Future`] so you can `.await` on it. /// /// If you drop a task it will be cancelled immediately. Calling [`Task::detach`] allows /// the task to continue running, but with no way to return a value. #[must_use] #[derive(Debug)] pub struct Task(TaskState); #[derive(Debug)] enum TaskState { /// A task that is ready to return a value Ready(Option), /// A task that is currently running. Spawned(async_task::Task), } impl Task { /// Creates a new task that will resolve with the value pub fn ready(val: T) -> Self { Task(TaskState::Ready(Some(val))) } /// Detaching a task runs it to completion in the background pub fn detach(self) { match self { Task(TaskState::Ready(_)) => {} Task(TaskState::Spawned(task)) => task.detach(), } } } impl Future for Task { type Output = T; fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { match unsafe { self.get_unchecked_mut() } { Task(TaskState::Ready(val)) => Poll::Ready(val.take().unwrap()), Task(TaskState::Spawned(task)) => Pin::new(task).poll(cx), } } }