Implement standard errors

This commit is contained in:
Bradlee Speice 2024-12-30 18:48:19 -05:00
parent 9edd9fb7b6
commit dd22d7a164
5 changed files with 56 additions and 15 deletions

21
Cargo.lock generated
View File

@ -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"

View File

@ -7,6 +7,7 @@ publish = false
[dependencies]
slang-sys = { path = "slang-sys" }
thiserror = "2.0"
[workspace]
members = [

View File

@ -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)

View File

@ -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")
}
}
}

View File

@ -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<T> = std::result::Result<T, Error>;
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(