Skip to content

Commit

Permalink
Harden generic type parameter binding (#2791)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Jan 15, 2024
1 parent 78fbe6a commit 0564375
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl ::core::ops::Not for BOOL {
}
}
impl ::windows_core::IntoParam<BOOL> for bool {
fn into_param(self) -> ::windows_core::Param<BOOL> {
unsafe fn into_param(self) -> ::windows_core::Param<BOOL> {
::windows_core::Param::Owned(self.into())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl ::core::ops::Not for BOOLEAN {
}
}
impl ::windows_core::IntoParam<BOOLEAN> for bool {
fn into_param(self) -> ::windows_core::Param<BOOLEAN> {
unsafe fn into_param(self) -> ::windows_core::Param<BOOLEAN> {
::windows_core::Param::Owned(self.into())
}
}
28 changes: 13 additions & 15 deletions crates/libs/core/src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ pub trait IntoParam<T: TypeKind, C = <T as TypeKind>::TypeKind>: Sized
where
T: Type<T>,
{
fn into_param(self) -> Param<T>;
unsafe fn into_param(self) -> Param<T>;
}

impl<T> IntoParam<T> for Option<&T>
where
T: Type<T>,
{
fn into_param(self) -> Param<T> {
unsafe fn into_param(self) -> Param<T> {
Param::Borrowed(match self {
Some(item) => item.abi(),
None => unsafe { std::mem::zeroed() },
Some(item) => std::mem::transmute_copy(item),
None => std::mem::zeroed(),
})
}
}
Expand All @@ -50,13 +50,11 @@ where
U: Interface,
U: CanInto<T>,
{
fn into_param(self) -> Param<T> {
unsafe {
if U::QUERY {
self.cast().map_or(Param::Borrowed(std::mem::zeroed()), |ok| Param::Owned(ok))
} else {
Param::Borrowed(std::mem::transmute_copy(self))
}
unsafe fn into_param(self) -> Param<T> {
if U::QUERY {
self.cast().map_or(Param::Borrowed(std::mem::zeroed()), |ok| Param::Owned(ok))
} else {
Param::Borrowed(std::mem::transmute_copy(self))
}
}
}
Expand All @@ -65,8 +63,8 @@ impl<T> IntoParam<T, ValueType> for &T
where
T: TypeKind<TypeKind = ValueType> + Clone,
{
fn into_param(self) -> Param<T> {
Param::Borrowed(self.abi())
unsafe fn into_param(self) -> Param<T> {
Param::Borrowed(std::mem::transmute_copy(self))
}
}

Expand All @@ -76,7 +74,7 @@ where
U: TypeKind<TypeKind = CopyType> + Clone,
U: CanInto<T>,
{
fn into_param(self) -> Param<T> {
Param::Owned(unsafe { std::mem::transmute_copy(&self) })
unsafe fn into_param(self) -> Param<T> {
Param::Owned(std::mem::transmute_copy(&self))
}
}
2 changes: 1 addition & 1 deletion crates/libs/core/src/strings/hstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ impl From<HSTRING> for std::ffi::OsString {
}

impl IntoParam<PCWSTR> for &HSTRING {
fn into_param(self) -> Param<PCWSTR> {
unsafe fn into_param(self) -> Param<PCWSTR> {
Param::Owned(PCWSTR(self.as_ptr()))
}
}
Expand Down
8 changes: 2 additions & 6 deletions crates/libs/core/src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ pub trait Type<T: TypeKind, C = <T as TypeKind>::TypeKind>: TypeKind + Sized + C
type Abi;
type Default;

fn abi(&self) -> Self::Abi {
unsafe { std::mem::transmute_copy(self) }
}

/// # Safety
unsafe fn from_abi(abi: Self::Abi) -> Result<Self>;
fn from_default(default: &Self::Default) -> Result<Self>;
Expand Down Expand Up @@ -55,7 +51,7 @@ where
type Abi = std::mem::MaybeUninit<Self>;
type Default = Self;

unsafe fn from_abi(abi: std::mem::MaybeUninit<Self>) -> Result<Self> {
unsafe fn from_abi(abi: Self::Abi) -> Result<Self> {
Ok(abi.assume_init())
}

Expand All @@ -71,7 +67,7 @@ where
type Abi = Self;
type Default = Self;

unsafe fn from_abi(abi: Self) -> Result<Self> {
unsafe fn from_abi(abi: Self::Abi) -> Result<Self> {
Ok(abi)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/libs/windows/src/Windows/Win32/Foundation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11572,7 +11572,7 @@ impl ::core::ops::Not for BOOL {
}
}
impl ::windows_core::IntoParam<BOOL> for bool {
fn into_param(self) -> ::windows_core::Param<BOOL> {
unsafe fn into_param(self) -> ::windows_core::Param<BOOL> {
::windows_core::Param::Owned(self.into())
}
}
Expand Down Expand Up @@ -11645,7 +11645,7 @@ impl ::core::ops::Not for BOOLEAN {
}
}
impl ::windows_core::IntoParam<BOOLEAN> for bool {
fn into_param(self) -> ::windows_core::Param<BOOLEAN> {
unsafe fn into_param(self) -> ::windows_core::Param<BOOLEAN> {
::windows_core::Param::Owned(self.into())
}
}
Expand Down
15 changes: 15 additions & 0 deletions crates/tests/implement/tests/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,24 @@ fn test_2759() -> Result<()> {
v.Append(&uri)?;
let uri = Uri::CreateUri(h!("https://microsoft.com/"))?;
v.Append(&uri)?;
v.Append(&uri.cast::<IStringable>()?)?;

assert_eq!(&v.GetAt(0)?.ToString()?, h!("https://github.com/"));
assert_eq!(&v.GetAt(1)?.ToString()?, h!("https://microsoft.com/"));

Ok(())
}

#[test]
fn test_into_param() -> Result<()> {
let v: IVector<i32> = Vector::new(vec![]).into();
v.Append(1)?;
v.Append(Some(&2))?;
v.Append(None)?;

assert_eq!(v.GetAt(0)?, 1);
assert_eq!(v.GetAt(1)?, 2);
assert_eq!(v.GetAt(2)?, 0);

Ok(())
}

0 comments on commit 0564375

Please sign in to comment.