From dd22d7a1649964e92c5041417df1d21bf50d2858 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 30 Dec 2024 18:48:19 -0500 Subject: [PATCH] Implement standard errors --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + slang-sys/build.rs | 1 + slang-sys/src/lib.rs | 12 +++++++++++- src/lib.rs | 36 ++++++++++++++++++++++-------------- 5 files changed, 56 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 108f7a8..16c7ad2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -220,6 +220,7 @@ name = "slang" version = "0.1.0" dependencies = [ "slang-sys", + "thiserror", ] [[package]] @@ -241,6 +242,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "2.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f072643fd0190df67a8bab670c20ef5d8737177d6ac6b2e9a236cb096206b2cc" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b50fa271071aae2e6ee85f842e2e28ba8cd2c5fb67f11fcb1fd70b276f9e7d4" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicode-ident" version = "1.0.14" diff --git a/Cargo.toml b/Cargo.toml index 66858b5..9c2e1af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ publish = false [dependencies] slang-sys = { path = "slang-sys" } +thiserror = "2.0" [workspace] members = [ diff --git a/slang-sys/build.rs b/slang-sys/build.rs index 4391ba8..e621d94 100644 --- a/slang-sys/build.rs +++ b/slang-sys/build.rs @@ -54,6 +54,7 @@ fn main() { }) .constified_enum("SlangProfileID") .constified_enum("SlangCapabilityID") + .new_type_alias("SlangResult") .vtable_generation(true) .layout_tests(false) .derive_copy(true) diff --git a/slang-sys/src/lib.rs b/slang-sys/src/lib.rs index 9d1e596..38ce9e6 100644 --- a/slang-sys/src/lib.rs +++ b/slang-sys/src/lib.rs @@ -3,7 +3,7 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs")); use std::ffi::{c_char, c_int, c_void}; - +use std::fmt::{Display, Formatter}; // Based on Slang version 2024.14.5 #[repr(C)] @@ -135,3 +135,13 @@ pub struct IModuleVtable { pub getDependencyFilePath: unsafe extern "C" fn(*mut c_void, index: SlangInt32) -> *const c_char, pub getModuleReflection: unsafe extern "C" fn(*mut c_void) -> *mut slang_DeclReflection, } + +impl Display for SlangResult { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + if self.0 > 0 { + write!(f, "success") + } else { + write!(f, "failure") + } + } +} diff --git a/src/lib.rs b/src/lib.rs index da3a3c7..e1ac5e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ pub mod reflection; use std::ffi::{CStr, CString}; +use std::fmt::{Debug, Display, Formatter}; use std::ptr::{null, null_mut}; use slang_sys as sys; @@ -29,33 +30,28 @@ const fn uuid(data1: u32, data2: u16, data3: u16, data4: [u8; 8]) -> UUID { } } +#[derive(thiserror::Error, Debug)] pub enum Error { - Code(sys::SlangResult), - Blob(Blob), -} + #[error("underlying library error: `{0}`")] + Slang(sys::SlangResult), -impl std::fmt::Debug for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Error::Code(code) => write!(f, "{}", code), - Error::Blob(blob) => write!(f, "{}", blob.as_str().unwrap()), - } - } + #[error("blob")] + Blob(Blob), } pub type Result = std::result::Result; pub(crate) fn succeeded(result: sys::SlangResult) -> bool { - result >= 0 + result.0 >= 0 } fn result_from_blob(code: sys::SlangResult, blob: *mut sys::slang_IBlob) -> Result<()> { - if code < 0 { + if succeeded(code) { + Ok(()) + } else { Err(Error::Blob(Blob(IUnknown( std::ptr::NonNull::new(blob as *mut _).unwrap(), )))) - } else { - Ok(()) } } @@ -125,6 +121,18 @@ impl Drop for IUnknown { #[derive(Clone)] pub struct Blob(IUnknown); +impl Debug for Blob { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "ptr={:p}", self.0.0) + } +} + +impl Display for Blob { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + Debug::fmt(&self, f) + } +} + unsafe impl Interface for Blob { type Vtable = sys::IBlobVtable; const IID: UUID = uuid(