From 23f64e119c08238803c85dec012921745d8098f0 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Fri, 12 Jan 2024 15:39:54 +0100 Subject: [PATCH] panic-calm --- Cargo.lock | 4 ++++ Cargo.toml | 2 ++ members/panic-calm/Cargo.toml | 8 ++++++++ members/panic-calm/src/main.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 members/panic-calm/Cargo.toml create mode 100644 members/panic-calm/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 916a993..cbcd2cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -874,6 +874,10 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +[[package]] +name = "panic-calm" +version = "0.1.0" + [[package]] name = "peg" version = "0.6.3" diff --git a/Cargo.toml b/Cargo.toml index 8417147..2caac63 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,7 @@ members = [ ".", "members/criterion-demo", "members/cucumber-demo", + "members/panic-calm", "members/revsqrt", "members/serde-json-demo", "members/slog-demo", @@ -14,6 +15,7 @@ default-members = [ "members/serde-json-demo", "members/slog-demo", "members/cucumber-demo", + "members/panic-calm", ] [package] diff --git a/members/panic-calm/Cargo.toml b/members/panic-calm/Cargo.toml new file mode 100644 index 0000000..c964210 --- /dev/null +++ b/members/panic-calm/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "panic-calm" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/members/panic-calm/src/main.rs b/members/panic-calm/src/main.rs new file mode 100644 index 0000000..708dbb0 --- /dev/null +++ b/members/panic-calm/src/main.rs @@ -0,0 +1,27 @@ +use std::{io::Write, panic}; +fn inner() { + let mut counter = 0u8; + loop { + // will eventually panic when overflowing? + counter += 1; + print!("{counter}\t"); + if counter % 8 == 0 { println!() } + if counter == 255 { + // so panic will look fancier :) + println!() + } + } +} + +fn main() { + // will not catch all panics, only ones that unwind + let panic = panic::catch_unwind(|| { + inner(); + }); + if panic.is_err() { + dbg!(&panic); + dbg!(&panic.as_ref().unwrap_err().type_id()); + println!("recovered from a panic"); + } + else {println!("no panic on the titanic")} +}