mystor / rust-cpp

Embed C++ directly inside your rust code!
Apache License 2.0
798 stars 44 forks source link

Introduce new macro to wrap a C++ class: cpp_class! #28

Closed ogoffart closed 6 years ago

ogoffart commented 6 years ago

A macro which allow to wrap C++ class into rust types with and call their destructor and copy constructor appropriatly

Example of use:

cpp_class!(pub struct MyClass, "MyClass");
impl MyClass {
    fn new() -> Self {
        unsafe { cpp!([] -> MyClass as "MyClass" { return MyClass(); }) }
    }
    fn member_function(&self, param : i32) -> i32 {
        unsafe { cpp!([self as "const MyClass*", param as "int"] -> i32 as "int" {
            return self->member_function(param);
        }) }
    }
}
ogoffart commented 6 years ago

@mystor Do you have any feedback to give?

I'm slowly developing a crate that uses a C++ library, and uses this feature and the other ones. Would be nice to have it merged.

ogoffart commented 6 years ago

@mystor ping?

mystor commented 6 years ago

@ogoffart Hey, so sorry for how long this has taken. I'll try to take a look at this in the next few days, but I'm pretty busy with work right now.

ogoffart commented 6 years ago

I made most change, but I did not rename the macro to re-use cpp.

I think it is better to be implicit about the difference of usage of the macro. That makes the code more readable and lets the user find the documentation for the relevant macro more easily. Is this also not a reason why there are no overloaded functions in rust? This is not a strong opinion. What do you think?

mystor commented 6 years ago

Thanks for the changes :-) Looks good now.