From f941f1c592dab8b6559cb9d35fc7388a720c147b Mon Sep 17 00:00:00 2001 From: Nick Santana Date: Wed, 11 Jan 2023 14:49:37 -0800 Subject: [PATCH] Add `panicking::panicking()` function The `panicking::panicking()` function is a crate function that indicates if the current thread is panicking. --- panic/src/panicking.rs | 60 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/panic/src/panicking.rs b/panic/src/panicking.rs index e62723c..0d44188 100644 --- a/panic/src/panicking.rs +++ b/panic/src/panicking.rs @@ -1,12 +1,17 @@ // Copyright (c) 2023 The MobileCoin Foundation -#[allow(dead_code)] +//! Common backend panic implementation +//! +//! This is a subset of the functionality available in Rust's std +//! [panicking.rs](https://github.com/rust-lang/rust/blob/master/library/std/src/panicking.rs) +//! module. -/// Common backend panic implementation -/// -/// This is a subset of the functionality available in Rust's std -/// [panicking.rs](https://github.com/rust-lang/rust/blob/master/library/std/src/panicking.rs) -/// module. +#![allow(dead_code)] + +/// Determines whether the current thread is unwinding because of panic. +pub(crate) fn panicking() -> bool { + !panic_count::count_is_zero() +} pub(crate) mod panic_count { //! Number of panics that are currently being handled on the current thread @@ -115,3 +120,46 @@ pub(crate) mod panic_count { } } } + +#[cfg(test)] +mod test { + use super::*; + + /// Sets panic count is 0 + /// Similar to the tests in mod `panic_count` tests need to call this prior + /// to testing to ensure correct behavior + fn clear_panic_count() { + while panic_count::get_count() != 0 { + panic_count::decrease(); + } + } + + #[test] + fn is_panicking_false_when_panic_count_zero() { + clear_panic_count(); + assert!(!panicking()); + } + + #[test] + fn is_panicking_true_when_panic_count_gt_zero() { + clear_panic_count(); + + // First panic + panic_count::increase(); + assert!(panicking()); + + // Second panic + panic_count::increase(); + assert!(panicking()); + + // finished second panic + panic_count::decrease(); + assert!(panicking()); + + // finished first panic + // NB that this one is *not* panicking since we've decreased back down + // to zero + panic_count::decrease(); + assert!(!panicking()); + } +}