1use std::fmt::Debug;
5
6use vortex_error::VortexExpect;
7use vortex_session::SessionExt;
8
9use crate::runtime::Handle;
10
11pub struct RuntimeSession {
13 handle: Option<Handle>,
14}
15
16impl Default for RuntimeSession {
17 fn default() -> Self {
18 Self {
19 handle: Handle::find(),
20 }
21 }
22}
23
24impl Debug for RuntimeSession {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 f.debug_struct("RuntimeSession").finish_non_exhaustive()
27 }
28}
29
30pub trait RuntimeSessionExt: SessionExt {
32 fn handle(&self) -> Handle {
34 self.get::<RuntimeSession>().handle
35 .as_ref()
36 .vortex_expect("Runtime handle not configured in Vortex session. Please setup a `CurrentThreadRuntime`, or configure the session for `with_tokio`.")
37 .clone()
38 }
39
40 #[cfg(feature = "tokio")]
44 fn with_tokio(self) -> Self {
45 self.get_mut::<RuntimeSession>().handle =
46 Some(crate::runtime::tokio::TokioRuntime::current());
47 self
48 }
49
50 fn with_handle(self, handle: Handle) -> Self {
52 self.get_mut::<RuntimeSession>().handle = Some(handle);
53 self
54 }
55}
56impl<S: SessionExt> RuntimeSessionExt for S {}