Skip to content
This repository was archived by the owner on Jul 3, 2020. It is now read-only.

Commit 6d17cff

Browse files
OptimisticPeachmb64
authored andcommitted
Fixed the mem::uninitialized warnings (#240)
* Fixed the `mem::uninitialized` errors by using `MaybeUninit` and added a few missing `dyn`s. * Implemented error for `AssetError` * Implemented error for `AssetError` * Update lib.rs * Fixes some more warnings about improper c types
1 parent 4eddc8b commit 6d17cff

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

cargo-apk/injected-glue/lib.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,10 @@ pub fn android_main2<F>(app: *mut ffi::android_app, main_function: F)
295295
}
296296
}
297297

298-
let mut thread = mem::uninitialized();
299-
let result = pthread_create(&mut thread, ptr::null(), logging_thread,
298+
let mut thread = mem::MaybeUninit::uninit();
299+
let result = pthread_create(thread.as_mut_ptr(), ptr::null(), logging_thread,
300300
pfd[0] as usize as *mut c_void);
301+
let thread = thread.assume_init();
301302
assert_eq!(result, 0);
302303
let result = pthread_detach(thread);
303304
assert_eq!(result, 0);
@@ -344,14 +345,14 @@ pub fn android_main2<F>(app: *mut ffi::android_app, main_function: F)
344345
break;
345346
}
346347

347-
let mut events = mem::uninitialized();
348-
let mut source: *mut ffi::android_poll_source = mem::uninitialized();
348+
let mut events = mem::MaybeUninit::uninit();
349+
let mut source: mem::MaybeUninit<*mut ffi::android_poll_source> = mem::MaybeUninit::uninit();
349350

350351
// A `-1` means to block forever, but any other positive value
351352
// specifies the number of milliseconds to block for, before
352353
// returning.
353-
let code = ffi::ALooper_pollAll(-1, ptr::null_mut(), &mut events,
354-
&mut source as *mut _ as *mut _);
354+
let code = ffi::ALooper_pollAll(-1, ptr::null_mut(), events.as_mut_ptr(),
355+
source.as_mut_ptr() as *mut _ as *mut _);
355356
if code == ffi::ALOOPER_POLL_WAKE {
356357
send_event(Event::Wake)
357358
}
@@ -369,7 +370,7 @@ pub fn android_main2<F>(app: *mut ffi::android_app, main_function: F)
369370
// the user will get a locked UI until the system terminates our
370371
// process. So we continue processing events..
371372
}
372-
373+
let source = source.assume_init();
373374
// Processing the event
374375
if !source.is_null() {
375376
((*source).process)(ANDROID_APP, source);

glue/src/lib.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
extern {
44
fn cargo_apk_injected_glue_get_native_window() -> *const c_void;
5-
fn cargo_apk_injected_glue_add_sender(sender: *mut ());
6-
fn cargo_apk_injected_glue_add_sender_missing(sender: *mut ());
7-
fn cargo_apk_injected_glue_add_sync_event_handler(sender: *mut ());
8-
fn cargo_apk_injected_glue_remove_sync_event_handler(sender: *mut ());
5+
fn cargo_apk_injected_glue_add_sender(sender: *mut c_void);
6+
fn cargo_apk_injected_glue_add_sender_missing(sender: *mut c_void);
7+
fn cargo_apk_injected_glue_add_sync_event_handler(sender: *mut c_void);
8+
fn cargo_apk_injected_glue_remove_sync_event_handler(sender: *mut c_void);
99
fn cargo_apk_injected_glue_set_multitouch(multitouch: bool);
10-
fn cargo_apk_injected_glue_write_log(ptr: *const (), len: usize);
11-
fn cargo_apk_injected_glue_load_asset(ptr: *const (), len: usize) -> *mut c_void;
10+
fn cargo_apk_injected_glue_write_log(ptr: *const c_void, len: usize);
11+
fn cargo_apk_injected_glue_load_asset(ptr: *const c_void, len: usize) -> *mut c_void;
1212
fn cargo_apk_injected_glue_wake_event_loop();
1313
}
1414

@@ -60,11 +60,20 @@ pub enum MotionAction {
6060
Cancel,
6161
}
6262

63+
#[derive(Clone, Copy, Debug)]
6364
pub enum AssetError {
6465
AssetMissing,
6566
EmptyBuffer,
6667
}
6768

69+
impl std::fmt::Display for AssetError {
70+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71+
write!(f, "{:?}", self)
72+
}
73+
}
74+
75+
impl std::error::Error for AssetError {}
76+
6877
// Trait used to dispatch sync events from the polling loop thread.
6978
pub trait SyncEventHandler {
7079
fn handle(&mut self, event: &Event);
@@ -81,7 +90,7 @@ pub fn add_sender(sender: Sender<Event>) {
8190

8291
/// Adds a SyncEventHandler which will receive sync events from the polling loop.
8392
#[inline]
84-
pub fn add_sync_event_handler(handler: Box<SyncEventHandler>) {
93+
pub fn add_sync_event_handler(handler: Box<dyn SyncEventHandler>) {
8594
unsafe {
8695
let handler = Box::into_raw(Box::new(handler)) as *mut _;
8796
cargo_apk_injected_glue_add_sync_event_handler(handler);
@@ -90,7 +99,7 @@ pub fn add_sync_event_handler(handler: Box<SyncEventHandler>) {
9099

91100
/// Removes a SyncEventHandler.
92101
#[inline]
93-
pub fn remove_sync_event_handler(handler: *const SyncEventHandler) {
102+
pub fn remove_sync_event_handler(handler: *const dyn SyncEventHandler) {
94103
unsafe {
95104
let handler = Box::into_raw(Box::new(handler)) as *mut _;
96105
cargo_apk_injected_glue_remove_sync_event_handler(handler);
@@ -143,7 +152,7 @@ pub fn load_asset(filename: &str) -> Result<Vec<u8>, AssetError> {
143152
}
144153
}
145154

146-
// Wakes the event poll asynchronously and sends a Event::Wake event to the senders.
155+
// Wakes the event poll asynchronously and sends a Event::Wake event to the senders.
147156
// This method can be called on any thread. This method returns immediately.
148157
#[inline]
149158
pub fn wake_event_loop() {

0 commit comments

Comments
 (0)