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 , RawFd } ;
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) ,
@@ -907,7 +907,7 @@ pub fn unix_fd_add_full<F>(
907
907
func : F ,
908
908
) -> SourceId
909
909
where
910
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static ,
910
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static ,
911
911
{
912
912
unsafe {
913
913
from_glib ( ffi:: g_unix_fd_add_full (
@@ -939,9 +939,9 @@ where
939
939
/// This function panics if called from a different thread than the one that
940
940
/// owns the main context.
941
941
#[ doc( alias = "g_unix_fd_add_full" ) ]
942
- pub fn unix_fd_add_local < F > ( fd : RawFd , condition : IOCondition , func : F ) -> SourceId
942
+ pub fn unix_fd_add_local < F > ( fd : impl AsFd , condition : IOCondition , func : F ) -> SourceId
943
943
where
944
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + ' static ,
944
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + ' static ,
945
945
{
946
946
unsafe {
947
947
let context = MainContext :: default ( ) ;
@@ -950,7 +950,7 @@ where
950
950
. expect ( "default main context already acquired by another thread" ) ;
951
951
from_glib ( ffi:: g_unix_fd_add_full (
952
952
ffi:: G_PRIORITY_DEFAULT ,
953
- fd,
953
+ fd. as_fd ( ) . as_raw_fd ( ) ,
954
954
condition. into_glib ( ) ,
955
955
Some ( trampoline_unix_fd_local :: < F > ) ,
956
956
into_raw_unix_fd_local ( func) ,
@@ -984,7 +984,7 @@ pub fn unix_fd_add_local_full<F>(
984
984
func : F ,
985
985
) -> SourceId
986
986
where
987
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + ' static ,
987
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + ' static ,
988
988
{
989
989
unsafe {
990
990
let context = MainContext :: default ( ) ;
@@ -1230,17 +1230,17 @@ where
1230
1230
/// until it returns `ControlFlow::Break`.
1231
1231
#[ doc( alias = "g_unix_fd_source_new" ) ]
1232
1232
pub fn unix_fd_source_new < F > (
1233
- fd : RawFd ,
1233
+ fd : impl AsFd ,
1234
1234
condition : IOCondition ,
1235
1235
name : Option < & str > ,
1236
1236
priority : Priority ,
1237
1237
func : F ,
1238
1238
) -> Source
1239
1239
where
1240
- F : FnMut ( RawFd , IOCondition ) -> ControlFlow + Send + ' static ,
1240
+ F : FnMut ( BorrowedFd , IOCondition ) -> ControlFlow + Send + ' static ,
1241
1241
{
1242
1242
unsafe {
1243
- let source = ffi:: g_unix_fd_source_new ( fd, condition. into_glib ( ) ) ;
1243
+ let source = ffi:: g_unix_fd_source_new ( fd. as_fd ( ) . as_raw_fd ( ) , condition. into_glib ( ) ) ;
1244
1244
ffi:: g_source_set_callback (
1245
1245
source,
1246
1246
Some ( transmute :: <
0 commit comments