Clippy fixes

This commit is contained in:
Robert Bragg
2022-11-11 03:09:57 +00:00
parent aa5fcc53bb
commit 38554d98db
5 changed files with 41 additions and 54 deletions
+11 -11
View File
@@ -50,14 +50,14 @@ impl<'a> StateSaver<'a> {
// In case the application calls store() multiple times for some reason we
// make sure to free any pre-existing state...
if (*app_ptr).savedState != ptr::null_mut() {
if !(*app_ptr).savedState.is_null() {
libc::free((*app_ptr).savedState);
(*app_ptr).savedState = ptr::null_mut();
(*app_ptr).savedStateSize = 0;
}
let buf = libc::malloc(state.len());
if buf == ptr::null_mut() {
if buf.is_null() {
panic!("Failed to allocate save_state buffer");
}
@@ -84,7 +84,7 @@ impl<'a> StateLoader<'a> {
pub fn load(&self) -> Option<Vec<u8>> {
unsafe {
let app_ptr = self.app.native_app.as_ptr();
if (*app_ptr).savedState != ptr::null_mut() && (*app_ptr).savedStateSize > 0 {
if !(*app_ptr).savedState.is_null() && (*app_ptr).savedStateSize > 0 {
let buf: &mut [u8] = std::slice::from_raw_parts_mut(
(*app_ptr).savedState.cast(),
(*app_ptr).savedStateSize as usize,
@@ -155,7 +155,7 @@ pub struct AndroidAppInner {
}
impl AndroidAppInner {
pub fn native_window<'a>(&self) -> Option<NativeWindow> {
pub fn native_window(&self) -> Option<NativeWindow> {
self.native_window.read().unwrap().clone()
}
@@ -214,7 +214,7 @@ impl AndroidAppInner {
ffi::NativeAppGlueLooperId_LOOPER_ID_MAIN => {
trace!("ALooper_pollAll returned ID_MAIN");
let source: *mut ffi::android_poll_source = source.cast();
if source != ptr::null_mut() {
if !source.is_null() {
let cmd_i = ffi::android_app_read_cmd(native_app.as_ptr());
let cmd = match cmd_i as u32 {
@@ -248,11 +248,11 @@ impl AndroidAppInner {
}
ffi::NativeAppGlueAppCmd_APP_CMD_START => MainEvent::Start,
ffi::NativeAppGlueAppCmd_APP_CMD_RESUME => MainEvent::Resume {
loader: StateLoader { app: &self },
loader: StateLoader { app: self },
},
ffi::NativeAppGlueAppCmd_APP_CMD_SAVE_STATE => {
MainEvent::SaveState {
saver: StateSaver { app: &self },
saver: StateSaver { app: self },
}
}
ffi::NativeAppGlueAppCmd_APP_CMD_PAUSE => MainEvent::Pause,
@@ -391,14 +391,14 @@ impl AndroidAppInner {
}
}
pub fn input_events<'b, F>(&self, mut callback: F)
pub fn input_events<F>(&self, mut callback: F)
where
F: FnMut(&InputEvent) -> InputStatus,
{
let buf = unsafe {
let app_ptr = self.native_app.as_ptr();
let input_buffer = ffi::android_app_swap_input_buffers(app_ptr);
if input_buffer == ptr::null_mut() {
if input_buffer.is_null() {
return;
}
InputBuffer::from_ptr(NonNull::new_unchecked(input_buffer))
@@ -496,7 +496,7 @@ impl<'a> InputBuffer<'a> {
// XXX: It's really not ideal here that Rust iterators can't yield values
// that borrow from the iterator, so we implicitly have to copy the
// events as we iterate...
pub fn motion_events_iter<'b>(&'b self) -> MotionEventsIterator<'b> {
pub fn motion_events_iter(&self) -> MotionEventsIterator {
unsafe {
let count = (*self.ptr.as_ptr()).motionEventsCount as usize;
MotionEventsIterator {
@@ -507,7 +507,7 @@ impl<'a> InputBuffer<'a> {
}
}
pub fn key_events_iter<'b>(&'b self) -> KeyEventsIterator<'b> {
pub fn key_events_iter(&self) -> KeyEventsIterator {
unsafe {
let count = (*self.ptr.as_ptr()).keyEventsCount as usize;
KeyEventsIterator {
+12 -13
View File
@@ -134,13 +134,13 @@ impl Rect {
}
}
impl Into<ndk_sys::ARect> for Rect {
fn into(self) -> ndk_sys::ARect {
ndk_sys::ARect {
left: self.left,
right: self.right,
top: self.top,
bottom: self.bottom,
impl From<Rect> for ndk_sys::ARect {
fn from(rect: Rect) -> Self {
Self {
left: rect.left,
right: rect.right,
top: rect.top,
bottom: rect.bottom,
}
}
}
@@ -156,9 +156,8 @@ impl From<ndk_sys::ARect> for Rect {
}
}
pub use activity_impl::StateSaver;
pub use activity_impl::StateLoader;
pub use activity_impl::StateSaver;
/// An application event delivered during [`AndroidApp::poll_events`]
#[non_exhaustive]
@@ -464,14 +463,13 @@ impl Hash for AndroidApp {
}
}
impl AndroidApp {
/// Queries the current [`NativeWindow`] for the application.
///
/// This will only return `Some(window)` between
/// [`MainEvent::InitWindow`] and [`MainEvent::TerminateWindow`]
/// events.
pub fn native_window<'a>(&self) -> Option<NativeWindow> {
pub fn native_window(&self) -> Option<NativeWindow> {
self.inner.read().unwrap().native_window()
}
@@ -590,7 +588,7 @@ impl AndroidApp {
///
/// To reduce overhead, by default only [`input::Axis::X`] and [`input::Axis::Y`] are enabled
/// and other axis should be enabled explicitly via [`Self::enable_motion_axis`].
pub fn input_events<'b, F>(&self, callback: F)
pub fn input_events<F>(&self, callback: F)
where
F: FnMut(&input::InputEvent) -> InputStatus,
{
@@ -603,7 +601,8 @@ impl AndroidApp {
pub fn sdk_version() -> i32 {
let mut prop = android_properties::getprop("ro.build.version.sdk");
if let Some(val) = prop.value() {
i32::from_str_radix(&val, 10).expect("Failed to parse ro.build.version.sdk property")
val.parse::<i32>()
.expect("Failed to parse ro.build.version.sdk property")
} else {
panic!("Couldn't read ro.build.version.sdk system property");
}
+9 -14
View File
@@ -12,8 +12,6 @@ use std::{
sync::{Arc, Condvar, Mutex, Weak},
};
use libc;
use log::Level;
use ndk::{configuration::Configuration, input_queue::InputQueue, native_window::NativeWindow};
use ndk_sys::ANativeActivity;
@@ -160,7 +158,7 @@ impl NativeActivityGlue {
) -> Option<InputQueue> {
let mut guard = self.mutex.lock().unwrap();
if guard.input_queue == ptr::null_mut() {
if guard.input_queue.is_null() {
return None;
}
@@ -272,7 +270,7 @@ impl NativeActivityState {
looper: *mut ndk_sys::ALooper,
ident: libc::c_int,
) {
if self.input_queue != ptr::null_mut() {
if !self.input_queue.is_null() {
log::trace!("Attaching input queue to looper");
ndk_sys::AInputQueue_attachLooper(
self.input_queue,
@@ -285,7 +283,7 @@ impl NativeActivityState {
}
pub unsafe fn detach_input_queue_from_looper(&mut self) {
if self.input_queue != ptr::null_mut() {
if !self.input_queue.is_null() {
log::trace!("Detaching input queue from looper");
ndk_sys::AInputQueue_detachLooper(self.input_queue);
}
@@ -461,12 +459,9 @@ impl WaitableNativeActivityState {
// The state_saved flag should only be set while in this method, and since
// it doesn't allow re-entrance and is cleared before returning then we expect
// this to be None
debug_assert!(
guard.app_has_saved_state == false,
"SaveState request clash"
);
debug_assert!(!guard.app_has_saved_state, "SaveState request clash");
guard.write_cmd(AppCmd::SaveState);
while guard.app_has_saved_state == false {
while !guard.app_has_saved_state {
guard = self.cond.wait(guard).unwrap();
}
guard.app_has_saved_state = false;
@@ -474,13 +469,13 @@ impl WaitableNativeActivityState {
// `ANativeActivity` explicitly documents that it expects save state to be
// given via a `malloc()` allocated pointer since it will automatically
// `free()` the state after it has been converted to a buffer for the JVM.
if guard.saved_state.len() > 0 {
if !guard.saved_state.is_empty() {
let saved_state_size = guard.saved_state.len() as _;
let saved_state_src_ptr = guard.saved_state.as_ptr();
unsafe {
let saved_state = libc::malloc(saved_state_size);
assert!(
saved_state != ptr::null_mut(),
!saved_state.is_null(),
"Failed to allocate {} bytes for restoring saved application state",
saved_state_size
);
@@ -494,7 +489,7 @@ impl WaitableNativeActivityState {
pub fn saved_state(&self) -> Option<Vec<u8>> {
let guard = self.mutex.lock().unwrap();
if guard.saved_state.len() > 0 {
if !guard.saved_state.is_empty() {
Some(guard.saved_state.clone())
} else {
None
@@ -530,7 +525,7 @@ impl WaitableNativeActivityState {
let mut guard = self.mutex.lock().unwrap();
guard.detach_input_queue_from_looper();
guard.input_queue = guard.pending_input_queue;
if guard.input_queue != ptr::null_mut() {
if !guard.input_queue.is_null() {
guard.attach_input_queue_to_looper(looper, input_queue_ident);
}
self.cond.notify_one();
+6 -13
View File
@@ -84,7 +84,6 @@ unsafe impl Send for AndroidAppWaker {}
unsafe impl Sync for AndroidAppWaker {}
impl AndroidAppWaker {
/// Interrupts the main thread if it is blocked within [`AndroidApp::poll_events()`]
///
/// If [`AndroidApp::poll_events()`] is interrupted it will invoke the poll
@@ -155,7 +154,7 @@ impl AndroidAppInner {
self.looper.ptr
}
pub fn native_window<'a>(&self) -> Option<NativeWindow> {
pub fn native_window(&self) -> Option<NativeWindow> {
self.native_activity.mutex.lock().unwrap().window.clone()
}
@@ -178,7 +177,7 @@ impl AndroidAppInner {
info!("Calling ALooper_pollAll, timeout = {timeout_milliseconds}");
assert!(
ndk_sys::ALooper_forThread() != ptr::null_mut(),
!ndk_sys::ALooper_forThread().is_null(),
"Application tried to poll events from non-main thread"
);
let id = ALooper_pollAll(
@@ -237,10 +236,10 @@ impl AndroidAppInner {
glue::AppCmd::LowMemory => Some(MainEvent::LowMemory),
glue::AppCmd::Start => Some(MainEvent::Start),
glue::AppCmd::Resume => Some(MainEvent::Resume {
loader: StateLoader { app: &self },
loader: StateLoader { app: self },
}),
glue::AppCmd::SaveState => Some(MainEvent::SaveState {
saver: StateSaver { app: &self },
saver: StateSaver { app: self },
}),
glue::AppCmd::Pause => Some(MainEvent::Pause),
glue::AppCmd::Stop => Some(MainEvent::Stop),
@@ -361,7 +360,7 @@ impl AndroidAppInner {
// NOP - The InputQueue API doesn't let us optimize which axis values are read
}
pub fn input_events<'b, F>(&self, mut callback: F)
pub fn input_events<F>(&self, mut callback: F)
where
F: FnMut(&input::InputEvent) -> InputStatus,
{
@@ -395,13 +394,7 @@ impl AndroidAppInner {
input::InputEvent::MotionEvent(e) => ndk::event::InputEvent::MotionEvent(e),
input::InputEvent::KeyEvent(e) => ndk::event::InputEvent::KeyEvent(e),
};
queue.finish_event(
ndk_event,
match handled {
InputStatus::Handled => true,
_ => false,
},
);
queue.finish_event(ndk_event, matches!(handled, InputStatus::Handled));
}
}
}
+3 -3
View File
@@ -1,14 +1,14 @@
use std::{ffi::CStr, os::raw::c_char, ptr};
use std::{ffi::CStr, os::raw::c_char};
pub fn try_get_path_from_ptr(path: *const c_char) -> Option<std::path::PathBuf> {
if path == ptr::null() {
if path.is_null() {
return None;
}
let cstr = unsafe {
let cstr_slice = CStr::from_ptr(path.cast());
cstr_slice.to_str().ok()?
};
if cstr.len() == 0 {
if cstr.is_empty() {
return None;
}
Some(std::path::PathBuf::from(cstr))