Skip to content

Commit a40998e

Browse files
committed
unix_fd_add: Port to safe-io
1 parent ef635d2 commit a40998e

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

glib/src/source.rs

Lines changed: 26 additions & 21 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};
55
use std::{cell::RefCell, mem::transmute, num::NonZeroU32, time::Duration};
66

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

119
#[cfg(any(unix, docsrs))]
1210
use crate::IOCondition;
@@ -175,33 +173,35 @@ fn into_raw_child_watch_local<F: FnMut(Pid, i32) + 'static>(func: F) -> gpointer
175173
#[cfg(any(unix, docsrs))]
176174
#[cfg_attr(docsrs, doc(cfg(unix)))]
177175
unsafe extern "C" fn trampoline_unix_fd<
178-
F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static,
176+
F: FnMut(BorrowedFd, IOCondition) -> Continue + Send + 'static,
179177
>(
180-
fd: i32,
178+
raw_fd: i32,
181179
condition: ffi::GIOCondition,
182180
func: gpointer,
183181
) -> gboolean {
184182
let func: &RefCell<F> = &*(func as *const RefCell<F>);
183+
let fd = BorrowedFd::borrow_raw(raw_fd);
185184
(*func.borrow_mut())(fd, from_glib(condition)).into_glib()
186185
}
187186

188187
#[cfg(any(unix, docsrs))]
189188
#[cfg_attr(docsrs, doc(cfg(unix)))]
190189
unsafe extern "C" fn trampoline_unix_fd_local<
191-
F: FnMut(RawFd, IOCondition) -> Continue + 'static,
190+
F: FnMut(BorrowedFd, IOCondition) -> Continue + 'static,
192191
>(
193-
fd: i32,
192+
raw_fd: i32,
194193
condition: ffi::GIOCondition,
195194
func: gpointer,
196195
) -> gboolean {
197196
let func: &ThreadGuard<RefCell<F>> = &*(func as *const ThreadGuard<RefCell<F>>);
197+
let fd = BorrowedFd::borrow_raw(raw_fd);
198198
(*func.get_ref().borrow_mut())(fd, from_glib(condition)).into_glib()
199199
}
200200

201201
#[cfg(any(unix, docsrs))]
202202
#[cfg_attr(docsrs, doc(cfg(unix)))]
203203
unsafe extern "C" fn destroy_closure_unix_fd<
204-
F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static,
204+
F: FnMut(BorrowedFd, IOCondition) -> Continue + Send + 'static,
205205
>(
206206
ptr: gpointer,
207207
) {
@@ -211,7 +211,7 @@ unsafe extern "C" fn destroy_closure_unix_fd<
211211
#[cfg(any(unix, docsrs))]
212212
#[cfg_attr(docsrs, doc(cfg(unix)))]
213213
unsafe extern "C" fn destroy_closure_unix_fd_local<
214-
F: FnMut(RawFd, IOCondition) -> Continue + 'static,
214+
F: FnMut(BorrowedFd, IOCondition) -> Continue + 'static,
215215
>(
216216
ptr: gpointer,
217217
) {
@@ -220,7 +220,7 @@ unsafe extern "C" fn destroy_closure_unix_fd_local<
220220

221221
#[cfg(any(unix, docsrs))]
222222
#[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>(
224224
func: F,
225225
) -> gpointer {
226226
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>(
229229

230230
#[cfg(any(unix, docsrs))]
231231
#[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 {
233235
let func: Box<ThreadGuard<RefCell<F>>> = Box::new(ThreadGuard::new(RefCell::new(func)));
234236
Box::into_raw(func) as gpointer
235237
}
@@ -771,14 +773,15 @@ where
771773
/// The default main loop almost always is the main loop of the main thread.
772774
/// Thus, the closure is called on the main thread.
773775
#[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
775777
where
776-
F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static,
778+
D: AsFd,
779+
F: FnMut(BorrowedFd, IOCondition) -> Continue + Send + 'static,
777780
{
778781
unsafe {
779782
from_glib(ffi::g_unix_fd_add_full(
780783
ffi::G_PRIORITY_DEFAULT,
781-
fd,
784+
fd.as_fd().as_raw_fd(),
782785
condition.into_glib(),
783786
Some(trampoline_unix_fd::<F>),
784787
into_raw_unix_fd(func),
@@ -805,9 +808,10 @@ where
805808
/// This function panics if called from a different thread than the one that
806809
/// owns the main context.
807810
#[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
809812
where
810-
F: FnMut(RawFd, IOCondition) -> Continue + 'static,
813+
D: AsFd,
814+
F: FnMut(BorrowedFd, IOCondition) -> Continue + 'static,
811815
{
812816
unsafe {
813817
let context = MainContext::default();
@@ -816,7 +820,7 @@ where
816820
.expect("default main context already acquired by another thread");
817821
from_glib(ffi::g_unix_fd_add_full(
818822
ffi::G_PRIORITY_DEFAULT,
819-
fd,
823+
fd.as_fd().as_raw_fd(),
820824
condition.into_glib(),
821825
Some(trampoline_unix_fd_local::<F>),
822826
into_raw_unix_fd_local(func),
@@ -1052,18 +1056,19 @@ where
10521056
/// `func` will be called repeatedly while the file descriptor matches the given IO condition
10531057
/// until it returns `Continue(false)`.
10541058
#[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,
10571061
condition: IOCondition,
10581062
name: Option<&str>,
10591063
priority: Priority,
10601064
func: F,
10611065
) -> Source
10621066
where
1063-
F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static,
1067+
D: AsFd,
1068+
F: FnMut(BorrowedFd, IOCondition) -> Continue + Send + 'static,
10641069
{
10651070
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());
10671072
ffi::g_source_set_callback(
10681073
source,
10691074
Some(transmute::<

0 commit comments

Comments
 (0)