From 8d251003a25aa697de052f01515e5cc23ce999e4 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sun, 3 Nov 2024 12:42:43 +0000 Subject: [PATCH] reduce Error-related codegen; add PoisonError Signed-off-by: Jason Volk --- src/core/config/mod.rs | 1 + src/core/error/err.rs | 1 + src/core/error/mod.rs | 10 +++++++++- src/core/error/panic.rs | 4 ++++ src/core/error/response.rs | 1 + 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs index a6216da2..43cca4b8 100644 --- a/src/core/config/mod.rs +++ b/src/core/config/mod.rs @@ -2001,6 +2001,7 @@ fn default_rocksdb_stats_level() -> u8 { 1 } // I know, it's a great name #[must_use] +#[inline] pub fn default_default_room_version() -> RoomVersionId { RoomVersionId::V10 } fn default_ip_range_denylist() -> Vec { diff --git a/src/core/error/err.rs b/src/core/error/err.rs index 82bb40b0..baeb992d 100644 --- a/src/core/error/err.rs +++ b/src/core/error/err.rs @@ -137,6 +137,7 @@ macro_rules! err_log { let visit = &mut |vs: ValueSet<'_>| { struct Visitor<'a>(&'a mut String); impl Visit for Visitor<'_> { + #[inline] fn record_debug(&mut self, field: &Field, val: &dyn fmt::Debug) { if field.name() == "message" { write!(self.0, "{:?}", val).expect("stream error"); diff --git a/src/core/error/mod.rs b/src/core/error/mod.rs index 42250a0c..302d0f87 100644 --- a/src/core/error/mod.rs +++ b/src/core/error/mod.rs @@ -4,7 +4,7 @@ mod panic; mod response; mod serde; -use std::{any::Any, borrow::Cow, convert::Infallible, fmt}; +use std::{any::Any, borrow::Cow, convert::Infallible, fmt, sync::PoisonError}; pub use self::log::*; use crate::error; @@ -59,6 +59,8 @@ pub enum Error { JsTryFromInt(#[from] ruma::JsTryFromIntError), // js_int re-export #[error(transparent)] Path(#[from] axum::extract::rejection::PathRejection), + #[error("Mutex poisoned: {0}")] + Poison(Cow<'static, str>), #[error("Regex error: {0}")] Regex(#[from] regex::Error), #[error("Request error: {0}")] @@ -184,6 +186,12 @@ impl fmt::Debug for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.message()) } } +impl From> for Error { + #[cold] + #[inline(never)] + fn from(e: PoisonError) -> Self { Self::Poison(e.to_string().into()) } +} + #[allow(clippy::fallible_impl_from)] impl From for Error { #[cold] diff --git a/src/core/error/panic.rs b/src/core/error/panic.rs index c070f786..bec25132 100644 --- a/src/core/error/panic.rs +++ b/src/core/error/panic.rs @@ -10,11 +10,14 @@ impl UnwindSafe for Error {} impl RefUnwindSafe for Error {} impl Error { + #[inline] pub fn panic(self) -> ! { panic_any(self.into_panic()) } #[must_use] + #[inline] pub fn from_panic(e: Box) -> Self { Self::Panic(debug::panic_str(&e), e) } + #[inline] pub fn into_panic(self) -> Box { match self { Self::Panic(_, e) | Self::PanicAny(e) => e, @@ -24,6 +27,7 @@ impl Error { } /// Get the panic message string. + #[inline] pub fn panic_str(self) -> Option<&'static str> { self.is_panic() .then_some(debug::panic_str(&self.into_panic())) diff --git a/src/core/error/response.rs b/src/core/error/response.rs index 7568a1c0..21fbdcf2 100644 --- a/src/core/error/response.rs +++ b/src/core/error/response.rs @@ -26,6 +26,7 @@ impl axum::response::IntoResponse for Error { } impl From for UiaaResponse { + #[inline] fn from(error: Error) -> Self { if let Error::Uiaa(uiaainfo) = error { return Self::AuthResponse(uiaainfo);