diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-05-09 17:37:37 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-05-09 17:37:37 +0800 |
| commit | 2eba732e17cf71dcf8a54b428a1c5e344582c2c2 (patch) | |
| tree | e04266109421a0255625490dc997dc0d55bbf64a /mingling_core/src/program/setup | |
| parent | 10bc4ca7a4b3f14cfb57bf72a6da8aaa1490acf3 (diff) | |
Add exit code control and hook lifecycle features
Diffstat (limited to 'mingling_core/src/program/setup')
| -rw-r--r-- | mingling_core/src/program/setup/exit_code_control.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/mingling_core/src/program/setup/exit_code_control.rs b/mingling_core/src/program/setup/exit_code_control.rs new file mode 100644 index 0000000..0844a24 --- /dev/null +++ b/mingling_core/src/program/setup/exit_code_control.rs @@ -0,0 +1,36 @@ +use std::sync::atomic::{AtomicI32, Ordering}; + +use crate::{ProgramCollect, hook::ProgramAnonymousHook, setup::ProgramSetup}; + +static EXIT_CODE: AtomicI32 = AtomicI32::new(0); + +/// Provides the ability to control the program's exit code, which is returned when the program ends. +/// +/// - Use `mingling::update_exit_code` to update the exit code. +/// - Use `mingling::current_exit_code` to query the current exit code. +pub struct ExitCodeSetup; + +impl<C> ProgramSetup<C> for ExitCodeSetup +where + C: ProgramCollect<Enum = C>, +{ + fn setup(&mut self, program: &mut crate::Program<C>) { + program + .with_hook_anonymous(ProgramAnonymousHook::empty().on_finish(|| current_exit_code())); + } +} + +/// Updates the program's exit code. +/// +/// This function sets the value that will be returned when the program exits. +/// The new code will take effect immediately and be used when the program finishes. +pub fn update_exit_code(code: i32) { + EXIT_CODE.store(code, Ordering::SeqCst); +} + +/// Returns the current exit code. +/// +/// This function queries the value that will be returned when the program exits. +pub fn current_exit_code() -> i32 { + EXIT_CODE.load(Ordering::SeqCst) +} |
