From b2ce099eb57cf1aee253fbafe5195b8e49912aa8 Mon Sep 17 00:00:00 2001 From: Kenny Kerr Date: Fri, 16 Feb 2024 13:01:18 -0600 Subject: [PATCH] Map `TryFromIntError` to `ERROR_INVALID_DATA` in `windows-result` (#2851) --- crates/libs/result/src/bindings.rs | 1 + crates/libs/result/src/error.rs | 2 +- crates/libs/result/tests/bindings.txt | 1 + crates/tests/result/tests/error.rs | 13 +++++++++++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/libs/result/src/bindings.rs b/crates/libs/result/src/bindings.rs index 99f2c5a4dc..00ee3cbb03 100644 --- a/crates/libs/result/src/bindings.rs +++ b/crates/libs/result/src/bindings.rs @@ -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; diff --git a/crates/libs/result/src/error.rs b/crates/libs/result/src/error.rs index 60dad5efe7..59c25beea1 100644 --- a/crates/libs/result/src/error.rs +++ b/crates/libs/result/src/error.rs @@ -160,7 +160,7 @@ impl From for Error { impl From for Error { fn from(_: std::num::TryFromIntError) -> Self { Self { - code: HRESULT(E_INVALIDARG), + code: HRESULT::from_win32(ERROR_INVALID_DATA), info: None, } } diff --git a/crates/libs/result/tests/bindings.txt b/crates/libs/result/tests/bindings.txt index ddef671a55..845ff1828b 100644 --- a/crates/libs/result/tests/bindings.txt +++ b/crates/libs/result/tests/bindings.txt @@ -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 diff --git a/crates/tests/result/tests/error.rs b/crates/tests/result/tests/error.rs index 4ca95d3e28..43b5b20af5 100644 --- a/crates/tests/result/tests/error.rs +++ b/crates/tests/result/tests/error.rs @@ -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)); @@ -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 { + 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)); +}