-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use HandleOrRuntime to allow alloydb/ethersdb to hold a custom runtime (
#1576) * Use HandleOrRuntime to allow alloydb/ethersdb to hold a custom runtime * Minor fix and clippy * Clippy * allow users to provide a runtime handle * Update docs * Fix slashes
- Loading branch information
Showing
4 changed files
with
119 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use tokio::runtime::{Handle, Runtime}; | ||
|
||
// Hold a tokio runtime handle or full runtime | ||
#[derive(Debug)] | ||
pub(crate) enum HandleOrRuntime { | ||
Handle(Handle), | ||
Runtime(Runtime), | ||
} | ||
|
||
impl HandleOrRuntime { | ||
#[inline] | ||
pub(crate) fn block_on<F>(&self, f: F) -> F::Output | ||
where | ||
F: std::future::Future + Send, | ||
F::Output: Send, | ||
{ | ||
match self { | ||
Self::Handle(handle) => tokio::task::block_in_place(move || handle.block_on(f)), | ||
Self::Runtime(rt) => rt.block_on(f), | ||
} | ||
} | ||
} |