Skip to content

Commit eb445c6

Browse files
authored
ndk: Bump MSRV from 1.64 to 1.66 (#431)
It is older than 6 months and now required for the `toml_edit` dependency (used by `num_enum_derive`). At the same time `num_enum` relies on arbitrary enum discriminants (for discriminated enums with tuple/struct variants) introduced by Rust 1.66 in order to implement `#[num_enum(catch_all)]`. This feature comes in use to replace the manual `match` blocks when implementing conversions for `HardwareBufferFormat` when also having an `Unknown(u32)` to catch valid private/vendor values that won't ever be described inside the NDK. This change effectively reverts #407 to its initial state, where a `catch_all` implementation was used. For the latter the intent is however to use this feature sparingly. In most APIs new values are few and far between, so treating these as an `Err` via `TryFromPrimitive` is desired to provoke upstream issue reports and quick turnaround on new values. Same for `enum`s that are used to pass values into functions: it is desired to only pass known values (by this `ndk` crate) into those, anything else should similarly be reported and added upstream. In these cases a `#[non_exhaustive]` allows us to do so with a tiny non-breaking patch release.
1 parent 7abf3e4 commit eb445c6

File tree

10 files changed

+70
-123
lines changed

10 files changed

+70
-123
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ jobs:
3838
run: cargo check -p ndk-sys --all-targets --all-features --target aarch64-linux-android
3939

4040
check_msrv:
41-
name: Check overall MSRV (1.64.0)
41+
name: Check overall MSRV (1.66.0)
4242
runs-on: ubuntu-latest
4343
steps:
4444
- uses: actions/checkout@v1
4545

46-
- uses: dtolnay/rust-toolchain@1.64.0
46+
- uses: dtolnay/rust-toolchain@1.66.0
4747
with:
4848
target: aarch64-linux-android
4949

ndk/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- event: Add `tool_type` getter for `Pointer`. (#323)
66
- input_queue: Allow any non-zero return code from `pre_dispatch()` again, as per documentation. (#325)
77
- asset: Use entire asset length when mapping buffer. (#387)
8-
- Bump MSRV to 1.64 for `raw-window-handle 0.5.1`. (#388)
8+
- Bump MSRV to 1.66 for `raw-window-handle 0.5.1`, `num_enum`'s `catch_all` with arbitrary enum discriminants. (#388,#431)
99
- Bump optional `jni` dependency for doctest example from `0.19` to `0.21`. (#390)
1010
- **Breaking:** Upgrade to [`ndk-sys 0.5.0`](../ndk-sys/CHANGELOG.md#050-TODO). (#370)
1111
- **Breaking:** Upgrade `bitflags` crate from `1` to `2`. (#394)

ndk/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "ndk"
33
version = "0.8.0-beta.0"
4-
authors = ["The Rust Windowing contributors"]
4+
authors = ["The Rust Mobile contributors"]
55
edition = "2021"
66
description = "Safe Rust bindings to the Android NDK"
77
license = "MIT OR Apache-2.0"
@@ -10,7 +10,7 @@ readme = "../README.md"
1010
documentation = "https://docs.rs/ndk"
1111
homepage = "https://github.com/rust-mobile/ndk"
1212
repository = "https://github.com/rust-mobile/ndk"
13-
rust-version = "1.64"
13+
rust-version = "1.66"
1414

1515
[features]
1616
all = ["audio", "bitmap","media", "api-level-30"]

ndk/src/asset.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
//! [`AAssetDir`]: https://developer.android.com/ndk/reference/group/asset#aassetdir
55
//! [`AAssetManager`]: https://developer.android.com/ndk/reference/group/asset#aassetmanager
66
7-
use std::ffi::{CStr, CString};
8-
use std::io;
9-
// TODO: Import from std::os::fd::{} since Rust 1.66
10-
use std::os::unix::io::{FromRawFd, OwnedFd};
11-
use std::ptr::NonNull;
7+
use std::{
8+
ffi::{CStr, CString},
9+
io,
10+
os::fd::{FromRawFd, OwnedFd},
11+
ptr::NonNull,
12+
};
1213

1314
/// A native [`AAssetManager *`]
1415
///

ndk/src/hardware_buffer.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use std::{
1313
mem::MaybeUninit,
1414
ops::Deref,
1515
os::{
16+
fd::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd},
1617
raw::c_void,
17-
// TODO: Import from std::os::fd::{} since Rust 1.66
18-
unix::io::{AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd},
1918
},
2019
ptr::NonNull,
2120
};
@@ -211,7 +210,7 @@ impl HardwareBuffer {
211210
width: desc.width,
212211
height: desc.height,
213212
layers: desc.layers,
214-
format: ffi::AHardwareBuffer_Format(desc.format).into(),
213+
format: desc.format.into(),
215214
usage: HardwareBufferUsage(ffi::AHardwareBuffer_UsageFlags(desc.usage)),
216215
stride: desc.stride,
217216
}
@@ -497,7 +496,7 @@ impl HardwareBufferDesc {
497496
width: self.width,
498497
height: self.height,
499498
layers: self.layers,
500-
format: ffi::AHardwareBuffer_Format::from(self.format).0,
499+
format: self.format.into(),
501500
usage: self.usage.0 .0,
502501
stride: self.stride,
503502
rfu0: 0,

ndk/src/hardware_buffer_format.rs

Lines changed: 40 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -2,121 +2,62 @@
22
//!
33
//! [`AHardwareBuffer_Format`]: https://developer.android.com/ndk/reference/group/a-hardware-buffer#ahardwarebuffer_format
44
5+
use num_enum::{FromPrimitive, IntoPrimitive};
6+
57
/// Buffer pixel formats.
6-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8+
#[repr(u32)]
9+
#[derive(Copy, Clone, Debug, PartialEq, Eq, FromPrimitive, IntoPrimitive)]
710
#[allow(non_camel_case_types)]
811
pub enum HardwareBufferFormat {
9-
/// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGBA_8888`].
10-
R8G8B8A8_UNORM,
11-
/// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGBX_8888`].
12-
R8G8B8X8_UNORM,
12+
/// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGBA_8888`].0.
13+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM")]
14+
R8G8B8A8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM.0,
15+
/// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGBX_8888`].0.
16+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM")]
17+
R8G8B8X8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM.0,
1318
#[cfg(feature = "api-level-26")]
14-
R8G8B8_UNORM,
15-
/// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGB_565`].
16-
R5G6B5_UNORM,
19+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM")]
20+
R8G8B8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM.0,
21+
/// Matches deprecated [`ffi::ANativeWindow_LegacyFormat::WINDOW_FORMAT_RGB_565`].0.
22+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM")]
23+
R5G6B5_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM.0,
1724
#[cfg(feature = "api-level-26")]
18-
R16G16B16A16_FLOAT,
25+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT")]
26+
R16G16B16A16_FLOAT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT.0,
1927
#[cfg(feature = "api-level-26")]
20-
R10G10B10A2_UNORM,
28+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM")]
29+
R10G10B10A2_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM.0,
2130
#[cfg(feature = "api-level-26")]
22-
BLOB,
31+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_BLOB")]
32+
BLOB = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_BLOB.0,
2333
#[cfg(feature = "api-level-26")]
24-
D16_UNORM,
34+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_D16_UNORM")]
35+
D16_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D16_UNORM.0,
2536
#[cfg(feature = "api-level-26")]
26-
D24_UNORM,
37+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_D24_UNORM")]
38+
D24_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D24_UNORM.0,
2739
#[cfg(feature = "api-level-26")]
28-
D24_UNORM_S8_UINT,
40+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT")]
41+
D24_UNORM_S8_UINT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT.0,
2942
#[cfg(feature = "api-level-26")]
30-
D32_FLOAT,
43+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_D32_FLOAT")]
44+
D32_FLOAT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D32_FLOAT.0,
3145
#[cfg(feature = "api-level-26")]
32-
D32_FLOAT_S8_UINT,
46+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT")]
47+
D32_FLOAT_S8_UINT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT.0,
3348
#[cfg(feature = "api-level-26")]
34-
S8_UINT,
49+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_S8_UINT")]
50+
S8_UINT = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_S8_UINT.0,
3551
#[cfg(feature = "api-level-26")]
36-
Y8Cb8Cr8_420,
52+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420")]
53+
Y8Cb8Cr8_420 = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420.0,
3754
#[cfg(feature = "api-level-26")]
38-
YCbCr_P010,
55+
#[doc(alias = "AHARDWAREBUFFER_FORMAT_YCbCr_P010")]
56+
YCbCr_P010 = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_YCbCr_P010.0,
3957
#[cfg(feature = "api-level-26")]
40-
R8_UNORM,
41-
Unknown(ffi::AHardwareBuffer_Format),
42-
}
43-
44-
impl From<ffi::AHardwareBuffer_Format> for HardwareBufferFormat {
45-
fn from(value: ffi::AHardwareBuffer_Format) -> Self {
46-
use ffi::AHardwareBuffer_Format as AFormat;
47-
use HardwareBufferFormat::*;
48-
match value {
49-
AFormat::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM => R8G8B8A8_UNORM,
50-
AFormat::AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM => R8G8B8X8_UNORM,
51-
#[cfg(feature = "api-level-26")]
52-
AFormat::AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM => R8G8B8_UNORM,
53-
AFormat::AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM => R5G6B5_UNORM,
54-
#[cfg(feature = "api-level-26")]
55-
AFormat::AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT => R16G16B16A16_FLOAT,
56-
#[cfg(feature = "api-level-26")]
57-
AFormat::AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM => R10G10B10A2_UNORM,
58-
#[cfg(feature = "api-level-26")]
59-
AFormat::AHARDWAREBUFFER_FORMAT_BLOB => BLOB,
60-
#[cfg(feature = "api-level-26")]
61-
AFormat::AHARDWAREBUFFER_FORMAT_D16_UNORM => D16_UNORM,
62-
#[cfg(feature = "api-level-26")]
63-
AFormat::AHARDWAREBUFFER_FORMAT_D24_UNORM => D24_UNORM,
64-
#[cfg(feature = "api-level-26")]
65-
AFormat::AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT => D24_UNORM_S8_UINT,
66-
#[cfg(feature = "api-level-26")]
67-
AFormat::AHARDWAREBUFFER_FORMAT_D32_FLOAT => D32_FLOAT,
68-
#[cfg(feature = "api-level-26")]
69-
AFormat::AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT => D32_FLOAT_S8_UINT,
70-
#[cfg(feature = "api-level-26")]
71-
AFormat::AHARDWAREBUFFER_FORMAT_S8_UINT => S8_UINT,
72-
#[cfg(feature = "api-level-26")]
73-
AFormat::AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420 => Y8Cb8Cr8_420,
74-
#[cfg(feature = "api-level-26")]
75-
AFormat::AHARDWAREBUFFER_FORMAT_YCbCr_P010 => YCbCr_P010,
76-
#[cfg(feature = "api-level-26")]
77-
AFormat::AHARDWAREBUFFER_FORMAT_R8_UNORM => R8_UNORM,
78-
_ => Unknown(value),
79-
}
80-
}
81-
}
82-
83-
impl From<HardwareBufferFormat> for ffi::AHardwareBuffer_Format {
84-
fn from(value: HardwareBufferFormat) -> Self {
85-
use ffi::AHardwareBuffer_Format as AFormat;
86-
use HardwareBufferFormat::*;
87-
match value {
88-
R8G8B8A8_UNORM => AFormat::AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
89-
R8G8B8X8_UNORM => AFormat::AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM,
90-
#[cfg(feature = "api-level-26")]
91-
R8G8B8_UNORM => AFormat::AHARDWAREBUFFER_FORMAT_R8G8B8_UNORM,
92-
R5G6B5_UNORM => AFormat::AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM,
93-
#[cfg(feature = "api-level-26")]
94-
R16G16B16A16_FLOAT => AFormat::AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT,
95-
#[cfg(feature = "api-level-26")]
96-
R10G10B10A2_UNORM => AFormat::AHARDWAREBUFFER_FORMAT_R10G10B10A2_UNORM,
97-
#[cfg(feature = "api-level-26")]
98-
BLOB => AFormat::AHARDWAREBUFFER_FORMAT_BLOB,
99-
#[cfg(feature = "api-level-26")]
100-
D16_UNORM => AFormat::AHARDWAREBUFFER_FORMAT_D16_UNORM,
101-
#[cfg(feature = "api-level-26")]
102-
D24_UNORM => AFormat::AHARDWAREBUFFER_FORMAT_D24_UNORM,
103-
#[cfg(feature = "api-level-26")]
104-
D24_UNORM_S8_UINT => AFormat::AHARDWAREBUFFER_FORMAT_D24_UNORM_S8_UINT,
105-
#[cfg(feature = "api-level-26")]
106-
D32_FLOAT => AFormat::AHARDWAREBUFFER_FORMAT_D32_FLOAT,
107-
#[cfg(feature = "api-level-26")]
108-
D32_FLOAT_S8_UINT => AFormat::AHARDWAREBUFFER_FORMAT_D32_FLOAT_S8_UINT,
109-
#[cfg(feature = "api-level-26")]
110-
S8_UINT => AFormat::AHARDWAREBUFFER_FORMAT_S8_UINT,
111-
#[cfg(feature = "api-level-26")]
112-
Y8Cb8Cr8_420 => AFormat::AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420,
113-
#[cfg(feature = "api-level-26")]
114-
YCbCr_P010 => AFormat::AHARDWAREBUFFER_FORMAT_YCbCr_P010,
115-
#[cfg(feature = "api-level-26")]
116-
R8_UNORM => AFormat::AHARDWAREBUFFER_FORMAT_R8_UNORM,
117-
Unknown(x) => x,
118-
}
119-
}
58+
R8_UNORM = ffi::AHardwareBuffer_Format::AHARDWAREBUFFER_FORMAT_R8_UNORM.0,
59+
#[num_enum(catch_all)]
60+
Unknown(u32),
12061
}
12162

12263
impl HardwareBufferFormat {

ndk/src/looper.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
1212
use bitflags::bitflags;
1313
use std::mem::ManuallyDrop;
14-
use std::os::raw::c_void;
15-
// TODO: Import from std::os::fd::{} since Rust 1.66
16-
use std::os::unix::io::{AsRawFd, BorrowedFd, RawFd};
14+
use std::os::{
15+
fd::{AsRawFd, BorrowedFd, RawFd},
16+
raw::c_void,
17+
};
1718
use std::ptr;
1819
use std::time::Duration;
1920
use thiserror::Error;

ndk/src/media/image_reader.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use std::{
1616
};
1717

1818
#[cfg(feature = "api-level-26")]
19-
// TODO: Import from std::os::fd::{} since Rust 1.66
20-
use std::os::unix::io::{FromRawFd, IntoRawFd, OwnedFd};
19+
use std::os::fd::{FromRawFd, IntoRawFd, OwnedFd};
2120

2221
#[cfg(feature = "api-level-26")]
2322
use crate::hardware_buffer::{HardwareBuffer, HardwareBufferUsage};

ndk/src/native_activity.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ impl NativeActivity {
219219
unsafe {
220220
ffi::ANativeActivity_setWindowFormat(
221221
self.ptr.as_ptr(),
222-
ffi::AHardwareBuffer_Format::from(format).0 as i32,
222+
u32::from(format)
223+
.try_into()
224+
.expect("i32 overflow in set_window_format()"),
223225
)
224226
}
225227
}

ndk/src/native_window.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl NativeWindow {
8181
pub fn format(&self) -> HardwareBufferFormat {
8282
let value = unsafe { ffi::ANativeWindow_getFormat(self.ptr.as_ptr()) };
8383
let value = u32::try_from(value).unwrap();
84-
HardwareBufferFormat::from(ffi::AHardwareBuffer_Format(value))
84+
value.into()
8585
}
8686

8787
/// Change the format and size of the window buffers.
@@ -99,9 +99,13 @@ impl NativeWindow {
9999
height: i32,
100100
format: Option<HardwareBufferFormat>,
101101
) -> Result<()> {
102-
let format = format.map_or(0, |f| ffi::AHardwareBuffer_Format::from(f).0);
102+
let format = format.map_or(0, |f| {
103+
u32::from(f)
104+
.try_into()
105+
.expect("i32 overflow in set_buffers_geometry")
106+
});
103107
let status = unsafe {
104-
ffi::ANativeWindow_setBuffersGeometry(self.ptr.as_ptr(), width, height, format as i32)
108+
ffi::ANativeWindow_setBuffersGeometry(self.ptr.as_ptr(), width, height, format)
105109
};
106110
status_to_io_result(status)
107111
}
@@ -179,7 +183,7 @@ impl<'a> NativeWindowBufferLockGuard<'a> {
179183
/// The format of the buffer. One of [`HardwareBufferFormat`].
180184
pub fn format(&self) -> HardwareBufferFormat {
181185
let format = u32::try_from(self.buffer.format).unwrap();
182-
HardwareBufferFormat::from(ffi::AHardwareBuffer_Format(format))
186+
format.into()
183187
}
184188

185189
/// The actual bits.

0 commit comments

Comments
 (0)