From a4c7f2f04b4a5de9274f23625b9e6399cd601f8b Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 17 Feb 2026 23:35:43 +0000 Subject: [PATCH] Move unhandled macro from database utils to core utils. Signed-off-by: Jason Volk --- src/core/utils/mod.rs | 1 + src/core/utils/unhandled.rs | 33 +++++++++++++++++++++++++++++++++ src/database/de.rs | 5 ++--- src/database/ser.rs | 4 +--- src/database/util.rs | 23 ----------------------- 5 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 src/core/utils/unhandled.rs diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs index bc6064b5..f15ed6de 100644 --- a/src/core/utils/mod.rs +++ b/src/core/utils/mod.rs @@ -20,6 +20,7 @@ pub mod sys; mod tests; pub mod time; pub mod two_phase_counter; +pub mod unhandled; pub use ::ctor::{ctor, dtor}; pub use ::tuwunel_macros::implement; diff --git a/src/core/utils/unhandled.rs b/src/core/utils/unhandled.rs new file mode 100644 index 00000000..83174bc4 --- /dev/null +++ b/src/core/utils/unhandled.rs @@ -0,0 +1,33 @@ +//! Indicate a branch which will never be taken. This induces optimal codegen in +//! release-mode by emitting unsafe unreachable_unchecked(). In debug-mode it +//! emits unimplemented() to panic on misplacement. + +#[cfg(disable)] // activate when more stable and callsites are vetted. +// #[cfg(not(debug_assertions))] +#[macro_export] +macro_rules! unhandled { + ($msg:literal) => { + // SAFETY: Eliminates branches never encountered in the codebase. This can + // promote optimization and reduce codegen. The developer must verify for every + // invoking callsite that the unhandled type is in no way involved and could not + // possibly be encountered. + unsafe { + std::hint::unreachable_unchecked(); + } + }; +} + +//#[cfg(debug_assertions)] +#[macro_export] +macro_rules! unhandled { + ($msg:literal) => { + $crate::maybe_unhandled!($msg); + }; +} + +#[macro_export] +macro_rules! maybe_unhandled { + ($msg:literal) => { + unimplemented!($msg) + }; +} diff --git a/src/database/de.rs b/src/database/de.rs index f33ec08e..cbe357d4 100644 --- a/src/database/de.rs +++ b/src/database/de.rs @@ -3,11 +3,10 @@ use serde::{ de::{DeserializeSeed, Visitor}, }; use tuwunel_core::{ - Error, Result, arrayvec::ArrayVec, checked, debug::DebugInspect, err, utils::string, + Error, Result, arrayvec::ArrayVec, checked, debug::DebugInspect, err, unhandled, + utils::string, }; -use crate::util::unhandled; - /// Deserialize into T from buffer. #[cfg_attr( unabridged, diff --git a/src/database/ser.rs b/src/database/ser.rs index b3d801d8..74150754 100644 --- a/src/database/ser.rs +++ b/src/database/ser.rs @@ -1,9 +1,7 @@ use std::{io::Write, mem::replace}; use serde::{Deserialize, Serialize, ser}; -use tuwunel_core::{Error, Result, debug::type_name, err, result::DebugInspect}; - -use crate::util::unhandled; +use tuwunel_core::{Error, Result, debug::type_name, err, result::DebugInspect, unhandled}; #[inline] pub fn serialize_to_vec(val: T) -> Result> { diff --git a/src/database/util.rs b/src/database/util.rs index cc9dba7a..cfbea618 100644 --- a/src/database/util.rs +++ b/src/database/util.rs @@ -1,29 +1,6 @@ use rocksdb::{Direction, ErrorKind, IteratorMode}; use tuwunel_core::Result; -//#[cfg(debug_assertions)] -macro_rules! unhandled { - ($msg:literal) => { - unimplemented!($msg) - }; -} - -// activate when stable; we're not ready for this yet -#[cfg(disable)] // #[cfg(not(debug_assertions))] -macro_rules! unhandled { - ($msg:literal) => { - // SAFETY: Eliminates branches for serializing and deserializing types never - // encountered in the codebase. This can promote optimization and reduce - // codegen. The developer must verify for every invoking callsite that the - // unhandled type is in no way involved and could not possibly be encountered. - unsafe { - std::hint::unreachable_unchecked(); - } - }; -} - -pub(crate) use unhandled; - #[inline] pub(crate) fn _into_direction(mode: &IteratorMode<'_>) -> Direction { use Direction::{Forward, Reverse};