rust3ds / ctru-rs

Rust wrapper for libctru
https://rust3ds.github.io/ctru-rs/
Other
116 stars 17 forks source link

Generate smaller enum types #180

Closed ian-h-chamberlain closed 3 months ago

ian-h-chamberlain commented 3 months ago

Closes #166 I guess?

Related to https://github.com/rust3ds/ctru-rs/pull/178#pullrequestreview-2013120422 and some of the changes in #162

By passing -fshort-enums to clang, we can get the same smaller types that gcc generates for some of these enums.

This also makes sure the layout tests have the same smaller sizes, so the expected sizes now match the actual sizes of the same C structs.


One thing this doesn't do is truly check that GCC is generating the same everything... ideally, I think we'd want to link against a C lib that had a bunch of APIs like this or something:

size_t sizeof_errorType() { return sizeof(errorType); }
size_t alignof_errorType() { return alignof(errorType); }
size_t offsetof_errorType() { return offsetof(errorConf, type); }

and then a Rust test like

extern "C" {
    fn sizeof_errorType() -> libc::size_t;
    fn aligneof_errorType() -> libc::size_t;
    fn offsetof_errorType() -> libc::size_t;
}

#[test]
fn layouts() {
    assert_eq!(mem::size_of<errorType>(), sizeof_errorType());
    assert_eq!(mem::align_of<errorType>(), alignof_errorType());
    assert_eq!(mem::offset_of!(errorConf, type_), offsetof_errorType());
}

But without bindgen doing something like this for us, this seems like a tricky thing to write unless we just do it manually. So for now I think I'll kinda ignore that and leave it as a future enhancement if we really need it.

FenrirWolf commented 3 months ago

If anything the scary/weird part is that gcc seems to use short enums by default for arm-none-eabi while clang doesn't. You'd think they would agree on those settings, but at least we can do it manually this way. We'll probably want a similar PR for citro3d-rs too