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 crate :: ffi:: { self , gboolean, gpointer} ;
8
- #[ cfg( all( not( unix) , docsrs) ) ]
9
- use libc:: c_int as RawFd ;
10
8
11
9
#[ cfg( unix) ]
12
10
use crate :: IOCondition ;
@@ -154,33 +152,35 @@ fn into_raw_child_watch_local<F: FnMut(Pid, i32) + 'static>(func: F) -> gpointer
154
152
#[ cfg( unix) ]
155
153
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
156
154
unsafe extern "C" fn trampoline_unix_fd <
157
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static ,
155
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static ,
158
156
> (
159
- fd : i32 ,
157
+ raw_fd : i32 ,
160
158
condition : ffi:: GIOCondition ,
161
159
func : gpointer ,
162
160
) -> gboolean {
163
161
let func: & RefCell < F > = & * ( func as * const RefCell < F > ) ;
162
+ let fd = BorrowedFd :: borrow_raw ( raw_fd) ;
164
163
( * func. borrow_mut ( ) ) ( fd, from_glib ( condition) ) . into_glib ( )
165
164
}
166
165
167
166
#[ cfg( unix) ]
168
167
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
169
168
unsafe extern "C" fn trampoline_unix_fd_local <
170
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + ' static ,
169
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + ' static ,
171
170
> (
172
- fd : i32 ,
171
+ raw_fd : i32 ,
173
172
condition : ffi:: GIOCondition ,
174
173
func : gpointer ,
175
174
) -> gboolean {
176
175
let func: & ThreadGuard < RefCell < F > > = & * ( func as * const ThreadGuard < RefCell < F > > ) ;
176
+ let fd = BorrowedFd :: borrow_raw ( raw_fd) ;
177
177
( * func. get_ref ( ) . borrow_mut ( ) ) ( fd, from_glib ( condition) ) . into_glib ( )
178
178
}
179
179
180
180
#[ cfg( unix) ]
181
181
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
182
182
unsafe extern "C" fn destroy_closure_unix_fd <
183
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static ,
183
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static ,
184
184
> (
185
185
ptr : gpointer ,
186
186
) {
@@ -190,7 +190,7 @@ unsafe extern "C" fn destroy_closure_unix_fd<
190
190
#[ cfg( unix) ]
191
191
#[ cfg_attr( docsrs, doc( cfg( unix) ) ) ]
192
192
unsafe extern "C" fn destroy_closure_unix_fd_local <
193
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + ' static ,
193
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + ' static ,
194
194
> (
195
195
ptr : gpointer ,
196
196
) {
@@ -199,7 +199,7 @@ unsafe extern "C" fn destroy_closure_unix_fd_local<
199
199
200
200
#[ cfg( unix) ]
201
201
#[ 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 > (
203
203
func : F ,
204
204
) -> gpointer {
205
205
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
208
208
209
209
#[ cfg( unix) ]
210
210
#[ 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 > (
212
212
func : F ,
213
213
) -> gpointer {
214
214
let func: Box < ThreadGuard < RefCell < F > > > = Box :: new ( ThreadGuard :: new ( RefCell :: new ( func) ) ) ;
@@ -872,14 +872,14 @@ where
872
872
/// The default main loop almost always is the main loop of the main thread.
873
873
/// Thus, the closure is called on the main thread.
874
874
#[ 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
876
876
where
877
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static ,
877
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static ,
878
878
{
879
879
unsafe {
880
880
from_glib ( ffi:: g_unix_fd_add_full (
881
881
ffi:: G_PRIORITY_DEFAULT ,
882
- fd,
882
+ fd. as_fd ( ) . as_raw_fd ( ) ,
883
883
condition. into_glib ( ) ,
884
884
Some ( trampoline_unix_fd :: < F > ) ,
885
885
into_raw_unix_fd ( func) ,
@@ -906,9 +906,9 @@ where
906
906
/// This function panics if called from a different thread than the one that
907
907
/// owns the main context.
908
908
#[ 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
910
910
where
911
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + ' static ,
911
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + ' static ,
912
912
{
913
913
unsafe {
914
914
let context = MainContext :: default ( ) ;
@@ -917,7 +917,7 @@ where
917
917
. expect ( "default main context already acquired by another thread" ) ;
918
918
from_glib ( ffi:: g_unix_fd_add_full (
919
919
ffi:: G_PRIORITY_DEFAULT ,
920
- fd,
920
+ fd. as_fd ( ) . as_raw_fd ( ) ,
921
921
condition. into_glib ( ) ,
922
922
Some ( trampoline_unix_fd_local :: < F > ) ,
923
923
into_raw_unix_fd_local ( func) ,
@@ -1154,17 +1154,17 @@ where
1154
1154
/// until it returns `ControlFlow::Break`.
1155
1155
#[ doc( alias = "g_unix_fd_source_new" ) ]
1156
1156
pub fn unix_fd_source_new < F > (
1157
- fd : RawFd ,
1157
+ fd : impl AsFd ,
1158
1158
condition : IOCondition ,
1159
1159
name : Option < & str > ,
1160
1160
priority : Priority ,
1161
1161
func : F ,
1162
1162
) -> Source
1163
1163
where
1164
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static ,
1164
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static ,
1165
1165
{
1166
1166
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 ( ) ) ;
1168
1168
ffi:: g_source_set_callback (
1169
1169
source,
1170
1170
Some ( transmute :: <
0 commit comments