rust-mobile / ndk

Rust bindings to the Android NDK
Apache License 2.0
1.11k stars 110 forks source link

ndk: Mark all enums as `non_exhaustive` and fix `repr` types #459

Closed MarijnS95 closed 7 months ago

MarijnS95 commented 8 months ago

Depends on #458

We're currently in a painful situation, both when it comes to correctness of the wrapper functions that take or return enum values, as well as convenience and ability to upgrade. This is a summation of issues, and reasons why external crates like android-activity are copy-pasting and fixing up our implementation:

To solve this we have to make the following changes to every pub enum with custom discriminants in the NDK crate:

TODO

In a followup PR missing constants will be added to various enums.

MarijnS95 commented 7 months ago

Nice, this must have been quite a chore to go through and update - kudos!

Thanks! Besides reviewing the changes here, I hope you've also cross-checked that I didn't miss out on some enums?

Are you okay with the proposed idea of both having non_exhaustive and a hidden __Unknown?

I'll process the review ASAP and get this in to unblock many other changes, many thanks for the review!

rib commented 7 months ago

I hope you've also cross-checked that I didn't miss out on some enums?

I didn't, but can take a pass to cross check now...

rib commented 7 months ago

Going through cross checking all enums, it could be good go add non_exhaustive to InputEvent potentially.

At least in android-activity I defined InputEvent as non_exhaustive considering also exposing Text/IME events but maybe for the ndk API it's ok to just assume the types of InputQueue events won't get extended?

rib commented 7 months ago

Are you okay with the proposed idea of both having non_exhaustive and a hidden __Unknown?

Yeah I think it makes sense. This is at least what I settled on with android-activity after trying to ponder the trade offs. It's maybe not the perfect solution - ideally the compiler could give us a true repr(i32) enum but it seems like a pretty decent trade off for allowing extension of enums via Android updates.

rib commented 7 months ago

I hope you've also cross-checked that I didn't miss out on some enums?

I didn't, but can take a pass to cross check now...

okey, I've taken a pass through all source for the ndk crate to try and find any enums missed and left a few comments above and haven't found anything else.

MarijnS95 commented 7 months ago

Going through cross checking all enums, it could be good go add non_exhaustive to InputEvent potentially.

At least in android-activity I defined InputEvent as non_exhaustive considering also exposing Text/IME events but maybe for the ndk API it's ok to just assume the types of InputQueue events won't get extended?

Good point. I ended up glossing over this but there are definitely more input types, though currently none has a type and specific functions. Let's mark this as non_exhaustive.

pub const AINPUT_EVENT_TYPE_KEY: _bindgen_ty_19 = 1;
pub const AINPUT_EVENT_TYPE_MOTION: _bindgen_ty_19 = 2;
pub const AINPUT_EVENT_TYPE_FOCUS: _bindgen_ty_19 = 3;
pub const AINPUT_EVENT_TYPE_CAPTURE: _bindgen_ty_19 = 4;
pub const AINPUT_EVENT_TYPE_DRAG: _bindgen_ty_19 = 5;
pub const AINPUT_EVENT_TYPE_TOUCH_MODE: _bindgen_ty_19 = 6;
MarijnS95 commented 7 months ago

It probably makes sense to add an __Unknown variant to FamilyVariant I think

Whoops, looks like I made an exception for an input-only enum here, to encourage NDK updates when needing to pass new values. Didn't do that anywhere else I think, so let's add it.

MarijnS95 commented 7 months ago

Thanks a lot @rib! I know it's a big ask to go through all enums, it's quite a bit to process. But glad to have it resolved in the end.

Who knows, once I import the new values, if we can just replace the enums in android-activity with these again.