noop_executor/
lib.rs

1//! No-op executors.
2
3#![no_std]
4
5use core::{
6    pin::pin,
7    task::{Context, Poll, Waker},
8};
9
10/// Blocks on a future.
11///
12/// It panics if a future is not ready.
13pub fn block_on<T>(future: impl Future<Output = T>) -> T {
14    let Poll::Ready(result) = pin!(future).poll(&mut Context::from_waker(Waker::noop())) else {
15        unreachable!()
16    };
17
18    result
19}
20
21#[cfg(test)]
22mod tests {
23    use super::*;
24    use core::future::{pending, ready};
25
26    #[test]
27    fn resolve_ready_future() {
28        assert_eq!(block_on(async { 42 }), 42);
29        assert_eq!(block_on(ready(42)), 42);
30    }
31
32    #[test]
33    #[should_panic]
34    fn panic_on_pending_future() {
35        assert_eq!(block_on(pending::<usize>()), 42);
36    }
37}