pub fn batch<T>(
requests: impl IntoIterator<Item = impl Future<Output = T>>,
) -> Vec<T>
Expand description
Batches a set of requests that will be sent to the compositor all at once.
§Rationale
Normally, all API calls are blocking. For example, calling window::get_all
then calling WindowHandle::app_id
on each returned window handle will block after each app_id
call waiting for the compositor to respond.
In order to mitigate this issue, you can batch up a set of API calls using this function. This will send all requests to the compositor at once without blocking, then wait for the compositor to respond.
You’ll see that this function takes in an IntoIterator
of Future
s. As such,
most API calls that return something have an async variant named *_async
that returns a future.
You must pass these futures into the batch function instead of their non-async counterparts.
§The batch_boxed
macro
The util
module also provides the batch_boxed
macro.
The batch
function only accepts one concrete type of future, meaning that you
can only batch a collection of futures from one specific function or method.
As a convenience, batch_boxed
accepts one or more different futures that return the same type.
It will place provided futures in a Pin<Box<_>>
to erase the types and pass them along to batch
.
§Examples
let windows = window::get_all().collect::<Vec<_>>();
let props: Vec<String> = batch(windows.iter().map(|window| window.app_id_async()));