aboutsummaryrefslogtreecommitdiff
path: root/examples/example-panic-unwind
diff options
context:
space:
mode:
Diffstat (limited to 'examples/example-panic-unwind')
-rw-r--r--examples/example-panic-unwind/Cargo.lock83
-rw-r--r--examples/example-panic-unwind/Cargo.toml16
-rw-r--r--examples/example-panic-unwind/src/main.rs56
3 files changed, 155 insertions, 0 deletions
diff --git a/examples/example-panic-unwind/Cargo.lock b/examples/example-panic-unwind/Cargo.lock
new file mode 100644
index 0000000..d6af75d
--- /dev/null
+++ b/examples/example-panic-unwind/Cargo.lock
@@ -0,0 +1,83 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "example-panic-unwind"
+version = "0.1.0"
+dependencies = [
+ "mingling",
+]
+
+[[package]]
+name = "just_fmt"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5454cda0d57db59778608d7a47bff5b16c6705598265869fb052b657f66cf05e"
+
+[[package]]
+name = "mingling"
+version = "0.1.9"
+dependencies = [
+ "mingling_core",
+ "mingling_macros",
+ "size",
+]
+
+[[package]]
+name = "mingling_core"
+version = "0.1.9"
+dependencies = [
+ "just_fmt",
+]
+
+[[package]]
+name = "mingling_macros"
+version = "0.1.9"
+dependencies = [
+ "just_fmt",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.106"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
+dependencies = [
+ "unicode-ident",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "size"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1b6709c7b6754dca1311b3c73e79fcce40dd414c782c66d88e8823030093b02b"
+
+[[package]]
+name = "syn"
+version = "2.0.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "unicode-ident",
+]
+
+[[package]]
+name = "unicode-ident"
+version = "1.0.24"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
diff --git a/examples/example-panic-unwind/Cargo.toml b/examples/example-panic-unwind/Cargo.toml
new file mode 100644
index 0000000..5d1c242
--- /dev/null
+++ b/examples/example-panic-unwind/Cargo.toml
@@ -0,0 +1,16 @@
+[package]
+name = "example-panic-unwind"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies.mingling]
+path = "../../mingling"
+features = ["parser"]
+
+# Enable panic unwinding in release builds
+[profile.release]
+panic = "unwind"
+
+# Enable panic unwinding in dev builds
+[profile.dev]
+panic = "unwind"
diff --git a/examples/example-panic-unwind/src/main.rs b/examples/example-panic-unwind/src/main.rs
new file mode 100644
index 0000000..18cf4d6
--- /dev/null
+++ b/examples/example-panic-unwind/src/main.rs
@@ -0,0 +1,56 @@
+//! Example Panic Unwind
+//!
+//! > This example introduces how to catch Panic in the Mingling program loop
+//!
+//! Run:
+//! ```bash
+//! cargo run --manifest-path examples/example-panic-unwind/Cargo.toml --quiet -- panic
+//! cargo run --manifest-path examples/example-panic-unwind/Cargo.toml --quiet -- panic OhMyGod
+//! ```
+//!
+//! Output:
+//! ```plaintext
+//! Program not panic
+//! Program panic: OhMyGod
+//! OhMyGod
+//! ```
+
+use mingling::{hook::ProgramHook, prelude::*};
+
+dispatcher!("panic", CMDPanic => EntryPanic);
+pack!(NotPanic = ());
+
+fn main() {
+ let mut program = ThisProgram::new();
+ program.with_dispatcher(CMDPanic);
+
+ // --------- IMPORTANT ---------
+ // Enable silence_panic to suppress automatic Panic output
+ program.stdout_setting.silence_panic = true;
+
+ // Define a hook to output &ProgramPanic when a Panic occurs
+ program
+ .with_hook(ProgramHook::empty().on_exec_panic(|info| println!("Program panic: {}", info)));
+ // --------- IMPORTANT ---------
+
+ program.exec();
+}
+
+#[chain]
+fn handle_panic(prev: EntryPanic) -> Next {
+ let panic_info = prev.pick::<Option<String>>(()).unpack();
+ match panic_info {
+ Some(s) => {
+ // Panic happens here, will be caught
+ panic!("{}", s)
+ }
+ None => NotPanic::default(),
+ }
+}
+
+#[renderer]
+fn render(_: NotPanic) {
+ r_println!("Program not panic");
+}
+
+gen_program!();