Skip to content

Commit e49c4a7

Browse files
committed
feat: add a 'no-async' runtime
1 parent 4e29dfa commit e49c4a7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

opentelemetry-sdk/src/runtime.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,31 @@ impl RuntimeChannel for TokioCurrentThread {
245245
)
246246
}
247247
}
248+
249+
/// Runtime implementation for synchronous execution environments.
250+
///
251+
/// This runtime can be used when executing in a non-async environment.
252+
/// The runtime methods will perform their operations synchronously.
253+
#[cfg(feature = "experimental_async_runtime")]
254+
#[derive(Debug, Clone, Copy)]
255+
pub struct NoAsync;
256+
257+
#[cfg(feature = "experimental_async_runtime")]
258+
impl Runtime for NoAsync {
259+
fn spawn<F>(&self, future: F)
260+
where
261+
F: Future<Output = ()> + Send + 'static,
262+
{
263+
std::thread::spawn(move || {
264+
futures_executor::block_on(future);
265+
});
266+
}
267+
268+
// Needed because async fn would borrow `self`, violating the `'static` requirement.
269+
#[allow(clippy::manual_async_fn)]
270+
fn delay(&self, duration: Duration) -> impl Future<Output = ()> + Send + 'static {
271+
async move {
272+
std::thread::sleep(duration);
273+
}
274+
}
275+
}

0 commit comments

Comments
 (0)