daily pastebin goal
74%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![feature(
  2.     arbitrary_self_types,
  3.     async_await,
  4.     await_macro,
  5.     futures_api,
  6.     pin
  7. )]
  8.  
  9. extern crate futures;
  10.  
  11. use futures::{channel::mpsc, prelude::*};
  12. use std::{cell::RefCell, collections::LinkedList};
  13.  
  14. pub use futures::{channel::mpsc::SendError, task::SpawnError};
  15.  
  16. type LocalMessage = Box<Send + 'static>;
  17. type LocalReceiver = mpsc::Receiver<LocalMessage>;
  18.  
  19. struct LocalChannel {
  20.     receiver: LocalReceiver,
  21.     waiting:  LinkedList<LocalMessage>,
  22. }
  23.  
  24. thread_local! {
  25.     static MY_CHANNEL: RefCell<Option<LocalChannel>> = RefCell::new(None);
  26. }
  27.  
  28. pub fn spawn<Fut>(_fut: Fut) -> impl Future<Output = Result<(), SpawnError>>
  29. where
  30.     Fut: Future<Output = ()> + Send + 'static,
  31. {
  32.     future::lazy(|_| Ok(()))
  33. }
  34.  
  35. #[doc(hidden)]
  36. pub async fn __receive<WantFn, Fut>(want: WantFn) -> LocalMessage
  37. where
  38.     Fut: Future<Output = bool>,
  39.     WantFn: Fn(&LocalMessage) -> Fut,
  40. {
  41.     let mut chan = MY_CHANNEL.with(|c| c.borrow_mut().take()).unwrap();
  42.  
  43.     loop {
  44.         let msg = await!(chan.receiver.next()).unwrap();
  45.         if await!(want(&msg)) {
  46.             MY_CHANNEL.with(|c| *c.borrow_mut() = Some(chan));
  47.             return msg;
  48.         }
  49.         chan.waiting.push_back(msg);
  50.     }
  51. }
  52.  
  53. fn main() {
  54.     futures::executor::ThreadPool::new()
  55.         .unwrap()
  56.         .run(spawn(
  57.             async {
  58.                 await!(__receive(|_| async { false }));
  59.             },
  60.         )).unwrap();
  61. }
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