pub struct BlockingAsyncProducer<F, Fut>{ /* private fields */ }Expand description
Adapter that wraps an async fn returning a Report and
implements dev_report::Producer by calling
tokio::runtime::Handle::current().block_on(...).
MUST be invoked from a sync context that is not itself running
inside a current_thread runtime. Calling block_on from inside
an async runtime would deadlock; if you need that, use
AsyncProducer directly without going through Producer.
§Example
use dev_async::{run_with_timeout, BlockingAsyncProducer};
use dev_report::{Producer, Report};
use std::time::Duration;
fn build_report() -> impl std::future::Future<Output = Report> {
async {
let check = run_with_timeout("op", Duration::from_millis(50), async {}).await;
let mut r = Report::new("crate", "0.1.0").with_producer("dev-async");
r.push(check);
r.finish();
r
}
}
// From a sync test or main:
// let rt = tokio::runtime::Runtime::new().unwrap();
// let handle = rt.handle().clone();
// let producer = BlockingAsyncProducer::new(handle, build_report);
// let report = producer.produce();Implementations§
Source§impl<F, Fut> BlockingAsyncProducer<F, Fut>
impl<F, Fut> BlockingAsyncProducer<F, Fut>
Sourcepub fn new(handle: Handle, factory: F) -> Self
pub fn new(handle: Handle, factory: F) -> Self
Build a new adapter bound to an externally-supplied handle.
factory is invoked once per produce() call and must return
a fresh future each time.
Use this when you already have a tokio::runtime::Handle
(e.g. from a long-lived runtime in your test harness). For the
common case of “I just want to drive an async producer from a
sync test”, prefer with_new_runtime.
Sourcepub fn with_new_runtime(factory: F) -> Result<Self>
pub fn with_new_runtime(factory: F) -> Result<Self>
Build a new adapter that owns a fresh multi-thread tokio::runtime::Runtime.
The runtime lives for the lifetime of the producer and is dropped (along with all its workers) when the producer is dropped. Use this when you don’t already have a runtime and just want to drive an async producer from sync code.
§Example
use dev_async::{run_with_timeout, BlockingAsyncProducer};
use dev_report::{Producer, Report};
use std::time::Duration;
let producer = BlockingAsyncProducer::with_new_runtime(|| async {
let check = run_with_timeout("op", Duration::from_millis(50), async {}).await;
let mut r = Report::new("crate", "0.1.0").with_producer("dev-async");
r.push(check);
r.finish();
r
})
.expect("build runtime");
let _report = producer.produce();Sourcepub fn with_current_thread_runtime(factory: F) -> Result<Self>
pub fn with_current_thread_runtime(factory: F) -> Result<Self>
Build a new adapter that owns a fresh current_thread
tokio::runtime::Runtime.
Lighter-weight than with_new_runtime:
no worker threads are spawned. Suitable for tests and
single-threaded harnesses.
§Example
use dev_async::BlockingAsyncProducer;
use dev_report::{Producer, Report};
let producer = BlockingAsyncProducer::with_current_thread_runtime(|| async {
Report::new("c", "0.1.0").with_producer("dev-async")
})
.expect("build runtime");
let _r = producer.produce();