Skip to content

unix_fd_add: Port to safe-io #1745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
52 changes: 26 additions & 26 deletions glib/src/source.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(unix)]
use std::os::unix::io::RawFd;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd};
use std::{cell::RefCell, mem::transmute, num::NonZeroU32, time::Duration};

use crate::ffi::{self, gboolean, gpointer};
#[cfg(all(not(unix), docsrs))]
use libc::c_int as RawFd;

#[cfg(unix)]
use crate::IOCondition;
Expand Down Expand Up @@ -154,33 +152,35 @@ fn into_raw_child_watch_local<F: FnMut(Pid, i32) + 'static>(func: F) -> gpointer
#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
unsafe extern "C" fn trampoline_unix_fd<
F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static,
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + Send + 'static,
>(
fd: i32,
raw_fd: i32,
condition: ffi::GIOCondition,
func: gpointer,
) -> gboolean {
let func: &RefCell<F> = &*(func as *const RefCell<F>);
let fd = BorrowedFd::borrow_raw(raw_fd);
(*func.borrow_mut())(fd, from_glib(condition)).into_glib()
}

#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
unsafe extern "C" fn trampoline_unix_fd_local<
F: FnMut(RawFd, IOCondition) -> ControlFlow + 'static,
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + 'static,
>(
fd: i32,
raw_fd: i32,
condition: ffi::GIOCondition,
func: gpointer,
) -> gboolean {
let func: &ThreadGuard<RefCell<F>> = &*(func as *const ThreadGuard<RefCell<F>>);
let fd = BorrowedFd::borrow_raw(raw_fd);
(*func.get_ref().borrow_mut())(fd, from_glib(condition)).into_glib()
}

#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
unsafe extern "C" fn destroy_closure_unix_fd<
F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static,
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + Send + 'static,
>(
ptr: gpointer,
) {
Expand All @@ -190,7 +190,7 @@ unsafe extern "C" fn destroy_closure_unix_fd<
#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
unsafe extern "C" fn destroy_closure_unix_fd_local<
F: FnMut(RawFd, IOCondition) -> ControlFlow + 'static,
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + 'static,
>(
ptr: gpointer,
) {
Expand All @@ -199,7 +199,7 @@ unsafe extern "C" fn destroy_closure_unix_fd_local<

#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
fn into_raw_unix_fd<F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static>(
fn into_raw_unix_fd<F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + Send + 'static>(
func: F,
) -> gpointer {
let func: Box<RefCell<F>> = Box::new(RefCell::new(func));
Expand All @@ -208,7 +208,7 @@ fn into_raw_unix_fd<F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static

#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
fn into_raw_unix_fd_local<F: FnMut(RawFd, IOCondition) -> ControlFlow + 'static>(
fn into_raw_unix_fd_local<F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + 'static>(
func: F,
) -> gpointer {
let func: Box<ThreadGuard<RefCell<F>>> = Box::new(ThreadGuard::new(RefCell::new(func)));
Expand Down Expand Up @@ -872,14 +872,14 @@ where
/// 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<F>(fd: RawFd, condition: IOCondition, func: F) -> SourceId
pub fn unix_fd_add<F>(fd: impl AsFd, condition: IOCondition, func: F) -> SourceId
where
F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static,
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + Send + 'static,
{
unsafe {
from_glib(ffi::g_unix_fd_add_full(
ffi::G_PRIORITY_DEFAULT,
fd,
fd.as_fd().as_raw_fd(),
condition.into_glib(),
Some(trampoline_unix_fd::<F>),
into_raw_unix_fd(func),
Expand All @@ -901,18 +901,18 @@ where
/// 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,
fd: impl AsFd,
priority: Priority,
condition: IOCondition,
func: F,
) -> SourceId
where
F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static,
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + Send + 'static,
{
unsafe {
from_glib(ffi::g_unix_fd_add_full(
priority.into_glib(),
fd,
fd.as_fd().as_raw_fd(),
condition.into_glib(),
Some(trampoline_unix_fd::<F>),
into_raw_unix_fd(func),
Expand All @@ -939,9 +939,9 @@ where
/// 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<F>(fd: RawFd, condition: IOCondition, func: F) -> SourceId
pub fn unix_fd_add_local<F>(fd: impl AsFd, condition: IOCondition, func: F) -> SourceId
where
F: FnMut(RawFd, IOCondition) -> ControlFlow + 'static,
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + 'static,
{
unsafe {
let context = MainContext::default();
Expand All @@ -950,7 +950,7 @@ where
.expect("default main context already acquired by another thread");
from_glib(ffi::g_unix_fd_add_full(
ffi::G_PRIORITY_DEFAULT,
fd,
fd.as_fd().as_raw_fd(),
condition.into_glib(),
Some(trampoline_unix_fd_local::<F>),
into_raw_unix_fd_local(func),
Expand Down Expand Up @@ -978,13 +978,13 @@ where
/// owns the main context.
#[doc(alias = "g_unix_fd_add_full")]
pub fn unix_fd_add_local_full<F>(
fd: RawFd,
fd: impl AsFd,
priority: Priority,
condition: IOCondition,
func: F,
) -> SourceId
where
F: FnMut(RawFd, IOCondition) -> ControlFlow + 'static,
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + 'static,
{
unsafe {
let context = MainContext::default();
Expand All @@ -993,7 +993,7 @@ where
.expect("default main context already acquired by another thread");
from_glib(ffi::g_unix_fd_add_full(
priority.into_glib(),
fd,
fd.as_fd().as_raw_fd(),
condition.into_glib(),
Some(trampoline_unix_fd_local::<F>),
into_raw_unix_fd_local(func),
Expand Down Expand Up @@ -1230,17 +1230,17 @@ where
/// until it returns `ControlFlow::Break`.
#[doc(alias = "g_unix_fd_source_new")]
pub fn unix_fd_source_new<F>(
fd: RawFd,
fd: impl AsFd,
condition: IOCondition,
name: Option<&str>,
priority: Priority,
func: F,
) -> Source
where
F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static,
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + Send + 'static,
{
unsafe {
let source = ffi::g_unix_fd_source_new(fd, condition.into_glib());
let source = ffi::g_unix_fd_source_new(fd.as_fd().as_raw_fd(), condition.into_glib());
ffi::g_source_set_callback(
source,
Some(transmute::<
Expand Down
Loading