Skip to content

Commit

Permalink
make VARIANT fail better on non-Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlie Davis committed Jun 14, 2024
1 parent 7f1a7ff commit 1f0bc86
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(windows)]
impl From<IDispatch> for windows_core::VARIANT {
fn from(value: IDispatch) -> Self {
unsafe {
Expand All @@ -10,6 +11,7 @@ impl From<IDispatch> for windows_core::VARIANT {
}
}

#[cfg(windows)]
impl From<IDispatch> for windows_core::PROPVARIANT {
fn from(value: IDispatch) -> Self {
unsafe {
Expand All @@ -22,6 +24,7 @@ impl From<IDispatch> 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<Self> {
Expand All @@ -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<Self> {
Expand Down
46 changes: 46 additions & 0 deletions crates/libs/core/src/variant.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(not(windows), allow(unused_variables))]

use super::*;
use core::mem::transmute;

Expand All @@ -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) };
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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())
Expand All @@ -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;
Expand All @@ -114,18 +131,25 @@ 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;
}

imp::PropVariantCompareEx(&self.0, &other.0, 0, 0) == 0
}

#[cfg(not(windows))]
unimplemented!();
}
}

Expand Down Expand Up @@ -188,6 +212,7 @@ impl PROPVARIANT {
}
}

#[cfg(windows)]
impl TryFrom<&VARIANT> for PROPVARIANT {
type Error = Error;
fn try_from(from: &VARIANT) -> Result<Self> {
Expand All @@ -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<Self> {
Expand Down Expand Up @@ -316,18 +342,21 @@ impl From<BSTR> 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<Self> {
Expand All @@ -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<Self> {
Expand Down Expand Up @@ -383,6 +413,7 @@ impl From<bool> for PROPVARIANT {
}
}

#[cfg(windows)]
impl TryFrom<&VARIANT> for bool {
type Error = Error;
fn try_from(from: &VARIANT) -> Result<Self> {
Expand All @@ -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<Self> {
Expand Down Expand Up @@ -501,6 +533,7 @@ impl From<u16> for PROPVARIANT {
}
}

#[cfg(windows)]
impl TryFrom<&VARIANT> for u16 {
type Error = Error;
fn try_from(from: &VARIANT) -> Result<Self> {
Expand All @@ -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<Self> {
Expand Down Expand Up @@ -551,6 +585,7 @@ impl From<i16> for PROPVARIANT {
}
}

#[cfg(windows)]
impl TryFrom<&VARIANT> for i16 {
type Error = Error;
fn try_from(from: &VARIANT) -> Result<Self> {
Expand All @@ -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<Self> {
Expand Down Expand Up @@ -601,6 +637,7 @@ impl From<u32> for PROPVARIANT {
}
}

#[cfg(windows)]
impl TryFrom<&VARIANT> for u32 {
type Error = Error;
fn try_from(from: &VARIANT) -> Result<Self> {
Expand All @@ -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<Self> {
Expand Down Expand Up @@ -651,6 +689,7 @@ impl From<i32> for PROPVARIANT {
}
}

#[cfg(windows)]
impl TryFrom<&VARIANT> for i32 {
type Error = Error;
fn try_from(from: &VARIANT) -> Result<Self> {
Expand All @@ -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<Self> {
Expand Down Expand Up @@ -701,6 +741,7 @@ impl From<u64> for PROPVARIANT {
}
}

#[cfg(windows)]
impl TryFrom<&VARIANT> for u64 {
type Error = Error;
fn try_from(from: &VARIANT) -> Result<Self> {
Expand All @@ -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<Self> {
Expand Down Expand Up @@ -751,6 +793,7 @@ impl From<i64> for PROPVARIANT {
}
}

#[cfg(windows)]
impl TryFrom<&VARIANT> for i64 {
type Error = Error;
fn try_from(from: &VARIANT) -> Result<Self> {
Expand All @@ -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<Self> {
Expand Down Expand Up @@ -835,6 +879,7 @@ impl From<f64> for PROPVARIANT {
}
}

#[cfg(windows)]
impl TryFrom<&VARIANT> for f64 {
type Error = Error;
fn try_from(from: &VARIANT) -> Result<Self> {
Expand All @@ -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<Self> {
Expand Down
4 changes: 4 additions & 0 deletions crates/libs/windows/src/Windows/Win32/System/Com/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7664,6 +7664,7 @@ pub type LPEXCEPFINO_DEFERRED_FILLIN = Option<unsafe extern "system" fn(pexcepin
pub type LPFNCANUNLOADNOW = Option<unsafe extern "system" fn() -> windows_core::HRESULT>;
pub type LPFNGETCLASSOBJECT = Option<unsafe extern "system" fn(param0: *const windows_core::GUID, param1: *const windows_core::GUID, param2: *mut *mut core::ffi::c_void) -> windows_core::HRESULT>;
pub type PFNCONTEXTCALL = Option<unsafe extern "system" fn(pparam: *mut ComCallData) -> windows_core::HRESULT>;
#[cfg(windows)]
impl From<IDispatch> for windows_core::VARIANT {
fn from(value: IDispatch) -> Self {
unsafe {
Expand All @@ -7676,6 +7677,7 @@ impl From<IDispatch> for windows_core::VARIANT {
}
}

#[cfg(windows)]
impl From<IDispatch> for windows_core::PROPVARIANT {
fn from(value: IDispatch) -> Self {
unsafe {
Expand All @@ -7688,6 +7690,7 @@ impl From<IDispatch> 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<Self> {
Expand All @@ -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<Self> {
Expand Down

0 comments on commit 1f0bc86

Please sign in to comment.