use std::fmt::Debug;
use vortex_error::VortexExpect;
use vortex_session::SessionExt;
use crate::runtime::Handle;
pub struct RuntimeSession {
handle: Option<Handle>,
}
impl Default for RuntimeSession {
fn default() -> Self {
Self {
handle: Handle::find(),
}
}
}
impl Debug for RuntimeSession {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RuntimeSession").finish_non_exhaustive()
}
}
pub trait RuntimeSessionExt: SessionExt {
fn handle(&self) -> Handle {
self.get::<RuntimeSession>().handle
.as_ref()
.vortex_expect("Runtime handle not configured in Vortex session. Please setup a `CurrentThreadRuntime`, or configure the session for `with_tokio`.")
.clone()
}
#[cfg(feature = "tokio")]
fn with_tokio(self) -> Self {
self.get_mut::<RuntimeSession>().handle =
Some(crate::runtime::tokio::TokioRuntime::current());
self
}
fn with_handle(self, handle: Handle) -> Self {
self.get_mut::<RuntimeSession>().handle = Some(handle);
self
}
}
impl<S: SessionExt> RuntimeSessionExt for S {}