Poperton
Poperton

Reputation: 2127

How to create a C++ object from Rust?

I know that a Box<SomeRustStruct> can be interpreted as a raw pointer SomeRustStruct* in C++, when the struct has a C representation.

How to do the converse? I only found some usages of bindgen.

How do I create and hold a pointer to a C++ object in Rust?

Upvotes: 7

Views: 846

Answers (1)

Masklinn
Masklinn

Reputation: 42227

The same way you create and hold a pointer to a C object in Rust: the C++ type would be represented by an opaque struct S on the Rust side, and the pointer is an *mut S.

You can also declare the type as a non-opaque type (but beware issues of layout in that case) if you need to perform direct field access on the Rust side but don't (want to) have accessor functions.

Upvotes: 2

Related Questions