Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map TryFromIntError to ERROR_INVALID_DATA in windows-result #2851

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}