Skip to content

Commit f5849f1

Browse files
committed
Use safe-io
For the from_owned_fd it might make sense to implement From or replace the unsafe from_fd/take_fd methods.
1 parent 5ed17a8 commit f5849f1

File tree

6 files changed

+51
-49
lines changed

6 files changed

+51
-49
lines changed

gio/src/desktop_app_info.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#[cfg(all(unix, feature = "v2_58"))]
44
use std::boxed::Box as Box_;
55
#[cfg(all(unix, feature = "v2_58"))]
6-
use std::os::unix::io::AsRawFd;
6+
use std::os::unix::io::{AsFd, AsRawFd};
77
#[cfg(all(unix, feature = "v2_58"))]
88
use std::ptr;
99

@@ -54,21 +54,16 @@ mod sealed {
5454
pub trait DesktopAppInfoExtManual: sealed::Sealed + IsA<DesktopAppInfo> {
5555
#[cfg_attr(docsrs, doc(cfg(all(feature = "v2_58", unix))))]
5656
#[doc(alias = "g_desktop_app_info_launch_uris_as_manager_with_fds")]
57-
fn launch_uris_as_manager_with_fds<
58-
P: IsA<AppLaunchContext>,
59-
T: AsRawFd,
60-
U: AsRawFd,
61-
V: AsRawFd,
62-
>(
57+
fn launch_uris_as_manager_with_fds<P: IsA<AppLaunchContext>>(
6358
&self,
6459
uris: &[&str],
6560
launch_context: Option<&P>,
6661
spawn_flags: glib::SpawnFlags,
6762
user_setup: Option<Box_<dyn FnOnce() + 'static>>,
6863
pid_callback: Option<&mut dyn (FnMut(&DesktopAppInfo, glib::Pid))>,
69-
stdin_fd: &mut T,
70-
stdout_fd: &mut U,
71-
stderr_fd: &mut V,
64+
stdin_fd: Option<impl AsFd>,
65+
stdout_fd: Option<impl AsFd>,
66+
stderr_fd: Option<impl AsFd>,
7267
) -> Result<(), Error> {
7368
let user_setup_data: Box_<Option<Box_<dyn FnOnce() + 'static>>> = Box_::new(user_setup);
7469
unsafe extern "C" fn user_setup_func(user_data: glib::ffi::gpointer) {
@@ -105,6 +100,10 @@ pub trait DesktopAppInfoExtManual: sealed::Sealed + IsA<DesktopAppInfo> {
105100
let super_callback0: Box_<Option<Box_<dyn FnOnce() + 'static>>> = user_setup_data;
106101
let super_callback1: &Option<&mut dyn (FnMut(&DesktopAppInfo, glib::Pid))> =
107102
&pid_callback_data;
103+
104+
let stdin_raw_fd = stdin_fd.map_or(-1, |fd| fd.as_fd().as_raw_fd());
105+
let stdout_raw_fd = stdout_fd.map_or(-1, |fd| fd.as_fd().as_raw_fd());
106+
let stderr_raw_fd = stderr_fd.map_or(-1, |fd| fd.as_fd().as_raw_fd());
108107
unsafe {
109108
let mut error = ptr::null_mut();
110109
let _ = ffi::g_desktop_app_info_launch_uris_as_manager_with_fds(
@@ -116,9 +115,9 @@ pub trait DesktopAppInfoExtManual: sealed::Sealed + IsA<DesktopAppInfo> {
116115
Box_::into_raw(super_callback0) as *mut _,
117116
pid_callback,
118117
super_callback1 as *const _ as *mut _,
119-
stdin_fd.as_raw_fd(),
120-
stdout_fd.as_raw_fd(),
121-
stderr_fd.as_raw_fd(),
118+
stdin_raw_fd,
119+
stdout_raw_fd,
120+
stderr_raw_fd,
122121
&mut error,
123122
);
124123
if error.is_null() {

gio/src/socket.rs

Lines changed: 9 additions & 7 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(unix)]
4-
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
4+
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
55
#[cfg(windows)]
66
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
77
#[cfg(feature = "v2_60")]
@@ -19,14 +19,16 @@ impl Socket {
1919
#[cfg(unix)]
2020
#[cfg_attr(docsrs, doc(cfg(unix)))]
2121
#[allow(clippy::missing_safety_doc)]
22-
pub unsafe fn from_fd(fd: impl IntoRawFd) -> Result<Socket, glib::Error> {
22+
pub fn from_fd(fd: OwnedFd) -> Result<Socket, glib::Error> {
2323
let fd = fd.into_raw_fd();
2424
let mut error = ptr::null_mut();
25-
let ret = ffi::g_socket_new_from_fd(fd, &mut error);
26-
if error.is_null() {
27-
Ok(from_glib_full(ret))
28-
} else {
29-
Err(from_glib_full(error))
25+
unsafe {
26+
let ret = ffi::g_socket_new_from_fd(fd, &mut error);
27+
if error.is_null() {
28+
Ok(from_glib_full(ret))
29+
} else {
30+
Err(from_glib_full(error))
31+
}
3032
}
3133
}
3234
#[cfg(windows)]

gio/src/unix_input_stream.rs

Lines changed: 5 additions & 8 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(unix)]
4-
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
4+
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
55

66
use glib::{prelude::*, translate::*};
77
#[cfg(all(not(unix), docsrs))]
@@ -12,16 +12,13 @@ use crate::{ffi, InputStream, UnixInputStream};
1212
impl UnixInputStream {
1313
// rustdoc-stripper-ignore-next
1414
/// Creates a new [`Self`] that takes ownership of the passed in fd.
15-
///
16-
/// # Safety
17-
/// You must not close the fd unless you've previously called [`UnixInputStreamExtManual::set_close_fd`]
18-
/// with `true` on this stream. At which point you may only do so when all references to this
19-
/// stream have been dropped.
2015
#[doc(alias = "g_unix_input_stream_new")]
21-
pub unsafe fn take_fd(fd: impl IntoRawFd) -> UnixInputStream {
16+
pub fn take_fd(fd: OwnedFd) -> UnixInputStream {
2217
let fd = fd.into_raw_fd();
2318
let close_fd = true.into_glib();
24-
InputStream::from_glib_full(ffi::g_unix_input_stream_new(fd, close_fd)).unsafe_cast()
19+
unsafe {
20+
InputStream::from_glib_full(ffi::g_unix_input_stream_new(fd, close_fd)).unsafe_cast()
21+
}
2522
}
2623

2724
// rustdoc-stripper-ignore-next

gio/src/unix_output_stream.rs

Lines changed: 5 additions & 8 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(unix)]
4-
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
4+
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
55

66
use glib::{prelude::*, translate::*};
77
#[cfg(all(not(unix), docsrs))]
@@ -12,16 +12,13 @@ use crate::{ffi, OutputStream, UnixOutputStream};
1212
impl UnixOutputStream {
1313
// rustdoc-stripper-ignore-next
1414
/// Creates a new [`Self`] that takes ownership of the passed in fd.
15-
///
16-
/// # Safety
17-
/// You must not close the fd unless you've previously called [`UnixOutputStreamExtManual::set_close_fd`]
18-
/// on this stream. At which point you may only do so when all references to this stream have
19-
/// been dropped.
2015
#[doc(alias = "g_unix_output_stream_new")]
21-
pub unsafe fn take_fd(fd: impl IntoRawFd) -> UnixOutputStream {
16+
pub fn take_fd(fd: OwnedFd) -> UnixOutputStream {
2217
let fd = fd.into_raw_fd();
2318
let close_fd = true.into_glib();
24-
OutputStream::from_glib_full(ffi::g_unix_output_stream_new(fd, close_fd)).unsafe_cast()
19+
unsafe {
20+
OutputStream::from_glib_full(ffi::g_unix_output_stream_new(fd, close_fd)).unsafe_cast()
21+
}
2522
}
2623

2724
// rustdoc-stripper-ignore-next

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());

glib/src/log.rs

Lines changed: 9 additions & 5 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(unix)]
4-
use std::os::unix::io::AsRawFd;
4+
use std::os::unix::io::{AsFd, AsRawFd};
55
use std::{
66
boxed::Box as Box_,
77
sync::{Arc, Mutex, OnceLock},
@@ -972,16 +972,20 @@ pub fn log_variant(log_domain: Option<&str>, log_level: LogLevel, fields: &crate
972972
#[cfg_attr(docsrs, doc(cfg(unix)))]
973973
#[doc(alias = "g_log_writer_supports_color")]
974974
#[inline]
975-
pub fn log_writer_supports_color<T: AsRawFd>(output_fd: T) -> bool {
976-
unsafe { from_glib(ffi::g_log_writer_supports_color(output_fd.as_raw_fd())) }
975+
pub fn log_writer_supports_color(output_fd: impl AsFd) -> bool {
976+
unsafe {
977+
from_glib(ffi::g_log_writer_supports_color(
978+
output_fd.as_fd().as_raw_fd(),
979+
))
980+
}
977981
}
978982

979983
#[cfg(unix)]
980984
#[cfg_attr(docsrs, doc(cfg(unix)))]
981985
#[doc(alias = "g_log_writer_is_journald")]
982986
#[inline]
983-
pub fn log_writer_is_journald<T: AsRawFd>(output_fd: T) -> bool {
984-
unsafe { from_glib(ffi::g_log_writer_is_journald(output_fd.as_raw_fd())) }
987+
pub fn log_writer_is_journald(output_fd: impl AsFd) -> bool {
988+
unsafe { from_glib(ffi::g_log_writer_is_journald(output_fd.as_fd().as_raw_fd())) }
985989
}
986990

987991
#[doc(alias = "g_log_writer_format_fields")]

0 commit comments

Comments
 (0)