Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ use std::thread;
use futures_core::ready;
use once_cell::sync::Lazy;
use pin_project_lite::pin_project;
use tokio::runtime::EnterGuard;

/// Applies the [`Compat`] adapter to futures and I/O types.
pub trait CompatExt {
Expand Down Expand Up @@ -453,6 +454,20 @@ impl<T: futures_io::AsyncSeek> tokio::io::AsyncSeek for Compat<T> {
}
}

/// Wrapper around the [`EnterGuard`] type.
pub struct TokioRuntimeGuard(EnterGuard<'static>);

/// Manually enter the tokio runtime.
///
/// This function returns the [`EnterGuard`] which keeps the tokio runtime active until it is
/// dropped. Use this function when you cannot use one of the preexisting wrappers this crate
/// provides.
///
/// TODO: Examples
pub fn enter_tokio_runtime() -> TokioRuntimeGuard {
TokioRuntimeGuard(get_runtime_handle().enter())
}

fn get_runtime_handle() -> tokio::runtime::Handle {
tokio::runtime::Handle::try_current().unwrap_or_else(|_| TOKIO1.handle().clone())
}
Expand Down
Loading