Skip to content

Commit f6e4f2b

Browse files
committed
unix_fd_add: Port to safe-io
1 parent 497b66f commit f6e4f2b

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

glib/src/source.rs

Lines changed: 27 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

3-
#[cfg(unix)]
4-
use std::os::unix::io::RawFd;
3+
#[cfg(any(unix, feature = "dox"))]
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), feature = "dox"))]
9-
use libc::c_int as RawFd;
108

119
#[cfg(any(unix, feature = "dox"))]
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, feature = "dox"))]
176174
#[cfg_attr(feature = "dox", 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, feature = "dox"))]
189188
#[cfg_attr(feature = "dox", 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, feature = "dox"))]
202202
#[cfg_attr(feature = "dox", 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, feature = "dox"))]
212212
#[cfg_attr(feature = "dox", 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, feature = "dox"))]
222222
#[cfg_attr(feature = "dox", 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, feature = "dox"))]
231231
#[cfg_attr(feature = "dox", 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),
@@ -1044,18 +1048,19 @@ where
10441048
/// `func` will be called repeatedly while the file descriptor matches the given IO condition
10451049
/// until it returns `Continue(false)`.
10461050
#[doc(alias = "g_unix_fd_source_new")]
1047-
pub fn unix_fd_source_new<F>(
1048-
fd: RawFd,
1051+
pub fn unix_fd_source_new<D, F>(
1052+
fd: D,
10491053
condition: IOCondition,
10501054
name: Option<&str>,
10511055
priority: Priority,
10521056
func: F,
10531057
) -> Source
10541058
where
1055-
F: FnMut(RawFd, IOCondition) -> Continue + Send + 'static,
1059+
D: AsFd,
1060+
F: FnMut(BorrowedFd, IOCondition) -> Continue + Send + 'static,
10561061
{
10571062
unsafe {
1058-
let source = ffi::g_unix_fd_source_new(fd, condition.into_glib());
1063+
let source = ffi::g_unix_fd_source_new(fd.as_fd().as_raw_fd(), condition.into_glib());
10591064
ffi::g_source_set_callback(
10601065
source,
10611066
Some(transmute::<

0 commit comments

Comments
 (0)