1 use std::future::Future; 2 use std::sync::Arc; 3 use std::task::{Context, Poll, Wake}; 4 use std::thread::{self, Thread}; 5 6 /// A waker that wakes up the current thread when called. 7 struct ThreadWaker(Thread); 8 9 impl Wake for ThreadWaker { wake(self: Arc<Self>)10 fn wake(self: Arc<Self>) { 11 self.0.unpark(); 12 } 13 } 14 15 /// Run a future to completion on the current thread. block_on<T>(fut: impl Future<Output = T>) -> T16fn block_on<T>(fut: impl Future<Output = T>) -> T { 17 // Pin the future so it can be polled. 18 let mut fut = Box::pin(fut); 19 20 // Create a new context to be passed to the future. 21 let t = thread::current(); 22 let waker = Arc::new(ThreadWaker(t)).into(); 23 let mut cx = Context::from_waker(&waker); 24 25 // Run the future to completion. 26 loop { 27 match fut.as_mut().poll(&mut cx) { 28 Poll::Ready(res) => return res, 29 Poll::Pending => thread::park(), 30 } 31 } 32 } 33 edition() -> i3234async fn edition() -> i32 { 35 2018 36 } 37 main()38pub fn main() { 39 println!("{}", block_on(edition())); 40 } 41