diff --git a/glib/src/source.rs b/glib/src/source.rs index b7702fa8d45a..8ab367b413e6 100644 --- a/glib/src/source.rs +++ b/glib/src/source.rs @@ -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; @@ -154,33 +152,35 @@ fn into_raw_child_watch_local(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 = &*(func as *const RefCell); + 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> = &*(func as *const ThreadGuard>); + 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, ) { @@ -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, ) { @@ -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 ControlFlow + Send + 'static>( +fn into_raw_unix_fd ControlFlow + Send + 'static>( func: F, ) -> gpointer { let func: Box> = Box::new(RefCell::new(func)); @@ -208,7 +208,7 @@ fn into_raw_unix_fd ControlFlow + Send + 'static #[cfg(unix)] #[cfg_attr(docsrs, doc(cfg(unix)))] -fn into_raw_unix_fd_local ControlFlow + 'static>( +fn into_raw_unix_fd_local ControlFlow + 'static>( func: F, ) -> gpointer { let func: Box>> = Box::new(ThreadGuard::new(RefCell::new(func))); @@ -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(fd: RawFd, condition: IOCondition, func: F) -> SourceId +pub fn unix_fd_add(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::), into_raw_unix_fd(func), @@ -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( - 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::), into_raw_unix_fd(func), @@ -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(fd: RawFd, condition: IOCondition, func: F) -> SourceId +pub fn unix_fd_add_local(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(); @@ -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::), into_raw_unix_fd_local(func), @@ -978,13 +978,13 @@ where /// owns the main context. #[doc(alias = "g_unix_fd_add_full")] pub fn unix_fd_add_local_full( - 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(); @@ -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::), into_raw_unix_fd_local(func), @@ -1230,17 +1230,17 @@ where /// until it returns `ControlFlow::Break`. #[doc(alias = "g_unix_fd_source_new")] pub fn unix_fd_source_new( - 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::<