Skip to content

Commit 351c353

Browse files
committed
Port spawn_async_with_fds to safe-io
1 parent 7595f71 commit 351c353

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

glib/src/functions.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::boxed::Box as Box_;
77
use std::mem;
88
#[cfg(not(windows))]
99
#[cfg(feature = "v2_58")]
10-
use std::os::unix::io::AsRawFd;
10+
use std::os::unix::io::{AsFd, AsRawFd};
1111
#[cfg(not(windows))]
1212
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
1313
use std::ptr;
@@ -24,15 +24,15 @@ use crate::{Error, Pid, SpawnFlags};
2424
#[cfg_attr(docsrs, doc(cfg(all(feature = "v2_58", not(windows)))))]
2525
#[allow(clippy::too_many_arguments)]
2626
#[doc(alias = "g_spawn_async_with_fds")]
27-
pub fn spawn_async_with_fds<P: AsRef<std::path::Path>, T: AsRawFd, U: AsRawFd, V: AsRawFd>(
27+
pub fn spawn_async_with_fds<P: AsRef<std::path::Path>>(
2828
working_directory: P,
2929
argv: &[&str],
3030
envp: &[&str],
3131
flags: SpawnFlags,
3232
child_setup: Option<Box_<dyn FnOnce() + 'static>>,
33-
stdin_fd: T,
34-
stdout_fd: U,
35-
stderr_fd: V,
33+
stdin_fd: Option<impl AsFd>,
34+
stdout_fd: Option<impl AsFd>,
35+
stderr_fd: Option<impl AsFd>,
3636
) -> Result<Pid, Error> {
3737
let child_setup_data: Box_<Option<Box_<dyn FnOnce() + 'static>>> = Box_::new(child_setup);
3838
unsafe extern "C" fn child_setup_func(user_data: ffi::gpointer) {
@@ -47,6 +47,9 @@ pub fn spawn_async_with_fds<P: AsRef<std::path::Path>, T: AsRawFd, U: AsRawFd, V
4747
None
4848
};
4949
let super_callback0: Box_<Option<Box_<dyn FnOnce() + 'static>>> = child_setup_data;
50+
let stdin_raw_fd = stdin_fd.map_or(-1, |fd| fd.as_fd().as_raw_fd());
51+
let stdout_raw_fd = stdout_fd.map_or(-1, |fd| fd.as_fd().as_raw_fd());
52+
let stderr_raw_fd = stderr_fd.map_or(-1, |fd| fd.as_fd().as_raw_fd());
5053
unsafe {
5154
let mut child_pid = mem::MaybeUninit::uninit();
5255
let mut error = ptr::null_mut();
@@ -58,9 +61,9 @@ pub fn spawn_async_with_fds<P: AsRef<std::path::Path>, T: AsRawFd, U: AsRawFd, V
5861
child_setup,
5962
Box_::into_raw(super_callback0) as *mut _,
6063
child_pid.as_mut_ptr(),
61-
stdin_fd.as_raw_fd(),
62-
stdout_fd.as_raw_fd(),
63-
stderr_fd.as_raw_fd(),
64+
stdin_raw_fd,
65+
stdout_raw_fd,
66+
stderr_raw_fd,
6467
&mut error,
6568
);
6669
let child_pid = from_glib(child_pid.assume_init());

0 commit comments

Comments
 (0)