Skip to content

Commit e94afa1

Browse files
committed
unix_fd_add: Port to safe-io
1 parent c9e4122 commit e94afa1

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

glib/src/source.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
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, RawFd};
55
use std::{cell::RefCell, mem::transmute, num::NonZeroU32, time::Duration};
66

77
use crate::ffi::{self, gboolean, gpointer};
8-
#[cfg(all(not(unix), docsrs))]
9-
use libc::c_int as RawFd;
108

119
#[cfg(unix)]
1210
use 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)))]
156154
unsafe 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)))]
169168
unsafe 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)))]
182182
unsafe 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)))]
192192
unsafe 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
876876
where
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),
@@ -907,7 +907,7 @@ pub fn unix_fd_add_full<F>(
907907
func: F,
908908
) -> SourceId
909909
where
910-
F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static,
910+
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + Send + 'static,
911911
{
912912
unsafe {
913913
from_glib(ffi::g_unix_fd_add_full(
@@ -939,9 +939,9 @@ where
939939
/// This function panics if called from a different thread than the one that
940940
/// owns the main context.
941941
#[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
943943
where
944-
F: FnMut(RawFd, IOCondition) -> ControlFlow + 'static,
944+
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + 'static,
945945
{
946946
unsafe {
947947
let context = MainContext::default();
@@ -950,7 +950,7 @@ where
950950
.expect("default main context already acquired by another thread");
951951
from_glib(ffi::g_unix_fd_add_full(
952952
ffi::G_PRIORITY_DEFAULT,
953-
fd,
953+
fd.as_fd().as_raw_fd(),
954954
condition.into_glib(),
955955
Some(trampoline_unix_fd_local::<F>),
956956
into_raw_unix_fd_local(func),
@@ -984,7 +984,7 @@ pub fn unix_fd_add_local_full<F>(
984984
func: F,
985985
) -> SourceId
986986
where
987-
F: FnMut(RawFd, IOCondition) -> ControlFlow + 'static,
987+
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + 'static,
988988
{
989989
unsafe {
990990
let context = MainContext::default();
@@ -1230,17 +1230,17 @@ where
12301230
/// until it returns `ControlFlow::Break`.
12311231
#[doc(alias = "g_unix_fd_source_new")]
12321232
pub fn unix_fd_source_new<F>(
1233-
fd: RawFd,
1233+
fd: impl AsFd,
12341234
condition: IOCondition,
12351235
name: Option<&str>,
12361236
priority: Priority,
12371237
func: F,
12381238
) -> Source
12391239
where
1240-
F: FnMut(RawFd, IOCondition) -> ControlFlow + Send + 'static,
1240+
F: FnMut(BorrowedFd, IOCondition) -> ControlFlow + Send + 'static,
12411241
{
12421242
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());
12441244
ffi::g_source_set_callback(
12451245
source,
12461246
Some(transmute::<

0 commit comments

Comments
 (0)