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

Implement runtime IID calculation for parametric interfaces via const fn #76

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ exclude = ["Generator/**"]

[dependencies]
winapi = { version = "0.3", features = ["winnt", "combaseapi", "oleauto", "roapi", "roerrorapi", "hstring", "winstring", "winerror", "restrictederrorinfo"] }
uuid = { version = "0.7", features = ["v5"] }

[features]
nightly = []
Expand Down
2 changes: 0 additions & 2 deletions Generator/ParametricInterfaceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace Generator
{
public class ParametricInterfaceManager
{
private static Guid Namespace = new Guid("11F47AD5-7B73-42C0-ABAE-878B1E16ADEE");

private static IEnumerable<Type> baseTypes = new List<Type>
{
//typeof(void),
Expand Down
5 changes: 3 additions & 2 deletions Generator/Types/ClassDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,16 @@ public override void Emit()
{
var dependsOnAssemblies = new List<string>(ForeignAssemblyDependencies.GroupBy(t => t.Module.Assembly.Name.Name).Select(g => g.Key));
var features = new FeatureConditions(dependsOnAssemblies);
string fullname = '"' + Type.FullName + '"';

Module.Append($@"
{ features.GetAttribute() }RT_CLASS!{{class { DefinitionName }: { aliasedType }}}");
{ features.GetAttribute() }RT_CLASS!{{class { DefinitionName }: { aliasedType } [{ fullname }]}}");
if (!features.IsEmpty)
{
// if the aliased type is from a different assembly that is not included, just use IInspectable instead
// otherwise types depending on this class would transitively depend on the aliased type
Module.Append($@"
{ features.GetInvertedAttribute() }RT_CLASS!{{class { DefinitionName }: IInspectable}}");
{ features.GetInvertedAttribute() }RT_CLASS!{{class { DefinitionName }: IInspectable [{ fullname }]}}");
}

foreach (var factory in factories.OrderBy(f => f))
Expand Down
3 changes: 2 additions & 1 deletion Generator/Types/EnumDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public override void CollectDependencies()

public override void Emit()
{
string fullname = '"' + Type.FullName + '"';
Module.Append($@"
RT_ENUM! {{ enum { DefinitionName }: { UnderlyingTypeName } {{
RT_ENUM! {{ enum { DefinitionName }: { UnderlyingTypeName } [{ fullname }] {{
{ String.Join(", ", Type.Fields.Where(f => f.Name != "value__").Select(f => $"{ NameHelpers.PreventKeywords(f.Name) } ({ Type.Name }_{ f.Name }) = { f.Constant }")) },
}}}}");
}
Expand Down
3 changes: 2 additions & 1 deletion Generator/Types/StructDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public override void CollectDependencies()

public override void Emit()
{
string fullname = '"' + Type.FullName + '"';
// TODO: derive(Eq) whenever possible?
Module.Append($@"
RT_STRUCT! {{ struct { DefinitionName } {{
RT_STRUCT! {{ struct { DefinitionName } [{ fullname }] {{
{ string.Join(", ", fields) }{ (fields.Any() ? "," : "") }
}}}}");
}
Expand Down
8 changes: 4 additions & 4 deletions src/cominterfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ pub trait ComInterface {
pub trait ComIid {
// TODO: use associated constant once that is stable
//const IID: REFIID;
fn iid() -> &'static Guid;
fn iid() -> Guid;
}

// extend some definitions from winapi (re-export existing types where possible!)
DEFINE_IID!(IID_IUnknown, 0x00000000, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

/// Re-export from WinAPI crate
pub type IUnknown = ::w::um::unknwnbase::IUnknown;
impl ComIid for IUnknown { #[inline] fn iid() -> &'static Guid { &IID_IUnknown } }
impl ComIid for IUnknown { #[inline] fn iid() -> Guid { *IID_IUnknown } }
impl ComInterface for IUnknown { type Vtbl = IUnknownVtbl; }

DEFINE_IID!(IID_IRestrictedErrorInfo, 0x82BA7092, 0x4C88, 0x427D, 0xA7, 0xBC, 0x16, 0xDD, 0x93, 0xFE, 0xB6, 0x7E);

/// Re-export from WinAPI crate
pub type IRestrictedErrorInfo = ::w::um::restrictederrorinfo::IRestrictedErrorInfo;
pub type IRestrictedErrorInfoVtbl = ::w::um::restrictederrorinfo::IRestrictedErrorInfoVtbl;
impl ComIid for IRestrictedErrorInfo { #[inline] fn iid() -> &'static Guid { &IID_IRestrictedErrorInfo } }
impl ComIid for IRestrictedErrorInfo { #[inline] fn iid() -> Guid { *IID_IRestrictedErrorInfo } }
impl ComInterface for IRestrictedErrorInfo { type Vtbl = IRestrictedErrorInfoVtbl; }

DEFINE_IID!(IID_IAgileObject, 0x94EA2B94, 0xE9CC, 0x49E0, 0xC0, 0xFF, 0xEE, 0x64, 0xCA, 0x8F, 0x5B, 0x90);
Expand All @@ -54,5 +54,5 @@ impl ::std::ops::DerefMut for IAgileObject {
unsafe { ::std::mem::transmute(self) }
}
}
impl ComIid for IAgileObject { #[inline] fn iid() -> &'static Guid { &IID_IAgileObject } }
impl ComIid for IAgileObject { #[inline] fn iid() -> Guid { *IID_IAgileObject } }
impl ComInterface for IAgileObject { type Vtbl = IUnknownVtbl; }
4 changes: 2 additions & 2 deletions src/comptr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::{Deref, DerefMut};
use std::fmt;
use std::ptr;
use ::{ComIid, ComInterface, RtInterface, RtClassInterface, IInspectable, Guid};
use ::{ComIid, ComInterface, RtInterface, RtClassInterface, IInspectable};

use w::shared::ntdef::VOID;
use w::shared::minwindef::LPVOID;
Expand All @@ -24,7 +24,7 @@ impl<T> fmt::Pointer for ComPtr<T> {
// This is a helper method that is not exposed publically by the library
#[inline]
pub fn query_interface<T, Target>(interface: &T) -> Option<ComPtr<Target>> where Target: ComIid, T: ComInterface {
let iid: &'static Guid = Target::iid();
let iid = Target::iid();
let as_unknown = unsafe { &mut *(interface as *const T as *mut T as *mut IUnknown) };
let mut res = ptr::null_mut();
unsafe {
Expand Down
17 changes: 17 additions & 0 deletions src/guid.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
extern crate uuid;

use std::{fmt, cmp, mem};

use w::shared::guiddef::GUID;
Expand Down Expand Up @@ -44,6 +46,21 @@ impl cmp::PartialEq<Guid> for Guid {

impl cmp::Eq for Guid {}

pub(crate) fn format_for_iid_descriptor(buffer: &mut String, guid: &Guid) { // should be const fn
buffer.push_str(&format!("{:08x}-{:04x}-{:04x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
guid.Data1, guid.Data2, guid.Data3,
guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3],
guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]));
}

pub(crate) fn iid_from_descriptor(descriptor: &str) -> Guid {
use self::uuid::Uuid;
let namespace = Uuid::parse_str("11F47AD5-7B73-42C0-ABAE-878B1E16ADEE").expect("invalid IID namespace UUID");
let iid = Uuid::new_v5(&namespace, descriptor.as_bytes());
let (p1, p2, p3, p4) = iid.as_fields();
Guid { Data1: p1, Data2: p2, Data3: p3, Data4: *p4 }
}

#[cfg(test)]
mod tests {
extern crate test;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub use rt::{RtInterface, RtClassInterface, RtNamedClass, RtValueType, RtType, R
RtDefaultConstructible, IInspectable, IInspectableVtbl, IActivationFactory,
IMemoryBufferByteAccess, Char, RuntimeContext, IteratorAdaptor};
pub use rt::async::{RtAsyncAction, RtAsyncOperation};
pub(crate) use rt::RtIidDescriptor;

mod result;
pub use result::{Result, Error, HRESULT};
Expand Down
Loading