1
1
// Take a look at the license at the top of the repository in the LICENSE file.
2
2
3
3
#[ cfg( unix) ]
4
- use std:: os:: unix:: io:: RawFd ;
4
+ use std:: os:: unix:: io:: { AsFd , AsRawFd , BorrowedFd } ;
5
5
use std:: { cell:: RefCell , mem:: transmute, num:: NonZeroU32 , time:: Duration } ;
6
6
7
7
use ffi:: { self , gboolean, gpointer} ;
8
- #[ cfg( all( not( unix) , docsrs) ) ]
9
- use libc:: c_int as RawFd ;
10
8
11
9
#[ cfg( any( unix, docsrs) ) ]
12
10
use crate :: IOCondition ;
@@ -175,33 +173,35 @@ fn into_raw_child_watch_local<F: FnMut(Pid, i32) + 'static>(func: F) -> gpointer
175
173
#[ cfg( any( unix, docsrs) ) ]
176
174
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
177
175
unsafe extern "C" fn trampoline_unix_fd <
178
- F : FnMut ( RawFd , IOCondition ) -> Continue + Send + ' static ,
176
+ F : FnMut ( BorrowedFd , IOCondition ) -> Continue + Send + ' static ,
179
177
> (
180
- fd : i32 ,
178
+ raw_fd : i32 ,
181
179
condition : ffi:: GIOCondition ,
182
180
func : gpointer ,
183
181
) -> gboolean {
184
182
let func: & RefCell < F > = & * ( func as * const RefCell < F > ) ;
183
+ let fd = BorrowedFd :: borrow_raw ( raw_fd) ;
185
184
( * func. borrow_mut ( ) ) ( fd, from_glib ( condition) ) . into_glib ( )
186
185
}
187
186
188
187
#[ cfg( any( unix, docsrs) ) ]
189
188
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
190
189
unsafe extern "C" fn trampoline_unix_fd_local <
191
- F : FnMut ( RawFd , IOCondition ) -> Continue + ' static ,
190
+ F : FnMut ( BorrowedFd , IOCondition ) -> Continue + ' static ,
192
191
> (
193
- fd : i32 ,
192
+ raw_fd : i32 ,
194
193
condition : ffi:: GIOCondition ,
195
194
func : gpointer ,
196
195
) -> gboolean {
197
196
let func: & ThreadGuard < RefCell < F > > = & * ( func as * const ThreadGuard < RefCell < F > > ) ;
197
+ let fd = BorrowedFd :: borrow_raw ( raw_fd) ;
198
198
( * func. get_ref ( ) . borrow_mut ( ) ) ( fd, from_glib ( condition) ) . into_glib ( )
199
199
}
200
200
201
201
#[ cfg( any( unix, docsrs) ) ]
202
202
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
203
203
unsafe extern "C" fn destroy_closure_unix_fd <
204
- F : FnMut ( RawFd , IOCondition ) -> Continue + Send + ' static ,
204
+ F : FnMut ( BorrowedFd , IOCondition ) -> Continue + Send + ' static ,
205
205
> (
206
206
ptr : gpointer ,
207
207
) {
@@ -211,7 +211,7 @@ unsafe extern "C" fn destroy_closure_unix_fd<
211
211
#[ cfg( any( unix, docsrs) ) ]
212
212
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
213
213
unsafe extern "C" fn destroy_closure_unix_fd_local <
214
- F : FnMut ( RawFd , IOCondition ) -> Continue + ' static ,
214
+ F : FnMut ( BorrowedFd , IOCondition ) -> Continue + ' static ,
215
215
> (
216
216
ptr : gpointer ,
217
217
) {
@@ -220,7 +220,7 @@ unsafe extern "C" fn destroy_closure_unix_fd_local<
220
220
221
221
#[ cfg( any( unix, docsrs) ) ]
222
222
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
223
- fn into_raw_unix_fd < F : FnMut ( RawFd , IOCondition ) -> Continue + Send + ' static > (
223
+ fn into_raw_unix_fd < F : FnMut ( BorrowedFd , IOCondition ) -> Continue + Send + ' static > (
224
224
func : F ,
225
225
) -> gpointer {
226
226
let func: Box < RefCell < F > > = Box :: new ( RefCell :: new ( func) ) ;
@@ -229,7 +229,9 @@ fn into_raw_unix_fd<F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static>(
229
229
230
230
#[ cfg( any( unix, docsrs) ) ]
231
231
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
232
- fn into_raw_unix_fd_local < F : FnMut ( RawFd , IOCondition ) -> Continue + ' static > ( func : F ) -> gpointer {
232
+ fn into_raw_unix_fd_local < D : AsFd , F : FnMut ( D , IOCondition ) -> Continue + ' static > (
233
+ func : F ,
234
+ ) -> gpointer {
233
235
let func: Box < ThreadGuard < RefCell < F > > > = Box :: new ( ThreadGuard :: new ( RefCell :: new ( func) ) ) ;
234
236
Box :: into_raw ( func) as gpointer
235
237
}
@@ -771,14 +773,15 @@ where
771
773
/// The default main loop almost always is the main loop of the main thread.
772
774
/// Thus, the closure is called on the main thread.
773
775
#[ doc( alias = "g_unix_fd_add_full" ) ]
774
- pub fn unix_fd_add < F > ( fd : RawFd , condition : IOCondition , func : F ) -> SourceId
776
+ pub fn unix_fd_add < D , F > ( fd : D , condition : IOCondition , func : F ) -> SourceId
775
777
where
776
- F : FnMut ( RawFd , IOCondition ) -> Continue + Send + ' static ,
778
+ D : AsFd ,
779
+ F : FnMut ( BorrowedFd , IOCondition ) -> Continue + Send + ' static ,
777
780
{
778
781
unsafe {
779
782
from_glib ( ffi:: g_unix_fd_add_full (
780
783
ffi:: G_PRIORITY_DEFAULT ,
781
- fd,
784
+ fd. as_fd ( ) . as_raw_fd ( ) ,
782
785
condition. into_glib ( ) ,
783
786
Some ( trampoline_unix_fd :: < F > ) ,
784
787
into_raw_unix_fd ( func) ,
@@ -805,9 +808,10 @@ where
805
808
/// This function panics if called from a different thread than the one that
806
809
/// owns the main context.
807
810
#[ doc( alias = "g_unix_fd_add_full" ) ]
808
- pub fn unix_fd_add_local < F > ( fd : RawFd , condition : IOCondition , func : F ) -> SourceId
811
+ pub fn unix_fd_add_local < D , F > ( fd : D , condition : IOCondition , func : F ) -> SourceId
809
812
where
810
- F : FnMut ( RawFd , IOCondition ) -> Continue + ' static ,
813
+ D : AsFd ,
814
+ F : FnMut ( BorrowedFd , IOCondition ) -> Continue + ' static ,
811
815
{
812
816
unsafe {
813
817
let context = MainContext :: default ( ) ;
@@ -816,7 +820,7 @@ where
816
820
. expect ( "default main context already acquired by another thread" ) ;
817
821
from_glib ( ffi:: g_unix_fd_add_full (
818
822
ffi:: G_PRIORITY_DEFAULT ,
819
- fd,
823
+ fd. as_fd ( ) . as_raw_fd ( ) ,
820
824
condition. into_glib ( ) ,
821
825
Some ( trampoline_unix_fd_local :: < F > ) ,
822
826
into_raw_unix_fd_local ( func) ,
@@ -1052,18 +1056,19 @@ where
1052
1056
/// `func` will be called repeatedly while the file descriptor matches the given IO condition
1053
1057
/// until it returns `Continue(false)`.
1054
1058
#[ doc( alias = "g_unix_fd_source_new" ) ]
1055
- pub fn unix_fd_source_new < F > (
1056
- fd : RawFd ,
1059
+ pub fn unix_fd_source_new < D , F > (
1060
+ fd : D ,
1057
1061
condition : IOCondition ,
1058
1062
name : Option < & str > ,
1059
1063
priority : Priority ,
1060
1064
func : F ,
1061
1065
) -> Source
1062
1066
where
1063
- F : FnMut ( RawFd , IOCondition ) -> Continue + Send + ' static ,
1067
+ D : AsFd ,
1068
+ F : FnMut ( BorrowedFd , IOCondition ) -> Continue + Send + ' static ,
1064
1069
{
1065
1070
unsafe {
1066
- let source = ffi:: g_unix_fd_source_new ( fd, condition. into_glib ( ) ) ;
1071
+ let source = ffi:: g_unix_fd_source_new ( fd. as_fd ( ) . as_raw_fd ( ) , condition. into_glib ( ) ) ;
1067
1072
ffi:: g_source_set_callback (
1068
1073
source,
1069
1074
Some ( transmute :: <
0 commit comments