Skip to main content

tuwunel_core/utils/
unhandled.rs

1//! Macros for branches expected never to execute.
2//!
3//! Active builds expand to `unimplemented!()`. A dormant
4//! `unreachable_unchecked()` definition is excluded by `#[cfg(disable)]`;
5//! activating it would also require excluding the ordinary definition.
6
7#[cfg(disable)] // activate when more stable and callsites are vetted.
8// #[cfg(not(debug_assertions))]
9/// Defines a dormant unchecked marker for branches assumed impossible.
10///
11/// This definition is excluded by `cfg(disable)`, and activating it requires
12/// also excluding the safe definition below. Reaching its expansion invokes
13/// [`std::hint::unreachable_unchecked`] and causes undefined behavior.
14#[macro_export]
15macro_rules! unhandled {
16	($msg:literal) => {
17		// SAFETY: Eliminates branches never encountered in the codebase. This can
18		// promote optimization and reduce codegen. The developer must verify for every
19		// invoking callsite that the unhandled type is in no way involved and could not
20		// possibly be encountered.
21		unsafe {
22			std::hint::unreachable_unchecked();
23		}
24	};
25}
26
27//#[cfg(debug_assertions)]
28/// Marks an unsupported branch and panics with the supplied message.
29///
30/// The expansion delegates to [`crate::maybe_unhandled!`] and always retains a
31/// runtime failure path.
32#[macro_export]
33macro_rules! unhandled {
34	($msg:literal) => {
35		$crate::maybe_unhandled!($msg)
36	};
37}
38
39/// Panics with the supplied message for a branch that is not implemented.
40///
41/// This macro always retains a runtime failure path and can therefore mark code
42/// that may remain reachable.
43#[macro_export]
44macro_rules! maybe_unhandled {
45	($msg:literal) => {
46		unimplemented!($msg)
47	};
48}