1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

#![allow(unreachable_code, internal_features, improper_ctypes)]
#![feature(used_with_arg, core_intrinsics, linkage, tuple_trait, doc_cfg)]
#![no_std]

//! Small crate for custom panicking behavior, primarily designed for embedded or `no_std` projects.
//!
//! By default, it's behavior for panics is to halt in both release and debug mode.
//! This crate, `panic_custom`, allows developers to customize this behavior by providing a custom
//! panic handler function.
//!
//! # Usage
//!
//! The crate provides two main ways to define custom panicking behavior:
//!
//! - Using the `define_panic!` macro with a closure argument.
//! - Using the `#[define_panic]` procedural macro.
//!
//! ## Using `define_panic!` Macro
//!
//! The `define_panic!` macro allows you to define custom panicking behavior by passing a closure as an argument.
//!
//! ```rust
//! use panic_custom::define_panic;
//!
//! const MY_CUSTOM_CONSTANT: usize = 0;
//!
//! define_panic!(|info| {
//!     let a = &MY_CUSTOM_CONSTANT;
//!     let b = MY_CUSTOM_CONSTANT;
//!
//!     42 // The return type is not important
//! });
//! ```
//!
//! ## Using `#[define_panic]` Procedural Macro
//!
//! The `#[define_panic]` procedural macro allows you to define a custom panic handler function.
//! To use this macro, enable the `proc_macros` feature and include `features = "proc_macros"` in your `Cargo.toml`.
//!
//! ```toml
//! [dependencies]
//! panic_custom = { version = "0.1", features = ["proc_macros"] }
//! ```
//!
//! ```rust
//! use panic_custom::define_panic;
//! use core::panic::PanicInfo;
//!
//! #[define_panic]
//! fn my_panic(info: &PanicInfo) -> ! {
//!     loop {}
//! }
//! ```
//!
//! # Features
//!
//! - `proc_macros`: Enables procedural macros for custom panic handling.
//!
//! - `abort_on_debug`: Sets the default behavior to abort on panic in debug mode. By default, the crate halts on panic in debug mode.
//!
//! - `abort_on_release`: Sets the default behavior to abort on panic in release mode. By default, the crate halts on panic in release mode.
//!
//! # Note
//!
//! Ensure that custom panic handlers are implemented safely to avoid undefined behavior. Incorrect panic handling logic may lead to unexpected program behavior.
//!
//! # See Also
//!
//! - [`core::panic::PanicInfo`](https://doc.rust-lang.org/core/panic/struct.PanicInfo.html): Struct representing information about a panic.
//!
//! # Reference
//!
//! - [The Rust Book - Panic Handling](https://doc.rust-lang.org/book/ch09-03-to-panic-or-not-to-panic.html)
//!
//! This crate provides flexibility in defining custom panic handling behavior, empowering developers to tailor their applications' panic behavior to their specific 
//! requirements, especially in embedded or `no_std` projects.

#[cfg(feature = "proc_macros")]
#[doc(cfg(feature = "proc_macros"))]
pub use panic_custom_proc_macros::define_panic;

#[cfg(not(feature = "proc_macros"))]
#[doc(hidden)]
pub mod no_macro {
    use core::sync::atomic::{self, Ordering};

    /// This macro defines the behavior of the panic handler when procedural macros are not enabled.
    ///
    /// To define a custom panic handler, provide the custom panic closure or function as an argument to the macro.
    /// The function can return anything and can be defined as any closure.
    ///
    /// ```rust
    /// use panic_custom::define_panic;
    ///
    /// const MY_CUSTOM_CONSTANT: usize = 0;
    ///
    /// define_panic!(|info| {
    ///     let a = &MY_CUSTOM_CONSTANT;
    ///     let b = MY_CUSTOM_CONSTANT;
    ///
    ///     loop {}
    /// });
    /// ```
    ///
    /// It is not neccesary for a handler closure to loop, the macro will make it return ! for you.
    /// This is a regular macro not a procedural one. For using the procedural macro with the same
    /// name you should add a feature 'proc_macros' to your crate.
    ///
     /// ```rust
    /// use panic_custom::define_panic;
    ///
    /// define_panic!(|info| {
    ///     let a = 42;
    ///
    ///     42 // Still works + will be optimized by a compiler.
    /// });
    /// ```
    #[macro_export]
    #[doc(cfg(not(feature = "proc_macros")))]
    macro_rules! define_panic {
        ($panic_fn:expr) => {
            #[inline(never)]
            #[panic_handler]
            fn panic(info: &core::panic::PanicInfo) -> ! {
                // Custom defined function
                unsafe { $panic_fn(info); }

                $crate::no_macro::__default_panic()
            }
        };
        () => {
            #[inline(never)]
            #[panic_handler]
            fn panic(_: &core::panic::PanicInfo) -> ! {
                $crate::no_macro::__default_panic()
            }
        }
    }

    #[doc(hidden)]
    #[inline(always)]
    pub fn __default_panic() -> ! {
        #[cfg(not(debug_assertions))]
        {
            #[cfg(not(feature = "abort_on_release"))] // Aborts.
            {
                loop {
                    atomic::compiler_fence(Ordering::SeqCst); // Halting on debug.
                }
            }

            #[cfg(feature = "abort_on_release")] // Halts.
            {
                core::intrinsics::abort();
            }
        } 

        #[cfg(debug_assertions)]
        {
            #[cfg(not(feature = "abort_on_debug"))] // Aborts.
            {
                loop {
                    atomic::compiler_fence(Ordering::SeqCst); // Halting on debug.
                }
            }

            #[cfg(feature = "abort_on_debug")] // Halts.
            {
                core::intrinsics::abort();
            }
        }
    }
}

#[test]
fn some_panic() {
    use crate::define_panic;

    define_panic!(|_| {
        loop {}
    });

    panic!();
}