Skip to content

Commit

Permalink
Fix ToImpl (#1748)
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev authored May 10, 2022
1 parent d60b526 commit d0c856b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
16 changes: 11 additions & 5 deletions crates/libs/implement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
unsafe { ::core::mem::transmute_copy(&vtable_ptr) }
}
}
impl <#constraints> ::windows::core::ToImpl<#interface_ident> for #original_ident::<#(#generics,)*> {
unsafe fn to_impl(interface: &#interface_ident) -> &mut Self {
let this: ::windows::core::RawPtr = ::windows::core::Interface::as_raw(interface);
let this = (this as *mut ::windows::core::RawPtr).sub(2 + #offset) as *mut #impl_ident::<#(#generics,)*>;
&mut (*this).this
impl <#constraints> ::windows::core::AsImpl<#original_ident::<#(#generics,)*>> for #interface_ident {
fn as_impl(&self) -> &#original_ident::<#(#generics,)*> {
let this = ::windows::core::Interface::as_raw(self);
// SAFETY: the offset is guranteed to be in bounds, and the implementation struct
// is guaranteed to live at least as long as `self`.
unsafe {
// Subtract away the vtable offset plus 2 (for the `base` and `identity` fields) to get
// to the impl struct which contains that original implementation type.
let this = (this as *mut ::windows::core::RawPtr).sub(2 + #offset) as *mut #impl_ident::<#(#generics,)*>;
&(*this).this
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions crates/libs/windows/src/core/as_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/// A trait for retrieving the implementation behind a COM or WinRT interface.
///
/// This trait is automatically implemented when using the `implement` macro.
pub trait AsImpl<T> {
fn as_impl(&self) -> &T;
}
6 changes: 3 additions & 3 deletions crates/libs/windows/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod abi;
mod agile_reference;
mod array;
mod as_impl;
pub(crate) mod bindings;
mod compose;
mod delay_load;
Expand All @@ -24,7 +25,6 @@ mod ref_count;
mod runtime_name;
mod runtime_type;
mod sha1;
mod to_impl;
mod unknown;
mod waiter;
mod weak;
Expand All @@ -35,6 +35,8 @@ pub use abi::*;
pub use agile_reference::*;
pub use array::*;
#[doc(hidden)]
pub use as_impl::*;
#[doc(hidden)]
pub use compose::*;
pub(crate) use delay_load::*;
pub use error::*;
Expand Down Expand Up @@ -65,8 +67,6 @@ pub use runtime_name::*;
pub use runtime_type::*;
#[doc(hidden)]
pub use sha1::*;
#[doc(hidden)]
pub use to_impl::*;
pub use unknown::*;
#[doc(hidden)]
pub use waiter::*;
Expand Down
12 changes: 0 additions & 12 deletions crates/libs/windows/src/core/to_impl.rs

This file was deleted.

2 changes: 1 addition & 1 deletion crates/tests/nightly_data_object/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn test() -> Result<()> {
let _ = d.EnumFormatEtc(0);
d.DAdvise(core::ptr::null_mut(), 0, None)?;

let i = Test::to_impl(&d).0.get();
let i = d.as_impl().0.get();
assert!((*i).GetData);
assert!((*i).GetDataHere);
assert!((*i).QueryGetData);
Expand Down
6 changes: 3 additions & 3 deletions crates/tests/nightly_implement/tests/into_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl<T: RuntimeType + 'static> IIterator_Impl<T> for Iterator<T> {
fn Current(&self) -> Result<T> {
unsafe {
let this = self.0.get();
let owner = Iterable::to_impl(&(*this).0);
let owner = (*this).0.as_impl();

if owner.0.len() > (*this).1 {
Ok(owner.0[(*this).1].clone())
Expand All @@ -25,15 +25,15 @@ impl<T: RuntimeType + 'static> IIterator_Impl<T> for Iterator<T> {
fn HasCurrent(&self) -> Result<bool> {
unsafe {
let this = self.0.get();
let owner = Iterable::to_impl(&(*this).0);
let owner = (*this).0.as_impl();
Ok(owner.0.len() > (*this).1)
}
}

fn MoveNext(&self) -> Result<bool> {
unsafe {
let this = self.0.get();
let owner = Iterable::to_impl(&(*this).0);
let owner = (*this).0.as_impl();
(*this).1 += 1;
Ok(owner.0.len() > (*this).1)
}
Expand Down

0 comments on commit d0c856b

Please sign in to comment.