mystor / rust-cpp

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

Is it possible to use foreign types? #16

Closed porky11 closed 6 years ago

porky11 commented 7 years ago

Is it, or will it be possible to use types from c++? I didn't find that in the documentation nor in the tests. I'd like something like that:

#[macro_use]
extern crate cpp;

cpp!{{
    #include <glm/glm.hpp> 
}}

fn main() {
    let x = cpp![glm::vec3(1,0,0)];
}
mystor commented 7 years ago

No, this is not currently possible.

It might be possible to do something like this (allowing opaque C++ types to exist on the Rust stack - you'd still need cpp! blocks to call methods on the types) once we get something like impl trait or if I decide that implicit boxing and virtual call overhead is OK.

vadimcn commented 7 years ago

@mystor, btw, why would you need impl Trait for this?

mystor commented 7 years ago

I'm not sure if I would. I had one idea for how to implement it which would require it but I haven't had time to think through the options.

vadimcn commented 7 years ago

I thought you were talking about creating opaque structs of the same size and alignment as their C++ counterparts.

ogoffart commented 6 years ago

This is now possible with cpp_class! from https://github.com/mystor/rust-cpp/pull/28

#[macro_use]
extern crate cpp;

cpp!{{
    #include <glm/glm.hpp> 
}}

cpp_class!(unsafe struct vec3 as "glm::vec3");

fn main() {
    let x = unsafe { cpp!( [] -> vec3 as "glm::vec3" { return glm::vec3(1,0,0)]; }) };
}