Skip to content

Commit 8de2b6d

Browse files
committed
Clippy lint fixes
1 parent 57b5192 commit 8de2b6d

6 files changed

Lines changed: 29 additions & 26 deletions

File tree

android-activity/src/game_activity/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,19 +174,28 @@ impl AndroidApp {
174174
}
175175
}
176176

177+
// Wrapper around the raw android_app pointer that can be safely sent across threads.
178+
// SAFETY: The android_app pointer is managed by the GameActivity glue code and protected
179+
// by a Mutex. Access is synchronized and the pointer is cleared on APP_CMD_DESTROY.
180+
// The Mutex wrapper provides Sync, so we only need to implement Send.
181+
#[derive(Debug)]
182+
struct SendAndroidApp(*mut ffi::android_app);
183+
184+
unsafe impl Send for SendAndroidApp {}
185+
177186
#[derive(Debug, Clone)]
178187
struct GameActivityGlue {
179-
game_activity_app: Arc<Mutex<*mut ffi::android_app>>,
188+
game_activity_app: Arc<Mutex<SendAndroidApp>>,
180189
}
181190

182191
impl GameActivityGlue {
183192
fn new(game_activity_app: *mut ffi::android_app) -> Self {
184193
Self {
185-
game_activity_app: Arc::new(Mutex::new(game_activity_app)),
194+
game_activity_app: Arc::new(Mutex::new(SendAndroidApp(game_activity_app))),
186195
}
187196
}
188197

189-
fn locked_app(&self) -> std::sync::MutexGuard<'_, *mut ffi::android_app> {
198+
fn locked_app(&self) -> std::sync::MutexGuard<'_, SendAndroidApp> {
190199
self.game_activity_app.lock().unwrap()
191200
}
192201

@@ -203,15 +212,15 @@ impl GameActivityGlue {
203212
F: FnOnce(*mut ffi::android_app) -> R,
204213
{
205214
let app = self.locked_app();
206-
f(*app)
215+
f(app.0)
207216
}
208217

209218
/// Called when handling the `APP_CMD_DESTROY` event to clear our retained
210219
/// pointer to the GameActivity `android_app` glue so that we don't
211220
/// accidentally access it after it's been freed.
212221
fn clear_app(&self) {
213222
let mut app = self.locked_app();
214-
*app = ptr::null_mut();
223+
app.0 = ptr::null_mut();
215224
}
216225
}
217226

android-activity/src/init.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn forward_stdio_to_logcat() -> std::thread::JoinHandle<std::io::Result<()>> {
3333
std::thread::Builder::new()
3434
.name("stdio-to-logcat".to_string())
3535
.spawn(move || -> std::io::Result<()> {
36-
let tag = CStr::from_bytes_with_nul(b"RustStdoutStderr\0").unwrap();
36+
let tag = c"RustStdoutStderr";
3737
let mut reader = BufReader::new(file);
3838
let mut buffer = String::new();
3939
loop {
@@ -258,7 +258,7 @@ fn try_init_current_thread(env: &mut jni::Env, activity: &JObject) -> jni::error
258258
// Also name native thread - this needs to happen here after attaching to a JVM thread,
259259
// since that changes the thread name to something like "Thread-2".
260260
unsafe {
261-
let thread_name = std::ffi::CStr::from_bytes_with_nul(b"android_main\0").unwrap();
261+
let thread_name = c"android_main";
262262
let _ = libc::pthread_setname_np(libc::pthread_self(), thread_name.as_ptr());
263263
}
264264
Ok(())

android-activity/src/input/sdk.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ jni::bind_java_type! {
7878
}
7979

8080
impl AKeyCharacterMap<'_> {
81-
pub(crate) fn get<'local>(
81+
pub(crate) fn get(
8282
&self,
83-
env: &'local mut jni::Env,
83+
env: &mut jni::Env,
8484
key_code: jint,
8585
meta_state: jint,
8686
) -> Result<jint, InternalAppError> {
@@ -97,10 +97,7 @@ impl AKeyCharacterMap<'_> {
9797
.map_err(|err| jni_utils::clear_and_map_exception_to_err(env, err))
9898
}
9999

100-
pub(crate) fn get_keyboard_type<'local>(
101-
&self,
102-
env: &'local mut jni::Env,
103-
) -> Result<jint, InternalAppError> {
100+
pub(crate) fn get_keyboard_type(&self, env: &mut jni::Env) -> Result<jint, InternalAppError> {
104101
self._get_keyboard_type(env)
105102
.map_err(|err| jni_utils::clear_and_map_exception_to_err(env, err))
106103
}
@@ -183,7 +180,7 @@ impl KeyCharacterMap {
183180
}
184181
})
185182
.map_err(|err| {
186-
let err: InternalAppError = err.into();
183+
let err: InternalAppError = err;
187184
err.into()
188185
})
189186
}
@@ -217,7 +214,7 @@ impl KeyCharacterMap {
217214
})
218215
})
219216
.map_err(|err| {
220-
let err: InternalAppError = err.into();
217+
let err: InternalAppError = err;
221218
err.into()
222219
})
223220
}
@@ -239,7 +236,7 @@ impl KeyCharacterMap {
239236
Ok(keyboard_type.into())
240237
})
241238
.map_err(|err| {
242-
let err: InternalAppError = err.into();
239+
let err: InternalAppError = err;
243240
err.into()
244241
})
245242
}

android-activity/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@
206206
//!
207207
//! Before `android_main()` is called:
208208
//! - A `JavaVM` and
209-
//! [`android.content.Context`](https://developer.android.com/reference/android/content/Context)
210-
//! instance will be associated with the [`ndk_context`] crate so that other,
211-
//! independent, Rust crates are able to find a JavaVM for making JNI calls.
209+
//! [`android.content.Context`](https://developer.android.com/reference/android/content/Context)
210+
//! instance will be associated with the [`ndk_context`] crate so that other,
211+
//! independent, Rust crates are able to find a JavaVM for making JNI calls.
212212
//! - The `JavaVM` will be attached to the native thread (for JNI)
213213
//! - A [Looper] is attached to the Rust native thread.
214214
//!
@@ -1358,7 +1358,7 @@ impl<'a> OnCreateState<'a> {
13581358
/// - Don't wrap the reference in an `Auto` which would treat the reference
13591359
/// like a local reference and try to delete it when dropped.
13601360
pub fn activity_as_ptr(&self) -> *mut c_void {
1361-
self.java_activity as *mut c_void
1361+
self.java_activity
13621362
}
13631363

13641364
/// Returns the saved state of the `Activity` as a byte slice, which may be

android-activity/src/native_activity/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ impl AndroidAppInner {
351351
return;
352352
}
353353

354-
let na_mut = na as *mut ndk_sys::ANativeActivity;
354+
let na_mut = na;
355355
unsafe {
356356
ndk_sys::ANativeActivity_setWindowFlags(
357357
na_mut.cast(),

android-activity/src/util.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) fn android_log(level: Level, tag: &CStr, msg: &CStr) {
3232
}
3333

3434
pub(crate) fn log_panic(panic: Box<dyn std::any::Any + Send>) {
35-
let rust_panic = unsafe { CStr::from_bytes_with_nul_unchecked(b"RustPanic\0") };
35+
let rust_panic = c"RustPanic";
3636

3737
if let Some(panic) = panic.downcast_ref::<String>() {
3838
if let Ok(msg) = CString::new(panic.clone()) {
@@ -43,10 +43,7 @@ pub(crate) fn log_panic(panic: Box<dyn std::any::Any + Send>) {
4343
android_log(Level::Error, rust_panic, &msg);
4444
}
4545
} else {
46-
let unknown_panic = unsafe { CStr::from_bytes_with_nul_unchecked(b"UnknownPanic\0") };
47-
android_log(Level::Error, unknown_panic, unsafe {
48-
CStr::from_bytes_with_nul_unchecked(b"\0")
49-
});
46+
android_log(Level::Error, rust_panic, c"UnknownPanic");
5047
}
5148
}
5249

0 commit comments

Comments
 (0)