Thanks for the pointer!
So now I'm trying to use Rust macros to generate all required COM artifacts
from a single interface definition:
macro_rules! com_interface(
(
$ifc_name:ident
{
$( fn $fn_name:ident ( $( $param:ident : $ptype:ty ),* ) ->
$tres:ty );+
;
}
) =>
(
trait $ifc_name
{
$( fn $fn_name ( $($param : $ptype),* ) -> $tres );+
;
}
struct concat_idents!($ifc_name, _vtable)
{
$( $fn_name : *fn ( this: **concat_idents!($ifc_name, _vtable),
$($param : $ptype),* ) -> $tres ),+
}
)
)
com_interface!(
IInterface
{
fn Foo(a : int, b : int, c : ~str) -> ();
fn Bar(a : int) -> int;
//fn Baz() -> ();
}
)
This is supposed to produce the following code:
trait IInterface
{
fn Foo(a : int, b : int, c : ~str) -> ();
fn Bar(a : int) -> int;
}
struct IInterface_vtable
{
Foo : *fn(this : **IInterface_vtable, a : int, b : int, c : ~str)
-> (),
Bar : *fn(this : **IInterface_vtable, a : int) -> int
}
I am having several problems with this:
1. Apparently this only generates the trait block and silently discards the
struct portion of expansion (according to output of "rustc --pretty
expanded test_macro.rs")
2. I can't figure out the syntax of concat_idents... rustc says:
test_macro.rs:18:22: 18:23 error: expected `{`, `(`, or `;` after struct
name but found `!`
test_macro.rs:18 struct concat_idents!($ifc_name, _vtable)
(comment out the trait block to see this in action).
3. This macro doesn't work with parameter-less functions. Uncommenting
Baz() in the interface definition above produces:
test_macro.rs:29:9: 29:10 error: Local ambiguity: multiple parsing options:
built-in NTs ident ('param') or 1 other options.
test_macro.rs:29 fn Baz() -> ();
4. Even if Baz were parsed correctly, I would probably still have a problem
with expansion because of the extra trailing comma after "this"
parameter. Any ideas how to work around that?
thanks!
On Wed, Jan 2, 2013 at 7:23 PM, Patrick Walton <[email protected]> wrote:
> On 1/2/13 7:09 PM, Vadim wrote:
>
>> Hi,
>> Is there any expectation that Rust's traits would be binary-compatible
>> with C++ vtables? I expect the answer is "no", but just in case...
>>
>
> No, we aren't committing to any stable API.
>
> Reason I'm asking: I am toying with the idea of using Windows COM
>> components from Rust. Has anyone here done anything like that in the
>> past?
>>
>
> For Servo we have bindings to Core Foundation on the Mac, which is a
> COM-like system. We implement the Clone trait and use structs with
> destructors in order to enforce the proper use of reference counting. You
> can find the code here:
>
> https://github.com/mozilla-**servo/rust-core-foundation<https://github.com/mozilla-servo/rust-core-foundation>
>
> Patrick
>
> ______________________________**_________________
> Rust-dev mailing list
> [email protected]
> https://mail.mozilla.org/**listinfo/rust-dev<https://mail.mozilla.org/listinfo/rust-dev>
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev