Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
target
Cargo.lock
22 changes: 22 additions & 0 deletions android-activity/src/game_activity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::ptr;
use std::ptr::NonNull;
use std::sync::Weak;
use std::sync::{Arc, Mutex, RwLock};
use std::task::{RawWaker, RawWakerVTable, Waker};
use std::time::Duration;

use libc::c_void;
Expand Down Expand Up @@ -118,6 +119,27 @@ impl AndroidAppWaker {
ALooper_wake(self.looper.as_ptr());
}
}

/// Creates a [`Waker`] that wakes up the [`AndroidApp`].
///
/// This is useful for using this crate in `async` environments.
///
/// [`Waker`]: std::task::Waker
pub fn waker(self) -> Waker {
const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);

unsafe fn clone(data: *const ()) -> RawWaker {
RawWaker::new(data, &VTABLE)
}

unsafe fn wake(data: *const ()) {
ndk_sys::ALooper_wake(data as *const _ as *mut _)
}

unsafe fn drop(_: *const ()) {}

unsafe { Waker::from_raw(RawWaker::new(self.looper.as_ptr() as *const (), &VTABLE)) }
}
}

impl AndroidApp {
Expand Down
22 changes: 22 additions & 0 deletions android-activity/src/native_activity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::panic::AssertUnwindSafe;
use std::ptr;
use std::ptr::NonNull;
use std::sync::{Arc, Mutex, RwLock, Weak};
use std::task::{RawWaker, RawWakerVTable, Waker};
use std::time::Duration;

use libc::c_void;
Expand Down Expand Up @@ -83,6 +84,27 @@ impl AndroidAppWaker {
ndk_sys::ALooper_wake(self.looper.as_ptr());
}
}

/// Creates a [`Waker`] that wakes up the [`AndroidApp`].
///
/// This is useful for using this crate in `async` environments.
///
/// [`Waker`]: std::task::Waker
pub fn waker(self) -> Waker {
const VTABLE: RawWakerVTable = RawWakerVTable::new(clone, wake, wake, drop);

unsafe fn clone(data: *const ()) -> RawWaker {
RawWaker::new(data, &VTABLE)
}

unsafe fn wake(data: *const ()) {
ndk_sys::ALooper_wake(data as *const _ as *mut _)
}

unsafe fn drop(_: *const ()) {}

unsafe { Waker::from_raw(RawWaker::new(self.looper.as_ptr() as *const (), &VTABLE)) }
}
}

impl AndroidApp {
Expand Down
Loading