future_run.rs 789 B

12345678910111213141516171819202122232425262728293031323334
  1. use super::{Contexted, Run, RunState};
  2. use core::{
  3. pin::Pin,
  4. task::{Context, Poll, Waker},
  5. };
  6. pub struct FutureRun<F: Future>(F);
  7. impl<F> FutureRun<F>
  8. where
  9. F: Future,
  10. {
  11. pub const fn new(future: F) -> Self {
  12. Self(future)
  13. }
  14. }
  15. impl<F> Contexted for FutureRun<F> where F: Future {}
  16. impl<F> Run for FutureRun<F>
  17. where
  18. F: Future + 'static,
  19. {
  20. type Output = F::Output;
  21. fn run(self: Pin<&mut Self>, waker: &Waker) -> RunState<Self::Output> {
  22. let mut future = unsafe { self.map_unchecked_mut(|me| &mut me.0) };
  23. let mut context = Context::from_waker(waker);
  24. match future.as_mut().poll(&mut context) {
  25. Poll::Ready(output) => RunState::Finished(output),
  26. Poll::Pending => RunState::Running,
  27. }
  28. }
  29. }