Idea: Automatic marker traits for repr(C) and friends

I’m wondering about the possibility of constraining types by their representation. My use case is that I have a data structure in C that is a list of pointers to objects of the same type. I want to represent that on the Rust side, and make creating APIs that return this data structure ergonomic. For that, I would like to constrain parameters to constructor functions in such a way that I can make sure that they must be repr©, to avoid accidentally passing repr(Rust) types to C (which might accidentally work in testing, but break arbitrarily later).

One idea I had was adding an unsafe trait that someone could implement to specify that the type in question is using repr©. The obvious extension of that would be automatically adding traits for this, possibly including alignment. Has something like that already been proposed/discussed, or is it problematic for some reason I’m not spotting?

4 Likes

I would like this.

I suggested something related but different in Automatic marker trait for unconditionally valid `#repr(C)` types, but I didn’t get much feedback, so I suppose not many people are invested in this kind of safety crutches.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.