From ab7c5785fb290541ad4361c0d46241817c3ff5f9 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Mon, 18 May 2026 16:02:57 +0800 Subject: Refactor program module into submodules --- mingling_core/src/program/single_instance.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 mingling_core/src/program/single_instance.rs (limited to 'mingling_core/src/program/single_instance.rs') 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>> = + OnceLock::new(); + +/// Returns a reference to the current program instance, panics if not set. +pub fn this() -> &'static Program +where + C: ProgramCollect + 'static, +{ + try_get_this_program().expect("Program not initialized") +} + +/// Returns a reference to the current program instance, if set. +fn try_get_this_program() -> Option<&'static Program> +where + C: ProgramCollect + 'static, +{ + THIS_PROGRAM.get()?.as_ref()?.downcast_ref::>() +} -- cgit