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;
}
#[cold]
#[inline(never)]
fn print_err(error: &dyn Error) {
eprint!("{error}");

View File

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

View File

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

View File

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

View File

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