diff --git a/crates/libs/bindgen/src/rust/winrt_methods.rs b/crates/libs/bindgen/src/rust/winrt_methods.rs index c9ecbbc268..6f589b2f17 100644 --- a/crates/libs/bindgen/src/rust/winrt_methods.rs +++ b/crates/libs/bindgen/src/rust/winrt_methods.rs @@ -138,9 +138,9 @@ fn gen_winrt_abi_args(writer: &Writer, params: &[SignatureParam]) -> TokenStream let param = if param.def.flags().contains(ParamAttributes::In) { if param.ty.is_winrt_array() { if type_is_blittable(¶m.ty) { - quote! { #name.len() as u32, #name.as_ptr(), } + quote! { #name.len().try_into()?, #name.as_ptr(), } } else { - quote! { #name.len() as u32, ::core::mem::transmute(#name.as_ptr()), } + quote! { #name.len().try_into()?, ::core::mem::transmute(#name.as_ptr()), } } } else if type_is_non_exclusive_winrt_interface(¶m.ty) { quote! { #name.try_into_param()?.abi(), } @@ -157,9 +157,9 @@ fn gen_winrt_abi_args(writer: &Writer, params: &[SignatureParam]) -> TokenStream } } else if param.ty.is_winrt_array() { if type_is_blittable(¶m.ty) { - quote! { #name.len() as u32, #name.as_mut_ptr(), } + quote! { #name.len().try_into()?, #name.as_mut_ptr(), } } else { - quote! { #name.len() as u32, ::core::mem::transmute_copy(&#name), } + quote! { #name.len().try_into()?, ::core::mem::transmute_copy(&#name), } } } else if param.ty.is_winrt_array_ref() { quote! { #name.set_abi_len(), #name as *mut _ as _, } diff --git a/crates/libs/core/src/error.rs b/crates/libs/core/src/error.rs index 6a4e458dd5..b851a21311 100644 --- a/crates/libs/core/src/error.rs +++ b/crates/libs/core/src/error.rs @@ -90,6 +90,12 @@ impl std::convert::From for Error { } } +impl std::convert::From for Error { + fn from(_: std::num::TryFromIntError) -> Self { + Self { code: HRESULT(crate::imp::E_INVALIDARG), info: None } + } +} + // Unfortunately this is needed to make types line up. The Rust type system does // not know the `Infallible` can never be constructed. This code needs to be here // to satesify the type checker but it will never be run. Once `!` is stabilizied diff --git a/crates/libs/core/src/imp/bindings.rs b/crates/libs/core/src/imp/bindings.rs index 1f5b31329e..840d673648 100644 --- a/crates/libs/core/src/imp/bindings.rs +++ b/crates/libs/core/src/imp/bindings.rs @@ -22,6 +22,7 @@ pub type BOOL = i32; pub type BSTR = *const u16; pub const ERROR_NO_UNICODE_TRANSLATION: WIN32_ERROR = 1113u32; +pub const E_INVALIDARG: HRESULT = -2147024809i32; pub type FARPROC = ::core::option::Option isize>; pub const FORMAT_MESSAGE_ALLOCATE_BUFFER: FORMAT_MESSAGE_OPTIONS = 256u32; pub const FORMAT_MESSAGE_FROM_SYSTEM: FORMAT_MESSAGE_OPTIONS = 4096u32; @@ -30,6 +31,7 @@ pub type FORMAT_MESSAGE_OPTIONS = u32; pub type HANDLE = isize; pub type HEAP_FLAGS = u32; pub type HMODULE = isize; +pub type HRESULT = i32; pub type LOAD_LIBRARY_FLAGS = u32; pub const LOAD_LIBRARY_SEARCH_DEFAULT_DIRS: LOAD_LIBRARY_FLAGS = 4096u32; pub type PCSTR = *const u8; diff --git a/crates/libs/core/src/imp/com_bindings.rs b/crates/libs/core/src/imp/com_bindings.rs index 4707b1d4ee..338829c627 100644 --- a/crates/libs/core/src/imp/com_bindings.rs +++ b/crates/libs/core/src/imp/com_bindings.rs @@ -1147,115 +1147,115 @@ impl PropertyValue { pub fn CreateUInt8Array(value: &[u8]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateUInt8Array)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateUInt8Array)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateInt16Array(value: &[i16]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateInt16Array)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateInt16Array)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateUInt16Array(value: &[u16]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateUInt16Array)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateUInt16Array)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateInt32Array(value: &[i32]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateInt32Array)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateInt32Array)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateUInt32Array(value: &[u32]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateUInt32Array)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateUInt32Array)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateInt64Array(value: &[i64]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateInt64Array)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateInt64Array)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateUInt64Array(value: &[u64]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateUInt64Array)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateUInt64Array)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateSingleArray(value: &[f32]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateSingleArray)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateSingleArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateDoubleArray(value: &[f64]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateDoubleArray)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateDoubleArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateChar16Array(value: &[u16]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateChar16Array)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateChar16Array)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateBooleanArray(value: &[bool]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateBooleanArray)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateBooleanArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateStringArray(value: &[::windows_core::HSTRING]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateStringArray)(::windows_core::Interface::as_raw(this), value.len() as u32, ::core::mem::transmute(value.as_ptr()), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateStringArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, ::core::mem::transmute(value.as_ptr()), &mut result__).from_abi(result__) }) } pub fn CreateInspectableArray(value: &[::core::option::Option<::windows_core::IInspectable>]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateInspectableArray)(::windows_core::Interface::as_raw(this), value.len() as u32, ::core::mem::transmute(value.as_ptr()), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateInspectableArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, ::core::mem::transmute(value.as_ptr()), &mut result__).from_abi(result__) }) } pub fn CreateGuidArray(value: &[::windows_core::GUID]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateGuidArray)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateGuidArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateDateTimeArray(value: &[DateTime]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateDateTimeArray)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateDateTimeArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateTimeSpanArray(value: &[TimeSpan]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateTimeSpanArray)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateTimeSpanArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreatePointArray(value: &[Point]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreatePointArray)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreatePointArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateSizeArray(value: &[Size]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateSizeArray)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateSizeArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } pub fn CreateRectArray(value: &[Rect]) -> ::windows_core::Result<::windows_core::IInspectable> { Self::IPropertyValueStatics(|this| unsafe { let mut result__ = ::std::mem::zeroed(); - (::windows_core::Interface::vtable(this).CreateRectArray)(::windows_core::Interface::as_raw(this), value.len() as u32, value.as_ptr(), &mut result__).from_abi(result__) + (::windows_core::Interface::vtable(this).CreateRectArray)(::windows_core::Interface::as_raw(this), value.len().try_into()?, value.as_ptr(), &mut result__).from_abi(result__) }) } #[doc(hidden)] diff --git a/crates/libs/core/src/strings/bstr.rs b/crates/libs/core/src/strings/bstr.rs index 63f751c3c3..5003117f92 100644 --- a/crates/libs/core/src/strings/bstr.rs +++ b/crates/libs/core/src/strings/bstr.rs @@ -42,7 +42,7 @@ impl BSTR { return Ok(Self::new()); } - let result = unsafe { Self(crate::imp::SysAllocStringLen(value.as_ptr(), value.len() as u32)) }; + let result = unsafe { Self(crate::imp::SysAllocStringLen(value.as_ptr(), value.len().try_into()?)) }; if result.is_empty() { Err(crate::imp::E_OUTOFMEMORY.into()) diff --git a/crates/libs/core/src/strings/hstring.rs b/crates/libs/core/src/strings/hstring.rs index 014da49d92..a0a7f59fab 100644 --- a/crates/libs/core/src/strings/hstring.rs +++ b/crates/libs/core/src/strings/hstring.rs @@ -45,7 +45,7 @@ impl HSTRING { /// Create a `HSTRING` from a slice of 16 bit characters (wchars). pub fn from_wide(value: &[u16]) -> Result { - unsafe { Self::from_wide_iter(value.iter().copied(), value.len() as u32) } + unsafe { Self::from_wide_iter(value.iter().copied(), value.len()) } } /// Get the contents of this `HSTRING` as a String lossily. @@ -61,17 +61,17 @@ impl HSTRING { /// # Safety /// len must not be less than the number of items in the iterator. - unsafe fn from_wide_iter>(iter: I, len: u32) -> Result { + unsafe fn from_wide_iter>(iter: I, len: usize) -> Result { if len == 0 { return Ok(Self::new()); } - let ptr = Header::alloc(len)?; + let ptr = Header::alloc(len.try_into()?)?; // Place each utf-16 character into the buffer and // increase len as we go along. for (index, wide) in iter.enumerate() { - debug_assert!((index as u32) < len); + debug_assert!(index < len); std::ptr::write((*ptr).data.add(index), wide); (*ptr).len = index as u32 + 1; @@ -151,7 +151,7 @@ impl std::fmt::Debug for HSTRING { impl std::convert::From<&str> for HSTRING { fn from(value: &str) -> Self { - unsafe { Self::from_wide_iter(value.encode_utf16(), value.len() as u32).unwrap() } + unsafe { Self::from_wide_iter(value.encode_utf16(), value.len()).unwrap() } } } @@ -177,7 +177,7 @@ impl std::convert::From<&std::path::Path> for HSTRING { #[cfg(windows)] impl std::convert::From<&std::ffi::OsStr> for HSTRING { fn from(value: &std::ffi::OsStr) -> Self { - unsafe { Self::from_wide_iter(std::os::windows::ffi::OsStrExt::encode_wide(value), value.len() as u32).unwrap() } + unsafe { Self::from_wide_iter(std::os::windows::ffi::OsStrExt::encode_wide(value), value.len()).unwrap() } } } diff --git a/crates/tests/component/src/bindings.rs b/crates/tests/component/src/bindings.rs index a327809a66..e3c2781b95 100644 --- a/crates/tests/component/src/bindings.rs +++ b/crates/tests/component/src/bindings.rs @@ -197,9 +197,9 @@ impl Class { let mut result__ = ::core::mem::MaybeUninit::zeroed(); (::windows_core::Interface::vtable(this).Int32Array)( ::windows_core::Interface::as_raw(this), - a.len() as u32, + a.len().try_into()?, a.as_ptr(), - b.len() as u32, + b.len().try_into()?, b.as_mut_ptr(), c.set_abi_len(), c as *mut _ as _, @@ -220,9 +220,9 @@ impl Class { let mut result__ = ::core::mem::MaybeUninit::zeroed(); (::windows_core::Interface::vtable(this).StringArray)( ::windows_core::Interface::as_raw(this), - a.len() as u32, + a.len().try_into()?, ::core::mem::transmute(a.as_ptr()), - b.len() as u32, + b.len().try_into()?, ::core::mem::transmute_copy(&b), c.set_abi_len(), c as *mut _ as _, diff --git a/crates/tests/component_client/src/bindings.rs b/crates/tests/component_client/src/bindings.rs index 0d8d3b0aa0..950bdfe4ae 100644 --- a/crates/tests/component_client/src/bindings.rs +++ b/crates/tests/component_client/src/bindings.rs @@ -165,9 +165,9 @@ impl Class { let mut result__ = ::core::mem::MaybeUninit::zeroed(); (::windows_core::Interface::vtable(this).Int32Array)( ::windows_core::Interface::as_raw(this), - a.len() as u32, + a.len().try_into()?, a.as_ptr(), - b.len() as u32, + b.len().try_into()?, b.as_mut_ptr(), c.set_abi_len(), c as *mut _ as _, @@ -188,9 +188,9 @@ impl Class { let mut result__ = ::core::mem::MaybeUninit::zeroed(); (::windows_core::Interface::vtable(this).StringArray)( ::windows_core::Interface::as_raw(this), - a.len() as u32, + a.len().try_into()?, ::core::mem::transmute(a.as_ptr()), - b.len() as u32, + b.len().try_into()?, ::core::mem::transmute_copy(&b), c.set_abi_len(), c as *mut _ as _, diff --git a/crates/tests/standalone/src/b_calendar.rs b/crates/tests/standalone/src/b_calendar.rs index 96563a3bc2..43f4e278e1 100644 --- a/crates/tests/standalone/src/b_calendar.rs +++ b/crates/tests/standalone/src/b_calendar.rs @@ -1946,7 +1946,7 @@ impl IIterator { let mut result__ = ::std::mem::zeroed(); (::windows_core::Interface::vtable(this).GetMany)( ::windows_core::Interface::as_raw(this), - items.len() as u32, + items.len().try_into()?, ::core::mem::transmute_copy(&items), &mut result__, ) @@ -2107,7 +2107,7 @@ impl IVectorView { (::windows_core::Interface::vtable(this).GetMany)( ::windows_core::Interface::as_raw(this), startindex, - items.len() as u32, + items.len().try_into()?, ::core::mem::transmute_copy(&items), &mut result__, ) diff --git a/crates/tests/standalone/src/b_uri.rs b/crates/tests/standalone/src/b_uri.rs index 96e3bb1001..30ccce5907 100644 --- a/crates/tests/standalone/src/b_uri.rs +++ b/crates/tests/standalone/src/b_uri.rs @@ -127,7 +127,7 @@ impl IIterator { let mut result__ = ::std::mem::zeroed(); (::windows_core::Interface::vtable(this).GetMany)( ::windows_core::Interface::as_raw(this), - items.len() as u32, + items.len().try_into()?, ::core::mem::transmute_copy(&items), &mut result__, ) @@ -459,7 +459,7 @@ impl IVectorView { (::windows_core::Interface::vtable(this).GetMany)( ::windows_core::Interface::as_raw(this), startindex, - items.len() as u32, + items.len().try_into()?, ::core::mem::transmute_copy(&items), &mut result__, ) @@ -1070,7 +1070,7 @@ impl WwwFormUrlDecoder { (::windows_core::Interface::vtable(this).GetMany)( ::windows_core::Interface::as_raw(this), startindex, - items.len() as u32, + items.len().try_into()?, ::core::mem::transmute_copy(&items), &mut result__, ) diff --git a/crates/tools/core/bindings.txt b/crates/tools/core/bindings.txt index 2c61dd4543..6f902cc929 100644 --- a/crates/tools/core/bindings.txt +++ b/crates/tools/core/bindings.txt @@ -6,6 +6,7 @@ --filter Windows.Win32.Foundation.CloseHandle Windows.Win32.Foundation.ERROR_NO_UNICODE_TRANSLATION + Windows.Win32.Foundation.E_INVALIDARG Windows.Win32.Foundation.FreeLibrary Windows.Win32.Foundation.GetLastError Windows.Win32.Foundation.SysAllocStringLen