SHARE
TWEET

Untitled

a guest Aug 14th, 2019 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![feature(async_await)]
  2. extern crate futures;
  3.  
  4. use {
  5.     futures::{
  6.         future::{FutureExt, BoxFuture},
  7.         task::{ArcWake, waker_ref},
  8.     },
  9.     std::{
  10.         future::Future,
  11.         sync::{Arc, Mutex},
  12.         sync::mpsc::{sync_channel, SyncSender, Receiver},
  13.         task::{Context, Poll},
  14.         time::Duration,
  15.     },
  16. };
  17.  
  18.  
  19. // Async proccesses
  20. struct WaitingForGodotTask {
  21.     future: Mutex<Option<BoxFuture<()>>>,
  22.     task_sender: SyncSender<Arc<WaitingForGodotTask>>,
  23. }
  24.  
  25. #[derive(Debug)]
  26. struct Godot;
  27.  
  28. impl Godot { async fn new() -> Godot { let g = Godot{}; loop {}; g  } }
  29.  
  30. impl Default for Godot { fn default() -> Self {  Godot{} } }
  31.  
  32. impl Future for Godot {
  33.     type Output = Self;
  34.     fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
  35.         if true { Poll::Pending } else { Poll::Ready(Godot::default()) }
  36.     }
  37. }
  38.  
  39. // Runtime
  40. #[derive(Debug)]
  41. struct MadamExecutioner  { recvs: Receiver<Arc<WaitingForGodotTask>> }
  42. impl MadamExecutioner {
  43.     fn run(&self) {
  44.         futures::executor::spawn(move|| {
  45.             async { Godot::new(); };
  46.         });
  47.     }
  48. }
  49.  
  50. #[derive(Clone)]
  51. struct Spawner { task_sender: SyncSender<Arc<WaitingForGodotTask>>, }
  52.  
  53. impl ArcWake for WaitingForGodotTask {
  54.     fn wake_by_ref(arc_self: &Arc<Self>) {
  55.         // Implement `wake` by sending this task back onto the task channel
  56.         // so that it will be polled again by the executor.
  57.         let cloned = arc_self.clone();
  58.         arc_self.task_sender.send(cloned).expect("too many tasks queued");
  59.     }
  60. }
  61.  
  62. fn main() {
  63.     let (task_sender, ready_queue) = sync_channel(5);
  64.     let me = MadamExecutioner { recvs: ready_queue };
  65.     dbg!(me);
  66. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top