diff options
Diffstat (limited to 'mingling_core/src/program/single_instance.rs')
| -rw-r--r-- | mingling_core/src/program/single_instance.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/mingling_core/src/program/single_instance.rs b/mingling_core/src/program/single_instance.rs new file mode 100644 index 0000000..45d4d33 --- /dev/null +++ b/mingling_core/src/program/single_instance.rs @@ -0,0 +1,23 @@ +use std::sync::OnceLock; + +use crate::{Program, ProgramCollect}; + +/// Global static reference to the current program instance +pub(crate) static THIS_PROGRAM: OnceLock<Option<Box<dyn std::any::Any + Send + Sync>>> = + OnceLock::new(); + +/// Returns a reference to the current program instance, panics if not set. +pub fn this<C>() -> &'static Program<C> +where + C: ProgramCollect<Enum = C> + 'static, +{ + try_get_this_program().expect("Program not initialized") +} + +/// Returns a reference to the current program instance, if set. +fn try_get_this_program<C>() -> Option<&'static Program<C>> +where + C: ProgramCollect<Enum = C> + 'static, +{ + THIS_PROGRAM.get()?.as_ref()?.downcast_ref::<Program<C>>() +} |
