Implement standard errors
This commit is contained in:
parent
9edd9fb7b6
commit
dd22d7a164
21
Cargo.lock
generated
21
Cargo.lock
generated
@ -220,6 +220,7 @@ name = "slang"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"slang-sys",
|
"slang-sys",
|
||||||
|
"thiserror",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -241,6 +242,26 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"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]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.14"
|
version = "1.0.14"
|
||||||
|
@ -7,6 +7,7 @@ publish = false
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
slang-sys = { path = "slang-sys" }
|
slang-sys = { path = "slang-sys" }
|
||||||
|
thiserror = "2.0"
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
|
@ -54,6 +54,7 @@ fn main() {
|
|||||||
})
|
})
|
||||||
.constified_enum("SlangProfileID")
|
.constified_enum("SlangProfileID")
|
||||||
.constified_enum("SlangCapabilityID")
|
.constified_enum("SlangCapabilityID")
|
||||||
|
.new_type_alias("SlangResult")
|
||||||
.vtable_generation(true)
|
.vtable_generation(true)
|
||||||
.layout_tests(false)
|
.layout_tests(false)
|
||||||
.derive_copy(true)
|
.derive_copy(true)
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||||
|
|
||||||
use std::ffi::{c_char, c_int, c_void};
|
use std::ffi::{c_char, c_int, c_void};
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
// Based on Slang version 2024.14.5
|
// Based on Slang version 2024.14.5
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
@ -135,3 +135,13 @@ pub struct IModuleVtable {
|
|||||||
pub getDependencyFilePath: unsafe extern "C" fn(*mut c_void, index: SlangInt32) -> *const c_char,
|
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,
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
36
src/lib.rs
36
src/lib.rs
@ -1,6 +1,7 @@
|
|||||||
pub mod reflection;
|
pub mod reflection;
|
||||||
|
|
||||||
use std::ffi::{CStr, CString};
|
use std::ffi::{CStr, CString};
|
||||||
|
use std::fmt::{Debug, Display, Formatter};
|
||||||
use std::ptr::{null, null_mut};
|
use std::ptr::{null, null_mut};
|
||||||
|
|
||||||
use slang_sys as sys;
|
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 {
|
pub enum Error {
|
||||||
Code(sys::SlangResult),
|
#[error("underlying library error: `{0}`")]
|
||||||
Blob(Blob),
|
Slang(sys::SlangResult),
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Debug for Error {
|
#[error("blob")]
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
Blob(Blob),
|
||||||
match self {
|
|
||||||
Error::Code(code) => write!(f, "{}", code),
|
|
||||||
Error::Blob(blob) => write!(f, "{}", blob.as_str().unwrap()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
pub type Result<T> = std::result::Result<T, Error>;
|
||||||
|
|
||||||
pub(crate) fn succeeded(result: sys::SlangResult) -> bool {
|
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<()> {
|
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(
|
Err(Error::Blob(Blob(IUnknown(
|
||||||
std::ptr::NonNull::new(blob as *mut _).unwrap(),
|
std::ptr::NonNull::new(blob as *mut _).unwrap(),
|
||||||
))))
|
))))
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,6 +121,18 @@ impl Drop for IUnknown {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Blob(IUnknown);
|
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 {
|
unsafe impl Interface for Blob {
|
||||||
type Vtable = sys::IBlobVtable;
|
type Vtable = sys::IBlobVtable;
|
||||||
const IID: UUID = uuid(
|
const IID: UUID = uuid(
|
||||||
|
Loading…
Reference in New Issue
Block a user