Skip to content

Commit

Permalink
Simplify From impls from implement macro
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev committed May 11, 2022
1 parent 2eddbfa commit 94d335e
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions crates/libs/implement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
impl <#constraints> ::core::convert::From<#original_ident::<#(#generics,)*>> for #interface_ident {
fn from(this: #original_ident::<#(#generics,)*>) -> Self {
let this = #impl_ident::<#(#generics,)*>::new(this);
let mut this = ::std::boxed::Box::new(this);
let vtable_ptr = &mut this.vtables.#offset as *mut *const <#interface_ident as ::windows::core::Interface>::Vtable;
let _ = ::std::boxed::Box::leak(this);
unsafe { ::core::mem::transmute_copy(&vtable_ptr) }
let mut this = ::core::mem::ManuallyDrop::new(::std::boxed::Box::new(this));
let vtable_ptr = &this.vtables.#offset;
// SAFETY: interfaces are in-memory equivalent to pointers to their vtables.
unsafe { ::core::mem::transmute(vtable_ptr) }
}
}
impl <#constraints> ::windows::core::AsImpl<#original_ident::<#(#generics,)*>> for #interface_ident {
Expand Down Expand Up @@ -163,23 +163,19 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
}
impl <#constraints> ::core::convert::From<#original_ident::<#(#generics,)*>> for ::windows::core::IUnknown {
fn from(this: #original_ident::<#(#generics,)*>) -> Self {
let this = #impl_ident::<#(#generics,)*>::new(this);
let boxed = ::core::mem::ManuallyDrop::new(::std::boxed::Box::new(this));
unsafe {
let this = #impl_ident::<#(#generics,)*>::new(this);
let ptr = ::std::boxed::Box::into_raw(::std::boxed::Box::new(this));
::core::mem::transmute_copy(&::core::ptr::NonNull::new_unchecked(
&mut (*ptr).identity as *mut _ as _
))
::core::mem::transmute(&boxed.identity)
}
}
}
impl <#constraints> ::core::convert::From<#original_ident::<#(#generics,)*>> for ::windows::core::IInspectable {
fn from(this: #original_ident::<#(#generics,)*>) -> Self {
let this = #impl_ident::<#(#generics,)*>::new(this);
let boxed = ::core::mem::ManuallyDrop::new(::std::boxed::Box::new(this));
unsafe {
let this = #impl_ident::<#(#generics,)*>::new(this);
let ptr = ::std::boxed::Box::into_raw(::std::boxed::Box::new(this));
::core::mem::transmute_copy(&::core::ptr::NonNull::new_unchecked(
&mut (*ptr).identity as *mut _ as _
))
::core::mem::transmute(&boxed.identity)
}
}
}
Expand Down

0 comments on commit 94d335e

Please sign in to comment.