Skip to content
Merged
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
76 changes: 76 additions & 0 deletions glib/src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,39 @@ where
}
}

#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
// rustdoc-stripper-ignore-next
/// Adds a closure to be called by the main loop the returned `Source` is attached to whenever a
/// UNIX file descriptor reaches the given IO condition.
///
/// `func` will be called repeatedly with `priority` while the file descriptor matches the given IO condition
/// until it returns `ControlFlow::Break`.
///
/// The default main loop almost always is the main loop of the main thread.
/// Thus, the closure is called on the main thread.
#[doc(alias = "g_unix_fd_add_full")]
pub fn unix_fd_add_full<F>(
fd: RawFd,
priority: Priority,
condition: IOCondition,
func: F,
) -> SourceId
where
F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static,
{
unsafe {
from_glib(ffi::g_unix_fd_add_full(
priority.into_glib(),
fd,
condition.into_glib(),
Some(trampoline_unix_fd::<F>),
into_raw_unix_fd(func),
Some(destroy_closure_unix_fd::<F>),
))
}
}

#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
// rustdoc-stripper-ignore-next
Expand Down Expand Up @@ -926,6 +959,49 @@ where
}
}

#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
// rustdoc-stripper-ignore-next
/// Adds a closure to be called by the main loop the returned `Source` is attached to whenever a
/// UNIX file descriptor reaches the given IO condition.
///
/// `func` will be called repeatedly with `priority` while the file descriptor matches the given IO condition
/// until it returns `ControlFlow::Break`.
///
/// The default main loop almost always is the main loop of the main thread.
/// Thus, the closure is called on the main thread.
///
/// Different to `unix_fd_add()`, this does not require `func` to be
/// `Send` but can only be called from the thread that owns the main context.
///
/// This function panics if called from a different thread than the one that
/// owns the main context.
#[doc(alias = "g_unix_fd_add_full")]
pub fn unix_fd_add_local_full<F>(
fd: RawFd,
priority: Priority,
condition: IOCondition,
func: F,
) -> SourceId
where
F: FnMut(RawFd, IOCondition) -> ControlFlow + 'static,
{
unsafe {
let context = MainContext::default();
let _acquire = context
.acquire()
.expect("default main context already acquired by another thread");
from_glib(ffi::g_unix_fd_add_full(
priority.into_glib(),
fd,
condition.into_glib(),
Some(trampoline_unix_fd_local::<F>),
into_raw_unix_fd_local(func),
Some(destroy_closure_unix_fd_local::<F>),
))
}
}

// rustdoc-stripper-ignore-next
/// The priority of sources
#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
Expand Down
Loading