aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/src/program/single_instance.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-05-18 16:02:57 +0800
committer魏曹先生 <1992414357@qq.com>2026-05-18 16:02:57 +0800
commitab7c5785fb290541ad4361c0d46241817c3ff5f9 (patch)
tree042012fe0a6cac2e5da90ccff0df38e64ac50485 /mingling_core/src/program/single_instance.rs
parentf1408931eb19f90dd96092ea26bea8d14f5ec804 (diff)
Refactor program module into submodules
Diffstat (limited to 'mingling_core/src/program/single_instance.rs')
-rw-r--r--mingling_core/src/program/single_instance.rs23
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>>()
+}