Skip to content

Commit

Permalink
Map TryFromIntError to ERROR_INVALID_DATA in windows-result (#2851
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kennykerr authored Feb 16, 2024
1 parent aeaa794 commit b2ce099
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/libs/result/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
::windows_targets::link!("oleaut32.dll" "system" fn SysStringLen(pbstr : BSTR) -> u32);
pub type BOOL = i32;
pub type BSTR = *const u16;
pub const ERROR_INVALID_DATA: WIN32_ERROR = 13u32;
pub const ERROR_NO_UNICODE_TRANSLATION: WIN32_ERROR = 1113u32;
pub const E_INVALIDARG: HRESULT = -2147024809i32;
pub const E_UNEXPECTED: HRESULT = -2147418113i32;
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/result/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl From<std::string::FromUtf8Error> for Error {
impl From<std::num::TryFromIntError> for Error {
fn from(_: std::num::TryFromIntError) -> Self {
Self {
code: HRESULT(E_INVALIDARG),
code: HRESULT::from_win32(ERROR_INVALID_DATA),
info: None,
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/libs/result/tests/bindings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
--filter
Windows.Win32.Foundation.E_INVALIDARG
Windows.Win32.Foundation.E_UNEXPECTED
Windows.Win32.Foundation.ERROR_INVALID_DATA
Windows.Win32.Foundation.ERROR_NO_UNICODE_TRANSLATION
Windows.Win32.Foundation.GetLastError
Windows.Win32.Foundation.SysFreeString
Expand Down
13 changes: 13 additions & 0 deletions crates/tests/result/tests/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use windows_result::*;
const S_OK: HRESULT = HRESULT(0);
const E_INVALIDARG: HRESULT = HRESULT(-2147024809i32);
const ERROR_CANCELLED: u32 = 1223;
const ERROR_INVALID_DATA: u32 = 13;
const E_CANCELLED: HRESULT = HRESULT::from_win32(ERROR_CANCELLED);

windows_targets::link!("kernel32.dll" "system" fn SetLastError(code: u32));
Expand Down Expand Up @@ -49,3 +50,15 @@ fn from_win32() {
assert!(e.as_ptr().is_null());
assert_eq!(e.code(), E_CANCELLED);
}

#[test]
fn try_from_int() {
fn call(value: usize) -> Result<u16> {
Ok(value.try_into()?)
}

assert_eq!(call(123), Ok(123));

let e = call(usize::MAX).unwrap_err();
assert_eq!(e.code(), HRESULT::from_win32(ERROR_INVALID_DATA));
}

0 comments on commit b2ce099

Please sign in to comment.