Reduce code size of error handling

This commit is contained in:
Kornel 2024-08-18 13:44:59 +01:00 committed by Teodor Tanasoaia
parent a87c8d77ba
commit 222f1ea733
5 changed files with 32 additions and 8 deletions

View File

@ -326,6 +326,8 @@ trait PrettyResult {
fn unwrap_pretty(self) -> Self::Target; fn unwrap_pretty(self) -> Self::Target;
} }
#[cold]
#[inline(never)]
fn print_err(error: &dyn Error) { fn print_err(error: &dyn Error) {
eprint!("{error}"); eprint!("{error}");

View File

@ -278,6 +278,8 @@ pub enum Error<'a> {
} }
impl<'a> Error<'a> { impl<'a> Error<'a> {
#[cold]
#[inline(never)]
pub(crate) fn as_parse_error(&self, source: &'a str) -> ParseError { pub(crate) fn as_parse_error(&self, source: &'a str) -> ParseError {
match *self { match *self {
Error::Unexpected(unexpected_span, expected) => { Error::Unexpected(unexpected_span, expected) => {

View File

@ -3,6 +3,11 @@ use std::{error::Error, sync::Arc};
use thiserror::Error; use thiserror::Error;
#[cfg(send_sync)]
pub type ContextErrorSource = Box<dyn Error + Send + Sync + 'static>;
#[cfg(not(send_sync))]
pub type ContextErrorSource = Box<dyn Error + 'static>;
#[derive(Debug, Error)] #[derive(Debug, Error)]
#[error( #[error(
"In {fn_ident}{}{}{}", "In {fn_ident}{}{}{}",
@ -13,10 +18,7 @@ use thiserror::Error;
pub struct ContextError { pub struct ContextError {
pub fn_ident: &'static str, pub fn_ident: &'static str,
#[source] #[source]
#[cfg(send_sync)] pub source: ContextErrorSource,
pub source: Box<dyn Error + Send + Sync + 'static>,
#[cfg(not(send_sync))]
pub source: Box<dyn Error + 'static>,
pub label: String, pub label: String,
} }

View File

@ -1296,6 +1296,7 @@ fn get_lost_err() -> crate::DeviceError {
crate::DeviceError::Lost crate::DeviceError::Lost
} }
#[cold]
fn hal_usage_error<T: fmt::Display>(txt: T) -> ! { fn hal_usage_error<T: fmt::Display>(txt: T) -> ! {
panic!("wgpu-hal invariant was violated (usage error): {txt}") panic!("wgpu-hal invariant was violated (usage error): {txt}")
} }

View File

@ -24,6 +24,7 @@ use std::{
slice, slice,
sync::Arc, sync::Arc,
}; };
use wgc::error::ContextErrorSource;
use wgc::{ use wgc::{
command::bundle_ffi::*, device::DeviceLostClosure, id::CommandEncoderId, id::TextureViewId, command::bundle_ffi::*, device::DeviceLostClosure, id::CommandEncoderId, id::TextureViewId,
pipeline::CreateShaderModuleError, pipeline::CreateShaderModuleError,
@ -267,16 +268,18 @@ impl ContextWgpuCore {
self.0.generate_report() self.0.generate_report()
} }
fn handle_error( #[cold]
#[inline(never)]
fn handle_error_inner(
&self, &self,
sink_mutex: &Mutex<ErrorSinkRaw>, sink_mutex: &Mutex<ErrorSinkRaw>,
source: impl Error + WasmNotSendSync + 'static, source: ContextErrorSource,
label: Label<'_>, label: Label<'_>,
fn_ident: &'static str, fn_ident: &'static str,
) { ) {
let error = wgc::error::ContextError { let error = wgc::error::ContextError {
fn_ident, fn_ident,
source: Box::new(source), source,
label: label.unwrap_or_default().to_string(), label: label.unwrap_or_default().to_string(),
}; };
let mut sink = sink_mutex.lock(); let mut sink = sink_mutex.lock();
@ -299,16 +302,29 @@ impl ContextWgpuCore {
}); });
} }
#[inline]
fn handle_error(
&self,
sink_mutex: &Mutex<ErrorSinkRaw>,
source: impl Error + WasmNotSendSync + 'static,
label: Label<'_>,
fn_ident: &'static str,
) {
self.handle_error_inner(sink_mutex, Box::new(source), label, fn_ident)
}
#[inline]
fn handle_error_nolabel( fn handle_error_nolabel(
&self, &self,
sink_mutex: &Mutex<ErrorSinkRaw>, sink_mutex: &Mutex<ErrorSinkRaw>,
source: impl Error + WasmNotSendSync + 'static, source: impl Error + WasmNotSendSync + 'static,
fn_ident: &'static str, fn_ident: &'static str,
) { ) {
self.handle_error(sink_mutex, source, None, fn_ident) self.handle_error_inner(sink_mutex, Box::new(source), None, fn_ident)
} }
#[track_caller] #[track_caller]
#[cold]
fn handle_error_fatal( fn handle_error_fatal(
&self, &self,
cause: impl Error + WasmNotSendSync + 'static, cause: impl Error + WasmNotSendSync + 'static,
@ -317,6 +333,7 @@ impl ContextWgpuCore {
panic!("Error in {operation}: {f}", f = self.format_error(&cause)); panic!("Error in {operation}: {f}", f = self.format_error(&cause));
} }
#[inline(never)]
fn format_error(&self, err: &(impl Error + 'static)) -> String { fn format_error(&self, err: &(impl Error + 'static)) -> String {
let mut output = String::new(); let mut output = String::new();
let mut level = 1; let mut level = 1;