Crate worst_executor
source ·Expand description
The worst async executor you could think of.
This crate provides a single function, block_on
, which takes a future and
blocks the current thread until the future is resolved.
The way it works is by “spin-looping” over the poll
method until it is ready.
The nice thing about this is that it optimizes very well,
for example worst_executor::block_on(async { 42 })
compiles to a single mov
instruction.
The bad thing about this is that it does not actually do any scheduling, meaning that if you wait on a future that never resolves, your program will hang. which is why you should probably not use this.
Note that because of its simplicity, the library only uses core
and does not require std
or alloc
and is literally 14 lines of code.
This can become more useful using the core::future::join
macro
which allows you to wait on multiple futures at once (each time polling a different future).
Currently this library requires rust nightly for the pin
macro, the join
macro(used in tests) and the const_waker
feature (required for complete optimization of block_on
)
Examples
use worst_executor::block_on;
let val = block_on(async { 42 });
assert_eq!(val, 42);
#![feature(future_join)]
use worst_executor::block_on;
use core::future::join;
let (sender, receiver) = async_channel::bounded(1);
block_on(async {
let send_20_fut = async {
for i in 0..20 {
sender.send(i).await.unwrap();
}
};
let recv_20_fut = async {
for i in 0..20 {
assert_eq!(receiver.recv().await.unwrap(), i);
}
};
join!(send_20_fut, recv_20_fut).await;
});
Functions
poll
method until it is ready.