xref: /aosp_15_r20/external/bazelbuild-rules_rust/test/rustfmt/srcs/2018/unformatted.rs (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1 use std::future::Future; use std::sync::Arc; use std::task::{Context, Poll, Wake}; use std::thread::{self, Thread};
2 /// A waker that wakes up the current thread when called.
3 struct ThreadWaker(Thread);
wake(self: Arc<Self>)4 impl Wake for ThreadWaker {fn wake(self: Arc<Self>) {self.0.unpark();}}
5 /// Run a future to completion on the current thread.
block_on<T>(fut: impl Future<Output = T>) -> T6 fn block_on<T>(fut: impl Future<Output = T>) -> T {
7 // Pin the future so it can be polled.
8 let mut fut = Box::pin(fut);
9 // Create a new context to be passed to the future.
10 let t = thread::current();let waker = Arc::new(ThreadWaker(t)).into();
11 let mut cx = Context::from_waker(&waker);
12 // Run the future to completion.
13 loop {match fut.as_mut().poll(&mut cx) {
14 Poll::Ready(res) => return res, Poll::Pending => thread::park(),
15 }
16 }
17 }
edition() -> i3218 async fn edition() -> i32 {2018}
main()19 pub fn main(){println!("{}", block_on(edition()));}
20