run.rs 815 B

12345678910111213141516171819202122232425262728293031323334
  1. mod future_run;
  2. use core::{pin::Pin, task::Waker};
  3. pub use future_run::FutureRun;
  4. pub enum RunState<Output> {
  5. Running,
  6. Finished(Output),
  7. }
  8. pub trait Contexted {
  9. /// # Safety
  10. /// This function should be called in a preemption disabled context.
  11. fn load_running_context(&self) {}
  12. /// # Safety
  13. /// This function should be called in a preemption disabled context.
  14. fn restore_running_context(&self) {}
  15. }
  16. pub trait Run {
  17. type Output;
  18. fn run(self: Pin<&mut Self>, waker: &Waker) -> RunState<Self::Output>;
  19. fn join(mut self: Pin<&mut Self>, waker: &Waker) -> Self::Output {
  20. loop {
  21. match self.as_mut().run(waker) {
  22. RunState::Running => continue,
  23. RunState::Finished(output) => break output,
  24. }
  25. }
  26. }
  27. }