Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove cast from implement macro #3506

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions crates/libs/core/src/com_object.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::imp::Box;
use crate::{AsImpl, IUnknown, IUnknownImpl, Interface, InterfaceRef};
use crate::{IUnknown, IUnknownImpl, Interface, InterfaceRef};
use core::any::Any;
use core::borrow::Borrow;
use core::ops::Deref;
Expand Down Expand Up @@ -245,10 +245,7 @@ impl<T: ComObjectInner> Clone for ComObject<T> {
}
}

impl<T: ComObjectInner> AsRef<T> for ComObject<T>
where
IUnknown: From<T> + AsImpl<T>,
{
impl<T: ComObjectInner> AsRef<T> for ComObject<T> {
#[inline(always)]
fn as_ref(&self) -> &T {
self.get()
Expand Down
17 changes: 0 additions & 17 deletions crates/libs/implement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub fn implement(
original_type: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let attributes = syn::parse_macro_input!(attributes as ImplementAttributes);
let interfaces_len = proc_macro2::Literal::usize_unsuffixed(attributes.implement.len());

let identity_type = if let Some(first) = attributes.implement.first() {
first.to_ident()
Expand Down Expand Up @@ -327,22 +326,6 @@ pub fn implement(
const INNER_OFFSET_IN_POINTERS: usize = #offset_of_this_in_pointers_token;
}

impl #generics #original_ident::#generics where #constraints {
/// Try casting as the provided interface
///
/// # Safety
///
/// This function can only be safely called if `self` has been heap allocated and pinned using
/// the mechanisms provided by `implement` macro.
#[inline(always)]
unsafe fn cast<I: ::windows_core::Interface>(&self) -> ::windows_core::Result<I> {
let boxed = (self as *const _ as *const *mut ::core::ffi::c_void).sub(1 + #interfaces_len) as *mut #impl_ident::#generics;
let mut result = ::core::ptr::null_mut();
_ = <#impl_ident::#generics as ::windows_core::IUnknownImpl>::QueryInterface(&*boxed, &I::IID, &mut result);
::windows_core::Type::from_abi(result)
}
}

impl #generics ::core::convert::From<#original_ident::#generics> for ::windows_core::IUnknown where #constraints {
#[inline(always)]
fn from(this: #original_ident::#generics) -> Self {
Expand Down
6 changes: 2 additions & 4 deletions crates/tests/libs/implement/tests/from_raw_borrowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ impl IServiceProvider_Impl for Borrowed_Impl {
iid: *const GUID,
object: *mut *mut std::ffi::c_void,
) -> Result<()> {
unsafe {
let unknown: IUnknown = self.cast()?;
unknown.query(iid, object).ok()
}
let unknown = self.as_interface::<IUnknown>();
unsafe { unknown.query(iid, object).ok() }
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/tests/libs/implement/tests/into_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ where
<T as Type<T>>::Default: PartialEq,
{
fn First(&self) -> Result<IIterator<T>> {
Ok(Iterator::<T>((unsafe { self.cast()? }, 0).into()).into())
Ok(Iterator::<T>((self.to_interface::<IIterable<T>>(), 0).into()).into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/tests/libs/implement/tests/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ where
self.Size()
}
fn GetView(&self) -> Result<IVectorView<T>> {
unsafe { self.cast() }
Ok(self.to_interface())
}
fn IndexOf(&self, value: Ref<T>, result: &mut u32) -> Result<bool> {
self.IndexOf(value, result)
Expand Down