-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ndk: Mark all enums as
non_exhaustive
and fix repr
types
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: - False mapping of unknown values to NDK `_UNKNOWN` constants. This makes the API lossy as the original unknown value does not have the **KNOWN** value that is assigned to the `_UNKNOWN` constant (typically `0`); - Lacking `#[non_exhaustive]` in most places, not allowing us to add new **and missing** constants to `enum`; - `#[repr(u32)]` that follows the default `u32` type used by `bindgen` when processing an "untyped" enum, rather than considering the type (`i32`, `i8`, etc) that is actually used by every function taking or returning this "integer constant"; - Inconsistent use of either: - Panicking on unknown values; - Wrapping unknown values in a custom `Err` type (i.e. `AudioError::UnknownValue` or `TryFromPrimitiveError`); - Assigning unknown values to an `Unknown = ffi::XXX_UNKNOWN` variant; - Sometimes returning them in a typed `Unknown(xxx)` variant. To solve this we have to make the following changes to every `pub enum` with custom discriminants in the NDK crate: - Drop all builtin type conversions and update `repr(u32)` to use the common type used by _every_ function that takes or returns this enum value. Conversions always happen via `.into()` from `IntoPrimitive` (needed to automatically process `__Unknown`); - Annotate all those `enum`s with `#[non_exhaustive]`. This requires callers to have a `x => todo!("New constant {x:?}")` or similar handling in a `match` block. It will neatly print the value for `__Unknown` added below; - Replace `TryFromPrimitive` with `IntoPrimitive`; - Have a catch-all `__Unknown` variant, that is `#[doc(hidden)]`. This: - Saves us from having a `TryIntoPrimitiveError` or custom `Err` type; - Allows the caller to pass in custom/unknown values; - Entices contributions/additions in the crate by having the type `#[doc(hidden)]`, as a lot of enums are currently lagging behind their upstream representation. - TODO: For error types like `AudioResult`/`AudioError`, implement `Error` on them immediately like on `MediaError`. This is possible now that the custom `UnknownValue` variant is gone. Note that for some (currently) input-only `enum`s IntoPrimitive
- Loading branch information
Showing
7 changed files
with
899 additions
and
825 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.