7

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Could you explain how to return the new object in C++ to Rust? Some casting would be needed for the opaque types, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.