contextfree / winrt-rust

Use and (eventually) make Windows Runtime APIs with Rust
Apache License 2.0
142 stars 10 forks source link

Autogenerate delegate (handler) implementations #7

Closed Boddlnagg closed 8 years ago

Boddlnagg commented 8 years ago

The hard part about this is generic parameters: In my current proof-of-concept impl of AsyncOperationCompletedHandler<TResult>, I plug in a dummy type for TResult to get a non-generic constant VTable (because constants can't be generic in Rust – maybe it would be possible somehow with associated consts, but I couldn't get it to work and also we want to be able to build on stable).

This works only as long as the generic parameters only appear indirectly within the signature of Invoke, which is (*mut IAsyncOperation<TResult>, AsyncStatus) -> HRESULT in this case. The binary representation of *mut IAsyncOperation<TResult> is the same no matter what TResult is. However, as soon as generic parameters are used directly, which happens e.g. in EventHandler<T>(object sender, T args), this won't work and we need a truly generic VTable.

One way to solve this would be to dynamically allocate the VTable instead of having a constant pointer. Then it should also be possible to turn the handler function (i.e. the invoke field of AsyncOperationCompletedHandlerImpl etc.) into an unboxed closure. I don't know about performance implications, and maybe there is another (better?) way.

Boddlnagg commented 8 years ago

This is fixed in my branch, using boxed (dynamically allocated) VTables.