Skip to content

Commit

Permalink
Avoid windows crate dependency in implement and interface macros (
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored Mar 7, 2024
1 parent 1f40da8 commit 9cdea4e
Show file tree
Hide file tree
Showing 27 changed files with 125 additions and 75 deletions.
1 change: 1 addition & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ jobs:
cargo clippy -p test_helpers &&
cargo clippy -p test_implement &&
cargo clippy -p test_interface &&
cargo clippy -p test_interface_core &&
cargo clippy -p test_interop &&
cargo clippy -p test_lib &&
cargo clippy -p test_literals &&
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,15 @@ jobs:
cargo test -p test_dispatch &&
cargo test -p test_does_not_return &&
cargo test -p test_enums &&
cargo clean &&
cargo test -p test_error &&
cargo clean &&
cargo test -p test_event &&
cargo test -p test_extensions &&
cargo test -p test_handles &&
cargo test -p test_helpers &&
cargo test -p test_implement &&
cargo test -p test_interface &&
cargo test -p test_interface_core &&
cargo test -p test_interop &&
cargo test -p test_lib &&
cargo test -p test_literals &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[::windows_implement::implement(IIterable<T>)]
#[windows_core::implement(IIterable<T>)]
struct StockIterable<T>
where
T: windows_core::RuntimeType + 'static,
Expand All @@ -25,7 +25,7 @@ where
}
}

#[::windows_implement::implement(IIterator<T>)]
#[windows_core::implement(IIterator<T>)]
struct StockIterator<T>
where
T: windows_core::RuntimeType + 'static,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[::windows_implement::implement(IMapView<K, V>, IIterable<IKeyValuePair<K, V>>)]
#[windows_core::implement(IMapView<K, V>, IIterable<IKeyValuePair<K, V>>)]
struct StockMapView<K, V>
where
K: windows_core::RuntimeType + 'static,
Expand Down Expand Up @@ -60,7 +60,7 @@ where
}
}

#[::windows_implement::implement(IIterator<IKeyValuePair<K, V>>)]
#[windows_core::implement(IIterator<IKeyValuePair<K, V>>)]
struct StockMapViewIterator<'a, K, V>
where
K: windows_core::RuntimeType + 'static,
Expand Down Expand Up @@ -129,7 +129,7 @@ where
}
}

#[::windows_implement::implement(IKeyValuePair<K, V>)]
#[windows_core::implement(IKeyValuePair<K, V>)]
struct StockKeyValuePair<K, V>
where
K: windows_core::RuntimeType + 'static,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[::windows_implement::implement(IVectorView<T>, IIterable<T>)]
#[windows_core::implement(IVectorView<T>, IIterable<T>)]
struct StockVectorView<T>
where
T: windows_core::RuntimeType + 'static,
Expand Down Expand Up @@ -61,7 +61,7 @@ where
}
}

#[::windows_implement::implement(IIterator<T>)]
#[windows_core::implement(IIterator<T>)]
struct StockVectorViewIterator<T>
where
T: windows_core::RuntimeType + 'static,
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ path = "../targets"
version = "0.1.0"
path = "../result"

[features]
default = []
implement = []
[dependencies]
windows-implement = { path = "../implement", version = "0.53.0" }
windows-interface = { path = "../interface", version = "0.53.0" }
1 change: 0 additions & 1 deletion crates/libs/core/src/inspectable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ impl RuntimeType for IInspectable {

impl RuntimeName for IInspectable {}

#[cfg(feature = "implement")]
impl IInspectable_Vtbl {
pub const fn new<Identity: IUnknownImpl, Name: RuntimeName, const OFFSET: isize>() -> Self {
unsafe extern "system" fn GetIids(_: *mut std::ffi::c_void, count: *mut u32, values: *mut *mut GUID) -> HRESULT {
Expand Down
2 changes: 2 additions & 0 deletions crates/libs/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub use strings::*;
pub use unknown::*;
pub use variant::*;
pub use weak::*;
pub use windows_implement::implement;
pub use windows_interface::interface;
pub use windows_result::*;

/// Attempts to load the factory object for the given WinRT class.
Expand Down
1 change: 0 additions & 1 deletion crates/libs/core/src/unknown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ pub trait IUnknownImpl {
unsafe fn GetTrustLevel(&self, value: *mut i32) -> HRESULT;
}

#[cfg(feature = "implement")]
impl IUnknown_Vtbl {
pub const fn new<T: IUnknownImpl, const OFFSET: isize>() -> Self {
unsafe extern "system" fn QueryInterface<T: IUnknownImpl, const OFFSET: isize>(this: *mut std::ffi::c_void, iid: *const GUID, interface: *mut *mut std::ffi::c_void) -> HRESULT {
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/implement/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "windows-implement"
version = "0.53.0"
authors = ["Microsoft"]
edition = "2021"
rust-version = "1.64"
rust-version = "1.62"
license = "MIT OR Apache-2.0"
description = "The implement macro for the windows crate"
repository = "https://github.com/microsoft/windows-rs"
Expand Down
50 changes: 25 additions & 25 deletions crates/libs/implement/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
let identity_type = if let Some(first) = attributes.implement.first() {
first.to_ident()
} else {
quote! { ::windows::core::IInspectable }
quote! { ::windows_core::IInspectable }
};

let original_type2 = original_type.clone();
Expand Down Expand Up @@ -97,11 +97,11 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
unsafe { ::core::mem::transmute(vtable_ptr) }
}
}
impl #generics ::windows::core::AsImpl<#original_ident::#generics> for #interface_ident where #constraints {
impl #generics ::windows_core::AsImpl<#original_ident::#generics> for #interface_ident where #constraints {
// SAFETY: the offset is guranteed to be in bounds, and the implementation struct
// is guaranteed to live at least as long as `self`.
unsafe fn as_impl(&self) -> &#original_ident::#generics {
let this = ::windows::core::Interface::as_raw(self);
let this = ::windows_core::Interface::as_raw(self);
// Subtract away the vtable offset plus 1, for the `identity` field, to get
// to the impl struct which contains that original implementation type.
let this = (this as *mut *mut ::core::ffi::c_void).sub(1 + #offset) as *mut #impl_ident::#generics;
Expand All @@ -114,54 +114,54 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
let tokens = quote! {
#[repr(C)]
struct #impl_ident #generics where #constraints {
identity: *const ::windows::core::IInspectable_Vtbl,
identity: *const ::windows_core::IInspectable_Vtbl,
vtables: (#(*const #vtbl_idents,)*),
this: #original_ident::#generics,
count: ::windows::core::imp::WeakRefCount,
count: ::windows_core::imp::WeakRefCount,
}
impl #generics #impl_ident::#generics where #constraints {
const VTABLES: (#(#vtbl_idents2,)*) = (#(#vtable_news,)*);
const IDENTITY: ::windows::core::IInspectable_Vtbl = ::windows::core::IInspectable_Vtbl::new::<Self, #identity_type, 0>();
const IDENTITY: ::windows_core::IInspectable_Vtbl = ::windows_core::IInspectable_Vtbl::new::<Self, #identity_type, 0>();
fn new(this: #original_ident::#generics) -> Self {
Self {
identity: &Self::IDENTITY,
vtables:(#(&Self::VTABLES.#offset,)*),
this,
count: ::windows::core::imp::WeakRefCount::new(),
count: ::windows_core::imp::WeakRefCount::new(),
}
}
}
impl #generics ::windows::core::IUnknownImpl for #impl_ident::#generics where #constraints {
impl #generics ::windows_core::IUnknownImpl for #impl_ident::#generics where #constraints {
type Impl = #original_ident::#generics;
fn get_impl(&self) -> &Self::Impl {
&self.this
}
unsafe fn QueryInterface(&self, iid: *const ::windows::core::GUID, interface: *mut *mut ::core::ffi::c_void) -> ::windows::core::HRESULT {
unsafe fn QueryInterface(&self, iid: *const ::windows_core::GUID, interface: *mut *mut ::core::ffi::c_void) -> ::windows_core::HRESULT {
if iid.is_null() || interface.is_null() {
return ::windows::core::HRESULT(-2147467261); // E_POINTER
return ::windows_core::HRESULT(-2147467261); // E_POINTER
}

let iid = &*iid;

*interface = if iid == &<::windows::core::IUnknown as ::windows::core::Interface>::IID
|| iid == &<::windows::core::IInspectable as ::windows::core::Interface>::IID
|| iid == &<::windows::core::imp::IAgileObject as ::windows::core::Interface>::IID {
*interface = if iid == &<::windows_core::IUnknown as ::windows_core::Interface>::IID
|| iid == &<::windows_core::IInspectable as ::windows_core::Interface>::IID
|| iid == &<::windows_core::imp::IAgileObject as ::windows_core::Interface>::IID {
&self.identity as *const _ as *mut _
} #(#queries)* else {
::core::ptr::null_mut()
};

if !(*interface).is_null() {
self.count.add_ref();
return ::windows::core::HRESULT(0);
return ::windows_core::HRESULT(0);
}

*interface = self.count.query(iid, &self.identity as *const _ as *mut _);

if (*interface).is_null() {
::windows::core::HRESULT(-2147467262) // E_NOINTERFACE
::windows_core::HRESULT(-2147467262) // E_NOINTERFACE
} else {
::windows::core::HRESULT(0)
::windows_core::HRESULT(0)
}
}
fn AddRef(&self) -> u32 {
Expand All @@ -174,12 +174,12 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
}
remaining
}
unsafe fn GetTrustLevel(&self, value: *mut i32) -> ::windows::core::HRESULT {
unsafe fn GetTrustLevel(&self, value: *mut i32) -> ::windows_core::HRESULT {
if value.is_null() {
return ::windows::core::HRESULT(-2147467261); // E_POINTER
return ::windows_core::HRESULT(-2147467261); // E_POINTER
}
*value = #trust_level;
::windows::core::HRESULT(0)
::windows_core::HRESULT(0)
}
}
impl #generics #original_ident::#generics where #constraints {
Expand All @@ -189,14 +189,14 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
///
/// This function can only be safely called if `self` has been heap allocated and pinned using
/// the mechanisms provided by `implement` macro.
unsafe fn cast<I: ::windows::core::Interface>(&self) -> ::windows::core::Result<I> {
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 = ::std::ptr::null_mut();
_ = <#impl_ident::#generics as ::windows::core::IUnknownImpl>::QueryInterface(&*boxed, &I::IID, &mut result);
::windows::core::Type::from_abi(result)
_ = <#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 {
impl #generics ::core::convert::From<#original_ident::#generics> for ::windows_core::IUnknown where #constraints {
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));
Expand All @@ -205,7 +205,7 @@ pub fn implement(attributes: proc_macro::TokenStream, original_type: proc_macro:
}
}
}
impl #generics ::core::convert::From<#original_ident::#generics> for ::windows::core::IInspectable where #constraints {
impl #generics ::core::convert::From<#original_ident::#generics> for ::windows_core::IInspectable where #constraints {
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));
Expand Down Expand Up @@ -237,7 +237,7 @@ impl ImplementType {
fn to_vtbl_ident(&self) -> proc_macro2::TokenStream {
let ident = self.to_ident();
quote! {
<#ident as ::windows::core::Interface>::Vtable
<#ident as ::windows_core::Interface>::Vtable
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "windows-interface"
version = "0.53.0"
authors = ["Microsoft"]
edition = "2021"
rust-version = "1.64"
rust-version = "1.62"
license = "MIT OR Apache-2.0"
description = "The interface macro for the windows crate"
repository = "https://github.com/microsoft/windows-rs"
Expand Down
32 changes: 16 additions & 16 deletions crates/libs/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ impl Interface {
#(#docs)*
#vis struct #name(#parent);
#implementation
unsafe impl ::windows::core::Interface for #name {
unsafe impl ::windows_core::Interface for #name {
type Vtable = #vtable_name;
const IID: ::windows::core::GUID = #guid;
const IID: ::windows_core::GUID = #guid;
}
impl ::windows::core::RuntimeName for #name {}
impl ::windows_core::RuntimeName for #name {}

#com_trait
#vtable
Expand Down Expand Up @@ -132,7 +132,7 @@ impl Interface {
let ret = &m.ret;
quote! {
#vis unsafe fn #name(&self, #(#args),*) #ret {
(::windows::core::Interface::vtable(self).#name)(::windows::core::Interface::as_raw(self), #(#params),*)
(::windows_core::Interface::vtable(self).#name)(::windows_core::Interface::as_raw(self), #(#params),*)
}
}
})
Expand Down Expand Up @@ -211,7 +211,7 @@ impl Interface {
let ret = &m.ret;
if parent_vtable.is_some() {
quote! {
unsafe extern "system" fn #name<Identity: ::windows::core::IUnknownImpl<Impl = Impl>, Impl: #trait_name, const OFFSET: isize>(this: *mut ::core::ffi::c_void, #(#args),*) #ret {
unsafe extern "system" fn #name<Identity: ::windows_core::IUnknownImpl<Impl = Impl>, Impl: #trait_name, const OFFSET: isize>(this: *mut ::core::ffi::c_void, #(#args),*) #ret {
let this = (this as *const *const ()).offset(OFFSET) as *const Identity;
let this = (*this).get_impl();
this.#name(#(#params),*).into()
Expand All @@ -220,7 +220,7 @@ impl Interface {
} else {
quote! {
unsafe extern "system" fn #name<Impl: #trait_name>(this: *mut ::core::ffi::c_void, #(#args),*) #ret {
let this = (this as *mut *mut ::core::ffi::c_void) as *const ::windows::core::ScopedHeap;
let this = (this as *mut *mut ::core::ffi::c_void) as *const ::windows_core::ScopedHeap;
let this = (*this).this as *const Impl;
(*this).#name(#(#params),*).into()
}
Expand Down Expand Up @@ -255,13 +255,13 @@ impl Interface {
#(#vtable_entries)*
}
impl #vtable_name {
pub const fn new<Identity: ::windows::core::IUnknownImpl<Impl = Impl>, Impl: #trait_name, const OFFSET: isize>() -> Self {
pub const fn new<Identity: ::windows_core::IUnknownImpl<Impl = Impl>, Impl: #trait_name, const OFFSET: isize>() -> Self {
#(#functions)*
Self { base__: #parent_vtable::new::<#parent_vtable_generics>(), #(#entries),* }
}

pub fn matches(iid: &windows::core::GUID) -> bool {
iid == &<#name as ::windows::core::Interface>::IID
pub fn matches(iid: &windows_core::GUID) -> bool {
iid == &<#name as ::windows_core::Interface>::IID
}
}
}
Expand All @@ -283,10 +283,10 @@ impl Interface {
const VTABLE: #vtable_name = #vtable_name::new::<T>();
}
impl #name {
fn new<'a, T: #trait_name>(this: &'a T) -> ::windows::core::ScopedInterface<'a, #name> {
let this = ::windows::core::ScopedHeap { vtable: &#implvtbl_name::<T>::VTABLE as *const _ as *const _, this: this as *const _ as *const _ };
fn new<'a, T: #trait_name>(this: &'a T) -> ::windows_core::ScopedInterface<'a, #name> {
let this = ::windows_core::ScopedHeap { vtable: &#implvtbl_name::<T>::VTABLE as *const _ as *const _, this: this as *const _ as *const _ };
let this = ::std::mem::ManuallyDrop::new(::std::boxed::Box::new(this));
unsafe { ::windows::core::ScopedInterface::new(::std::mem::transmute(&this.vtable)) }
unsafe { ::windows_core::ScopedInterface::new(::std::mem::transmute(&this.vtable)) }
}
}
}
Expand All @@ -298,12 +298,12 @@ impl Interface {
let name = &self.name;
let name_string = format!("{name}");
quote! {
impl ::core::convert::From<#name> for ::windows::core::IUnknown {
impl ::core::convert::From<#name> for ::windows_core::IUnknown {
fn from(value: #name) -> Self {
unsafe { ::core::mem::transmute(value) }
}
}
impl ::core::convert::From<&#name> for ::windows::core::IUnknown {
impl ::core::convert::From<&#name> for ::windows_core::IUnknown {
fn from(value: &#name) -> Self {
::core::convert::From::from(::core::clone::Clone::clone(value))
}
Expand Down Expand Up @@ -460,7 +460,7 @@ impl Guid {
let data4_7 = hex_lit(data4_7);
let data4_8 = hex_lit(data4_8);
Ok(quote! {
::windows::core::GUID {
::windows_core::GUID {
data1: #data1,
data2: #data2,
data3: #data3,
Expand All @@ -469,7 +469,7 @@ impl Guid {
})
} else {
Ok(quote! {
::windows::core::GUID::zeroed()
::windows_core::GUID::zeroed()
})
}
}
Expand Down
4 changes: 1 addition & 3 deletions crates/libs/windows/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ rustdoc-args = ["--cfg", "docsrs"]
[dependencies]
windows-core = { path = "../core", version = "0.55.0" }
windows-targets = { path = "../targets", version = "0.52.4" }
windows-implement = { path = "../implement", version = "0.53.0", optional = true }
windows-interface = { path = "../interface", version = "0.53.0", optional = true }

[features]
default = []
docs = []
deprecated = []
implement = ["windows-implement", "windows-interface", "windows-core/implement"]
implement = []
# generated features
AI = ["Foundation"]
AI_MachineLearning = ["AI"]
Expand Down
Loading

0 comments on commit 9cdea4e

Please sign in to comment.