From 1f0bc8603831d16d837ee3e202d6855ebbaf54af Mon Sep 17 00:00:00 2001 From: Arlie Davis Date: Fri, 14 Jun 2024 10:46:11 -0700 Subject: [PATCH] make VARIANT fail better on non-Windows --- .../mod/Win32/System/Com/IDispatch.rs | 4 ++ crates/libs/core/src/variant.rs | 46 +++++++++++++++++++ .../src/Windows/Win32/System/Com/mod.rs | 4 ++ 3 files changed, 54 insertions(+) diff --git a/crates/libs/bindgen/src/rust/extensions/mod/Win32/System/Com/IDispatch.rs b/crates/libs/bindgen/src/rust/extensions/mod/Win32/System/Com/IDispatch.rs index 84162b72fe5..5d5adc34a1d 100644 --- a/crates/libs/bindgen/src/rust/extensions/mod/Win32/System/Com/IDispatch.rs +++ b/crates/libs/bindgen/src/rust/extensions/mod/Win32/System/Com/IDispatch.rs @@ -1,3 +1,4 @@ +#[cfg(windows)] impl From for windows_core::VARIANT { fn from(value: IDispatch) -> Self { unsafe { @@ -10,6 +11,7 @@ impl From for windows_core::VARIANT { } } +#[cfg(windows)] impl From for windows_core::PROPVARIANT { fn from(value: IDispatch) -> Self { unsafe { @@ -22,6 +24,7 @@ impl From for windows_core::PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&windows_core::VARIANT> for IDispatch { type Error = windows_core::Error; fn try_from(from: &windows_core::VARIANT) -> windows_core::Result { @@ -37,6 +40,7 @@ impl TryFrom<&windows_core::VARIANT> for IDispatch { } } +#[cfg(windows)] impl TryFrom<&windows_core::PROPVARIANT> for IDispatch { type Error = windows_core::Error; fn try_from(from: &windows_core::PROPVARIANT) -> windows_core::Result { diff --git a/crates/libs/core/src/variant.rs b/crates/libs/core/src/variant.rs index ca85aa0c104..5e612c774bc 100644 --- a/crates/libs/core/src/variant.rs +++ b/crates/libs/core/src/variant.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(windows), allow(unused_variables))] + use super::*; use core::mem::transmute; @@ -23,30 +25,40 @@ impl Default for PROPVARIANT { impl Clone for VARIANT { fn clone(&self) -> Self { + #[cfg(windows)] unsafe { let mut value = Self::new(); imp::VariantCopy(&mut value.0, &self.0); value } + + #[cfg(not(windows))] + unimplemented!(); } } impl Clone for PROPVARIANT { fn clone(&self) -> Self { + #[cfg(windows)] unsafe { let mut value = Self::new(); imp::PropVariantCopy(&mut value.0, &self.0); value } + + #[cfg(not(windows))] + unimplemented!(); } } +#[cfg(windows)] impl Drop for VARIANT { fn drop(&mut self) { unsafe { imp::VariantClear(&mut self.0) }; } } +#[cfg(windows)] impl Drop for PROPVARIANT { fn drop(&mut self) { unsafe { imp::PropVariantClear(&mut self.0) }; @@ -66,6 +78,7 @@ impl core::fmt::Debug for VARIANT { let mut debug = f.debug_struct("VARIANT"); debug.field("type", &unsafe { self.0.Anonymous.Anonymous.vt }); + #[cfg(windows)] if let Ok(value) = BSTR::try_from(self) { debug.field("value", &value); } @@ -79,6 +92,7 @@ impl core::fmt::Debug for PROPVARIANT { let mut debug = f.debug_struct("PROPVARIANT"); debug.field("type", &unsafe { self.0.Anonymous.Anonymous.vt }); + #[cfg(windows)] if let Ok(value) = BSTR::try_from(self) { debug.field("value", &value); } @@ -87,12 +101,14 @@ impl core::fmt::Debug for PROPVARIANT { } } +#[cfg(windows)] impl core::fmt::Display for VARIANT { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { core::write!(f, "{}", BSTR::try_from(self).unwrap_or_default()) } } +#[cfg(windows)] impl core::fmt::Display for PROPVARIANT { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { core::write!(f, "{}", BSTR::try_from(self).unwrap_or_default()) @@ -101,6 +117,7 @@ impl core::fmt::Display for PROPVARIANT { impl PartialEq for VARIANT { fn eq(&self, other: &Self) -> bool { + #[cfg(windows)] unsafe { if self.0.Anonymous.Anonymous.vt != other.0.Anonymous.Anonymous.vt { return false; @@ -114,11 +131,15 @@ impl PartialEq for VARIANT { false } } + + #[cfg(not(windows))] + unimplemented!(); } } impl PartialEq for PROPVARIANT { fn eq(&self, other: &Self) -> bool { + #[cfg(windows)] unsafe { if self.0.Anonymous.Anonymous.vt != other.0.Anonymous.Anonymous.vt { return false; @@ -126,6 +147,9 @@ impl PartialEq for PROPVARIANT { imp::PropVariantCompareEx(&self.0, &other.0, 0, 0) == 0 } + + #[cfg(not(windows))] + unimplemented!(); } } @@ -188,6 +212,7 @@ impl PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&VARIANT> for PROPVARIANT { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -198,6 +223,7 @@ impl TryFrom<&VARIANT> for PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&PROPVARIANT> for VARIANT { type Error = Error; fn try_from(from: &PROPVARIANT) -> Result { @@ -316,18 +342,21 @@ impl From for PROPVARIANT { } } +#[cfg(windows)] impl From<&str> for VARIANT { fn from(value: &str) -> Self { BSTR::from(value).into() } } +#[cfg(windows)] impl From<&str> for PROPVARIANT { fn from(value: &str) -> Self { BSTR::from(value).into() } } +#[cfg(windows)] impl TryFrom<&VARIANT> for BSTR { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -336,6 +365,7 @@ impl TryFrom<&VARIANT> for BSTR { } } +#[cfg(windows)] impl TryFrom<&PROPVARIANT> for BSTR { type Error = Error; fn try_from(from: &PROPVARIANT) -> Result { @@ -383,6 +413,7 @@ impl From for PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&VARIANT> for bool { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -391,6 +422,7 @@ impl TryFrom<&VARIANT> for bool { } } +#[cfg(windows)] impl TryFrom<&PROPVARIANT> for bool { type Error = Error; fn try_from(from: &PROPVARIANT) -> Result { @@ -501,6 +533,7 @@ impl From for PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&VARIANT> for u16 { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -509,6 +542,7 @@ impl TryFrom<&VARIANT> for u16 { } } +#[cfg(windows)] impl TryFrom<&PROPVARIANT> for u16 { type Error = Error; fn try_from(from: &PROPVARIANT) -> Result { @@ -551,6 +585,7 @@ impl From for PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&VARIANT> for i16 { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -559,6 +594,7 @@ impl TryFrom<&VARIANT> for i16 { } } +#[cfg(windows)] impl TryFrom<&PROPVARIANT> for i16 { type Error = Error; fn try_from(from: &PROPVARIANT) -> Result { @@ -601,6 +637,7 @@ impl From for PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&VARIANT> for u32 { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -609,6 +646,7 @@ impl TryFrom<&VARIANT> for u32 { } } +#[cfg(windows)] impl TryFrom<&PROPVARIANT> for u32 { type Error = Error; fn try_from(from: &PROPVARIANT) -> Result { @@ -651,6 +689,7 @@ impl From for PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&VARIANT> for i32 { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -659,6 +698,7 @@ impl TryFrom<&VARIANT> for i32 { } } +#[cfg(windows)] impl TryFrom<&PROPVARIANT> for i32 { type Error = Error; fn try_from(from: &PROPVARIANT) -> Result { @@ -701,6 +741,7 @@ impl From for PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&VARIANT> for u64 { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -709,6 +750,7 @@ impl TryFrom<&VARIANT> for u64 { } } +#[cfg(windows)] impl TryFrom<&PROPVARIANT> for u64 { type Error = Error; fn try_from(from: &PROPVARIANT) -> Result { @@ -751,6 +793,7 @@ impl From for PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&VARIANT> for i64 { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -759,6 +802,7 @@ impl TryFrom<&VARIANT> for i64 { } } +#[cfg(windows)] impl TryFrom<&PROPVARIANT> for i64 { type Error = Error; fn try_from(from: &PROPVARIANT) -> Result { @@ -835,6 +879,7 @@ impl From for PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&VARIANT> for f64 { type Error = Error; fn try_from(from: &VARIANT) -> Result { @@ -843,6 +888,7 @@ impl TryFrom<&VARIANT> for f64 { } } +#[cfg(windows)] impl TryFrom<&PROPVARIANT> for f64 { type Error = Error; fn try_from(from: &PROPVARIANT) -> Result { diff --git a/crates/libs/windows/src/Windows/Win32/System/Com/mod.rs b/crates/libs/windows/src/Windows/Win32/System/Com/mod.rs index 03ff5c386f8..6214364f42b 100644 --- a/crates/libs/windows/src/Windows/Win32/System/Com/mod.rs +++ b/crates/libs/windows/src/Windows/Win32/System/Com/mod.rs @@ -7664,6 +7664,7 @@ pub type LPEXCEPFINO_DEFERRED_FILLIN = Option windows_core::HRESULT>; pub type LPFNGETCLASSOBJECT = Option windows_core::HRESULT>; pub type PFNCONTEXTCALL = Option windows_core::HRESULT>; +#[cfg(windows)] impl From for windows_core::VARIANT { fn from(value: IDispatch) -> Self { unsafe { @@ -7676,6 +7677,7 @@ impl From for windows_core::VARIANT { } } +#[cfg(windows)] impl From for windows_core::PROPVARIANT { fn from(value: IDispatch) -> Self { unsafe { @@ -7688,6 +7690,7 @@ impl From for windows_core::PROPVARIANT { } } +#[cfg(windows)] impl TryFrom<&windows_core::VARIANT> for IDispatch { type Error = windows_core::Error; fn try_from(from: &windows_core::VARIANT) -> windows_core::Result { @@ -7703,6 +7706,7 @@ impl TryFrom<&windows_core::VARIANT> for IDispatch { } } +#[cfg(windows)] impl TryFrom<&windows_core::PROPVARIANT> for IDispatch { type Error = windows_core::Error; fn try_from(from: &windows_core::PROPVARIANT) -> windows_core::Result {