cxx icon indicating copy to clipboard operation
cxx copied to clipboard

How to check that Rust type size is equal to C++ type size.

Open noy1993 opened this issue 1 year ago • 0 comments

I have a question: If the size of types in C++ and Rust are different, the code might still compile, but it would be very unsafe. Is there a way to make the compiler throw an error when the sizes of C++ and Rust types do not match?

For example, with the following code, a Rust Foo struct could be directly transmuted into a C++ Foo.

rust code:

struct Foo{
    v:f64
    v2:f64
}

unsafe impl ExternType for Foo {
    type Id = cxx::type_id!("Foo");
    type Kind = cxx::kind::Trivial;
}

c++ code:

class Foo{
public:
    double v;
};

noy1993 avatar Aug 31 '24 07:08 noy1993