11// Take a look at the license at the top of the repository in the LICENSE file.
22
33#[ cfg( unix) ]
4- use std:: os:: unix:: io:: RawFd ;
4+ use std:: os:: unix:: io:: { AsFd , AsRawFd , BorrowedFd } ;
55use std:: { cell:: RefCell , mem:: transmute, num:: NonZeroU32 , time:: Duration } ;
66
77use crate :: ffi:: { self , gboolean, gpointer} ;
8- #[ cfg( all( not( unix) , docsrs) ) ]
9- use libc:: c_int as RawFd ;
108
119#[ cfg( unix) ]
1210use crate :: IOCondition ;
@@ -154,33 +152,35 @@ fn into_raw_child_watch_local<F: FnMut(Pid, i32) + 'static>(func: F) -> gpointer
154152#[ cfg( unix) ]
155153#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
156154unsafe extern "C" fn trampoline_unix_fd <
157- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static ,
155+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static ,
158156> (
159- fd : i32 ,
157+ raw_fd : i32 ,
160158 condition : ffi:: GIOCondition ,
161159 func : gpointer ,
162160) -> gboolean {
163161 let func: & RefCell < F > = & * ( func as * const RefCell < F > ) ;
162+ let fd = BorrowedFd :: borrow_raw ( raw_fd) ;
164163 ( * func. borrow_mut ( ) ) ( fd, from_glib ( condition) ) . into_glib ( )
165164}
166165
167166#[ cfg( unix) ]
168167#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
169168unsafe extern "C" fn trampoline_unix_fd_local <
170- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + ' static ,
169+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + ' static ,
171170> (
172- fd : i32 ,
171+ raw_fd : i32 ,
173172 condition : ffi:: GIOCondition ,
174173 func : gpointer ,
175174) -> gboolean {
176175 let func: & ThreadGuard < RefCell < F > > = & * ( func as * const ThreadGuard < RefCell < F > > ) ;
176+ let fd = BorrowedFd :: borrow_raw ( raw_fd) ;
177177 ( * func. get_ref ( ) . borrow_mut ( ) ) ( fd, from_glib ( condition) ) . into_glib ( )
178178}
179179
180180#[ cfg( unix) ]
181181#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
182182unsafe extern "C" fn destroy_closure_unix_fd <
183- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static ,
183+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static ,
184184> (
185185 ptr : gpointer ,
186186) {
@@ -190,7 +190,7 @@ unsafe extern "C" fn destroy_closure_unix_fd<
190190#[ cfg( unix) ]
191191#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
192192unsafe extern "C" fn destroy_closure_unix_fd_local <
193- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + ' static ,
193+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + ' static ,
194194> (
195195 ptr : gpointer ,
196196) {
@@ -199,7 +199,7 @@ unsafe extern "C" fn destroy_closure_unix_fd_local<
199199
200200#[ cfg( unix) ]
201201#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
202- fn into_raw_unix_fd < F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static > (
202+ fn into_raw_unix_fd < F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static > (
203203 func : F ,
204204) -> gpointer {
205205 let func: Box < RefCell < F > > = Box :: new ( RefCell :: new ( func) ) ;
@@ -208,7 +208,7 @@ fn into_raw_unix_fd<F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static
208208
209209#[ cfg( unix) ]
210210#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
211- fn into_raw_unix_fd_local < F : FnMut ( RawFd , IOCondition ) -> ControlFlow + ' static > (
211+ fn into_raw_unix_fd_local < F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + ' static > (
212212 func : F ,
213213) -> gpointer {
214214 let func: Box < ThreadGuard < RefCell < F > > > = Box :: new ( ThreadGuard :: new ( RefCell :: new ( func) ) ) ;
@@ -872,14 +872,14 @@ where
872872/// The default main loop almost always is the main loop of the main thread.
873873/// Thus, the closure is called on the main thread.
874874#[ doc( alias = "g_unix_fd_add_full" ) ]
875- pub fn unix_fd_add < F > ( fd : RawFd , condition : IOCondition , func : F ) -> SourceId
875+ pub fn unix_fd_add < F > ( fd : impl AsFd , condition : IOCondition , func : F ) -> SourceId
876876where
877- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static ,
877+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static ,
878878{
879879 unsafe {
880880 from_glib ( ffi:: g_unix_fd_add_full (
881881 ffi:: G_PRIORITY_DEFAULT ,
882- fd,
882+ fd. as_fd ( ) . as_raw_fd ( ) ,
883883 condition. into_glib ( ) ,
884884 Some ( trampoline_unix_fd :: < F > ) ,
885885 into_raw_unix_fd ( func) ,
@@ -906,9 +906,9 @@ where
906906/// This function panics if called from a different thread than the one that
907907/// owns the main context.
908908#[ doc( alias = "g_unix_fd_add_full" ) ]
909- pub fn unix_fd_add_local < F > ( fd : RawFd , condition : IOCondition , func : F ) -> SourceId
909+ pub fn unix_fd_add_local < F > ( fd : impl AsFd , condition : IOCondition , func : F ) -> SourceId
910910where
911- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + ' static ,
911+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + ' static ,
912912{
913913 unsafe {
914914 let context = MainContext :: default ( ) ;
@@ -917,7 +917,7 @@ where
917917 . expect ( "default main context already acquired by another thread" ) ;
918918 from_glib ( ffi:: g_unix_fd_add_full (
919919 ffi:: G_PRIORITY_DEFAULT ,
920- fd,
920+ fd. as_fd ( ) . as_raw_fd ( ) ,
921921 condition. into_glib ( ) ,
922922 Some ( trampoline_unix_fd_local :: < F > ) ,
923923 into_raw_unix_fd_local ( func) ,
@@ -1154,17 +1154,17 @@ where
11541154/// until it returns `ControlFlow::Break`.
11551155#[ doc( alias = "g_unix_fd_source_new" ) ]
11561156pub fn unix_fd_source_new < F > (
1157- fd : RawFd ,
1157+ fd : impl AsFd ,
11581158 condition : IOCondition ,
11591159 name : Option < & str > ,
11601160 priority : Priority ,
11611161 func : F ,
11621162) -> Source
11631163where
1164- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static ,
1164+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static ,
11651165{
11661166 unsafe {
1167- let source = ffi:: g_unix_fd_source_new ( fd, condition. into_glib ( ) ) ;
1167+ let source = ffi:: g_unix_fd_source_new ( fd. as_fd ( ) . as_raw_fd ( ) , condition. into_glib ( ) ) ;
11681168 ffi:: g_source_set_callback (
11691169 source,
11701170 Some ( transmute :: <
0 commit comments