macro_rules! dispatch_or_permit {
($semaphore:expr, $closure:expr) => { ... };
}Expand description
Attempt to dispatch a blocking operation, or otherwise obtain a permit and run on thread, returning the result of the closure.
This helper macro is intended for use in the context of an async block or
function. It first attempts to dispatch the closure via
dispatch_rx() and await. If a dispatch pool is not
registered on the current thread, it instead obtains a permit via
blocking_permit_future() and awaits. If
the tokio-threaded feature is enabled, it will then run the closure via
BlockingPermit::run(). Otherwise it will
run the closure directly.
ยงUsage
The closure should return a Result<T, E> where From<Canceled> is
implemented for type E. The return type of the closure may be annotated as
necessary. The closure may also be a move closure.
async {
dispatch_or_permit!(&semaphore, move || { /*.. blocking code..*/ });
}