-
Notifications
You must be signed in to change notification settings - Fork 944
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
PR #5956 wasn't fully complete and still had some outstanding minor issues and cleanups to be done, as well as hidden semantic changes. This addresses a bunch of them: - Remove unnecessary `Error` mapping to `String` as `windows-rs`'s `Error` has a more complete `Display` representation by itself. - Remove `into_result()` as every call could have formatted the `windows-rs` `Error` in a log call directly. - Pass `None` instead of a pointer to an empty slice wherever possible (waiting for microsoft/win32metadata#1971 to trickle down into `windows-rs`). - Remove `.clone()` on COM objects (temporarily increasing the refcount) when it can be avoided by inverting the order of operations on `raw` variables.
- Loading branch information
Showing
8 changed files
with
59 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,32 @@ | ||
use std::borrow::Cow; | ||
|
||
use windows::Win32::{Foundation, Graphics::Dxgi}; | ||
|
||
pub(crate) trait HResult<O> { | ||
fn into_result(self) -> Result<O, Cow<'static, str>>; | ||
fn into_device_result(self, description: &str) -> Result<O, crate::DeviceError>; | ||
} | ||
impl<T> HResult<T> for windows::core::Result<T> { | ||
fn into_result(self) -> Result<T, Cow<'static, str>> { | ||
// TODO: use windows-rs built-in error formatting? | ||
let description = match self { | ||
Ok(t) => return Ok(t), | ||
Err(e) if e.code() == Foundation::E_UNEXPECTED => "unexpected", | ||
Err(e) if e.code() == Foundation::E_NOTIMPL => "not implemented", | ||
Err(e) if e.code() == Foundation::E_OUTOFMEMORY => "out of memory", | ||
Err(e) if e.code() == Foundation::E_INVALIDARG => "invalid argument", | ||
Err(e) => return Err(Cow::Owned(format!("{e:?}"))), | ||
}; | ||
Err(Cow::Borrowed(description)) | ||
} | ||
fn into_device_result(self, description: &str) -> Result<T, crate::DeviceError> { | ||
#![allow(unreachable_code)] | ||
|
||
let err_code = if let Err(err) = &self { | ||
Some(err.code()) | ||
} else { | ||
None | ||
}; | ||
self.into_result().map_err(|err| { | ||
self.map_err(|err| { | ||
log::error!("{} failed: {}", description, err); | ||
|
||
let Some(err_code) = err_code else { | ||
unreachable!() | ||
}; | ||
|
||
match err_code { | ||
match err.code() { | ||
Foundation::E_OUTOFMEMORY => { | ||
#[cfg(feature = "oom_panic")] | ||
panic!("{description} failed: Out of memory"); | ||
return crate::DeviceError::OutOfMemory; | ||
crate::DeviceError::OutOfMemory | ||
} | ||
Dxgi::DXGI_ERROR_DEVICE_RESET | Dxgi::DXGI_ERROR_DEVICE_REMOVED => { | ||
#[cfg(feature = "device_lost_panic")] | ||
panic!("{description} failed: Device lost ({err})"); | ||
crate::DeviceError::Lost | ||
} | ||
_ => { | ||
#[cfg(feature = "internal_error_panic")] | ||
panic!("{description} failed: {err}"); | ||
crate::DeviceError::Unexpected | ||
} | ||
} | ||
|
||
crate::DeviceError::Lost | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters