Skip to content

Commit dfd1468

Browse files
authored
Merge pull request #1696 from A6GibKm/safe-io-pt2
Port some functions to safe-io: pt II
2 parents 03f7eb2 + 046d4f5 commit dfd1468

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

gio/src/subprocess_launcher.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

33
#[cfg(any(unix, all(docsrs, unix)))]
4-
use std::os::unix::io::IntoRawFd;
4+
use std::os::unix::io::{AsFd, AsRawFd, IntoRawFd, OwnedFd};
55

66
use glib::translate::*;
77

@@ -26,40 +26,41 @@ impl SubprocessLauncher {
2626
#[cfg(unix)]
2727
#[cfg_attr(docsrs, doc(cfg(unix)))]
2828
#[doc(alias = "g_subprocess_launcher_take_fd")]
29-
pub fn take_fd(&self, source_fd: impl IntoRawFd, target_fd: impl IntoRawFd) {
29+
pub fn take_fd(&self, source_fd: OwnedFd, target_fd: impl AsFd) {
30+
let source_raw_fd = source_fd.into_raw_fd();
31+
let target_raw_fd = target_fd.as_fd().as_raw_fd();
3032
unsafe {
31-
ffi::g_subprocess_launcher_take_fd(
32-
self.to_glib_none().0,
33-
source_fd.into_raw_fd(),
34-
target_fd.into_raw_fd(),
35-
);
33+
ffi::g_subprocess_launcher_take_fd(self.to_glib_none().0, source_raw_fd, target_raw_fd);
3634
}
3735
}
3836

3937
#[cfg(unix)]
4038
#[cfg_attr(docsrs, doc(cfg(unix)))]
4139
#[doc(alias = "g_subprocess_launcher_take_stderr_fd")]
42-
pub fn take_stderr_fd(&self, fd: impl IntoRawFd) {
40+
pub fn take_stderr_fd(&self, fd: Option<OwnedFd>) {
4341
unsafe {
44-
ffi::g_subprocess_launcher_take_stderr_fd(self.to_glib_none().0, fd.into_raw_fd());
42+
let raw_fd = fd.map_or(-1, |fd| fd.into_raw_fd());
43+
ffi::g_subprocess_launcher_take_stderr_fd(self.to_glib_none().0, raw_fd);
4544
}
4645
}
4746

4847
#[cfg(unix)]
4948
#[cfg_attr(docsrs, doc(cfg(unix)))]
5049
#[doc(alias = "g_subprocess_launcher_take_stdin_fd")]
51-
pub fn take_stdin_fd(&self, fd: impl IntoRawFd) {
50+
pub fn take_stdin_fd(&self, fd: Option<OwnedFd>) {
51+
let raw_fd = fd.map_or(-1, |fd| fd.into_raw_fd());
5252
unsafe {
53-
ffi::g_subprocess_launcher_take_stdin_fd(self.to_glib_none().0, fd.into_raw_fd());
53+
ffi::g_subprocess_launcher_take_stdin_fd(self.to_glib_none().0, raw_fd);
5454
}
5555
}
5656

5757
#[cfg(unix)]
5858
#[cfg_attr(docsrs, doc(cfg(unix)))]
5959
#[doc(alias = "g_subprocess_launcher_take_stdout_fd")]
60-
pub fn take_stdout_fd(&self, fd: impl IntoRawFd) {
60+
pub fn take_stdout_fd(&self, fd: Option<OwnedFd>) {
61+
let raw_fd = fd.map_or(-1, |fd| fd.into_raw_fd());
6162
unsafe {
62-
ffi::g_subprocess_launcher_take_stdout_fd(self.to_glib_none().0, fd.into_raw_fd());
63+
ffi::g_subprocess_launcher_take_stdout_fd(self.to_glib_none().0, raw_fd);
6364
}
6465
}
6566
}

gio/src/unix_fd_list.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
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::{AsRawFd, IntoRawFd, RawFd};
4+
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
55
use std::{mem, ptr};
66

77
use glib::{prelude::*, translate::*};
88
#[cfg(all(not(unix), docsrs))]
9-
use socket::{AsRawFd, IntoRawFd, RawFd};
9+
use socket::{AsFd, AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
1010

1111
use crate::{ffi, UnixFDList};
1212

@@ -25,12 +25,12 @@ impl UnixFDList {
2525

2626
pub trait UnixFDListExtManual: IsA<UnixFDList> + Sized {
2727
#[doc(alias = "g_unix_fd_list_append")]
28-
fn append<T: AsRawFd>(&self, fd: T) -> Result<i32, glib::Error> {
28+
fn append(&self, fd: impl AsFd) -> Result<i32, glib::Error> {
2929
unsafe {
3030
let mut error = ptr::null_mut();
3131
let ret = ffi::g_unix_fd_list_append(
3232
self.as_ref().to_glib_none().0,
33-
fd.as_raw_fd(),
33+
fd.as_fd().as_raw_fd(),
3434
&mut error,
3535
);
3636
if error.is_null() {
@@ -42,12 +42,14 @@ pub trait UnixFDListExtManual: IsA<UnixFDList> + Sized {
4242
}
4343

4444
#[doc(alias = "g_unix_fd_list_get")]
45-
fn get(&self, index_: i32) -> Result<RawFd, glib::Error> {
45+
fn get(&self, index_: i32) -> Result<OwnedFd, glib::Error> {
4646
unsafe {
4747
let mut error = ptr::null_mut();
48-
let ret = ffi::g_unix_fd_list_get(self.as_ref().to_glib_none().0, index_, &mut error);
48+
let raw_fd =
49+
ffi::g_unix_fd_list_get(self.as_ref().to_glib_none().0, index_, &mut error);
4950
if error.is_null() {
50-
Ok(ret)
51+
let fd = OwnedFd::from_raw_fd(raw_fd);
52+
Ok(fd)
5153
} else {
5254
Err(from_glib_full(error))
5355
}

glib/src/functions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::mem;
99
#[cfg(feature = "v2_58")]
1010
use std::os::unix::io::{AsFd, AsRawFd};
1111
#[cfg(not(windows))]
12-
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
12+
use std::os::unix::io::{FromRawFd, OwnedFd, RawFd};
1313
use std::ptr;
1414

1515
// #[cfg(windows)]
@@ -261,7 +261,7 @@ pub fn unix_open_pipe(flags: i32) -> Result<(RawFd, RawFd), Error> {
261261
#[doc(alias = "g_file_open_tmp")]
262262
pub fn file_open_tmp(
263263
tmpl: Option<impl AsRef<std::path::Path>>,
264-
) -> Result<(RawFd, std::path::PathBuf), crate::Error> {
264+
) -> Result<(OwnedFd, std::path::PathBuf), crate::Error> {
265265
unsafe {
266266
let mut name_used = ptr::null_mut();
267267
let mut error = ptr::null_mut();
@@ -271,7 +271,7 @@ pub fn file_open_tmp(
271271
&mut error,
272272
);
273273
if error.is_null() {
274-
Ok((ret.into_raw_fd(), from_glib_full(name_used)))
274+
Ok((OwnedFd::from_raw_fd(ret), from_glib_full(name_used)))
275275
} else {
276276
Err(from_glib_full(error))
277277
}

0 commit comments

Comments
 (0)