Struct ndk::looper::ForeignLooper
source · pub struct ForeignLooper { /* private fields */ }
Expand description
A native ALooper *
, not necessarily allocated with the current thread.
Implementations§
source§impl ForeignLooper
impl ForeignLooper
sourcepub fn for_thread() -> Option<Self>
pub fn for_thread() -> Option<Self>
Returns the looper associated with the current thread, if any.
sourcepub unsafe fn from_ptr(ptr: NonNull<ALooper>) -> Self
pub unsafe fn from_ptr(ptr: NonNull<ALooper>) -> Self
Construct a ForeignLooper
object from the given pointer.
§Safety
By calling this function, you guarantee that the pointer is a valid, non-null pointer to an
NDK ffi::ALooper
.
sourcepub fn wake(&self)
pub fn wake(&self)
Wakes the looper. An event of Poll::Wake
will be sent.
sourcepub fn add_fd(
&self,
fd: BorrowedFd<'_>,
ident: i32,
events: FdEvent,
data: *mut c_void
) -> Result<(), LooperError>
pub fn add_fd( &self, fd: BorrowedFd<'_>, ident: i32, events: FdEvent, data: *mut c_void ) -> Result<(), LooperError>
Adds a file descriptor to be polled, without a callback.
See also the NDK docs.
§Safety
The caller should guarantee that this file descriptor stays open until it is removed via
remove_fd()
, and for however long the caller wishes to use this file
descriptor when it is returned in Poll::Event::fd
.
sourcepub fn add_fd_with_callback<F: FnMut(BorrowedFd<'_>, FdEvent) -> bool + Send>(
&self,
fd: BorrowedFd<'_>,
events: FdEvent,
callback: F
) -> Result<(), LooperError>
pub fn add_fd_with_callback<F: FnMut(BorrowedFd<'_>, FdEvent) -> bool + Send>( &self, fd: BorrowedFd<'_>, events: FdEvent, callback: F ) -> Result<(), LooperError>
Adds a file descriptor to be polled, with a callback that is invoked when any of the
FdEvent
s described in events
is triggered.
The callback receives the file descriptor it is associated with and a bitmask of the poll
events that were triggered (typically FdEvent::INPUT
). It should return true
to
continue receiving callbacks, or false
to have the callback unregistered.
See also the NDK docs.
Note that this will leak a Box
unless the callback returns false
to unregister
itself.
§Threading
This function will be called on the looper thread where and when it is polled.
For registering callbacks without Send
requirement, call the equivalent
ThreadLooper::add_fd_with_callback()
function on the Looper thread.
§Safety
The caller should guarantee that this file descriptor stays open until it is removed via
remove_fd()
or by returning false
from the callback, and for
however long the caller wishes to use this file descriptor inside and after the callback.
sourcepub fn remove_fd(&self, fd: BorrowedFd<'_>) -> Result<bool, LooperError>
pub fn remove_fd(&self, fd: BorrowedFd<'_>) -> Result<bool, LooperError>
Removes a previously added file descriptor from the looper.
Returns true
if the file descriptor was removed, false
if it was not previously
registered.
§Safety
When this method returns, it is safe to close the file descriptor since the looper will no
longer have a reference to it. However, it is possible for the callback to already be
running or for it to run one last time if the file descriptor was already signalled.
Calling code is responsible for ensuring that this case is safely handled. For example, if
the callback takes care of removing itself during its own execution either by returning 0
or by calling this method, then it can be guaranteed to not be invoked again at any later
time unless registered anew.
Note that unregistering a file descriptor with callback will leak a Box
created in
add_fd_with_callback()
. Consider returning false
from the callback instead to drop it.