Skip to content

Commit a07806c

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 ce52380 commit a07806c

File tree

6 files changed

+49
-22
lines changed

6 files changed

+49
-22
lines changed

gio/src/desktop_app_info.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ pub trait DesktopAppInfoExtManual: IsA<DesktopAppInfo> {
4848
#[cfg(all(feature = "v2_58", unix))]
4949
#[cfg_attr(docsrs, doc(cfg(all(feature = "v2_58", unix))))]
5050
#[doc(alias = "g_desktop_app_info_launch_uris_as_manager_with_fds")]
51-
fn launch_uris_as_manager_with_fds<
52-
P: IsA<AppLaunchContext>,
53-
T: AsRawFd,
54-
U: AsRawFd,
55-
V: AsRawFd,
56-
>(
51+
fn launch_uris_as_manager_with_fds<P: IsA<AppLaunchContext>, T: AsFd, U: AsFd, V: AsFd>(
5752
&self,
5853
uris: &[&str],
5954
launch_context: Option<&P>,
@@ -112,9 +107,9 @@ pub trait DesktopAppInfoExtManual: IsA<DesktopAppInfo> {
112107
Box_::into_raw(super_callback0) as *mut _,
113108
pid_callback,
114109
super_callback1 as *const _ as usize as *mut _,
115-
stdin_fd.as_raw_fd(),
116-
stdout_fd.as_raw_fd(),
117-
stderr_fd.as_raw_fd(),
110+
stdin_fd.as_fd().as_raw_fd(),
111+
stdout_fd.as_fd().as_raw_fd(),
112+
stderr_fd.as_fd().as_raw_fd(),
118113
&mut error,
119114
);
120115
if error.is_null() {

gio/src/socket.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#[cfg(not(unix))]
44
use std::os::raw::c_int;
55
#[cfg(unix)]
6-
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
6+
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
77
#[cfg(windows)]
88
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
99
#[cfg(feature = "v2_60")]
@@ -31,6 +31,14 @@ impl Socket {
3131
Err(from_glib_full(error))
3232
}
3333
}
34+
#[cfg(unix)]
35+
#[cfg_attr(docsrs, doc(cfg(windows)))]
36+
pub fn from_owned_fd(fd: OwnedFd) -> Result<Socket, glib::Error> {
37+
unsafe {
38+
let fd = fd.into_raw_fd();
39+
Socket::from_fd(fd)
40+
}
41+
}
3442
#[cfg(windows)]
3543
#[cfg_attr(docsrs, doc(cfg(windows)))]
3644
#[allow(clippy::missing_safety_doc)]

gio/src/unix_input_stream.rs

Lines changed: 11 additions & 1 deletion
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))]
@@ -35,6 +35,16 @@ impl UnixInputStream {
3535
let close_fd = false.into_glib();
3636
InputStream::from_glib_full(ffi::g_unix_input_stream_new(fd, close_fd)).unsafe_cast()
3737
}
38+
39+
// rustdoc-stripper-ignore-next
40+
/// Creates a new [`Self`] that takes ownership of the passed in fd.
41+
#[doc(alias = "g_unix_input_stream_new")]
42+
pub fn from_owned_fd(fd: OwnedFd) -> UnixInputStream {
43+
unsafe {
44+
let fd = fd.into_raw_fd();
45+
UnixInputStream::take_fd(fd)
46+
}
47+
}
3848
}
3949

4050
impl AsRawFd for UnixInputStream {

gio/src/unix_output_stream.rs

Lines changed: 11 additions & 1 deletion
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))]
@@ -35,6 +35,16 @@ impl UnixOutputStream {
3535
let close_fd = false.into_glib();
3636
OutputStream::from_glib_full(ffi::g_unix_output_stream_new(fd, close_fd)).unsafe_cast()
3737
}
38+
39+
// rustdoc-stripper-ignore-next
40+
/// Creates a new [`Self`] that takes ownership of the passed in fd.
41+
#[doc(alias = "g_unix_output_stream_new")]
42+
pub fn from_owned_fd(fd: OwnedFd) -> Self {
43+
unsafe {
44+
let fd = fd.into_raw_fd();
45+
UnixOutputStream::take_fd(fd)
46+
}
47+
}
3848
}
3949

4050
impl AsRawFd for UnixOutputStream {

glib/src/functions.rs

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

1414
// #[cfg(windows)]
@@ -23,7 +23,7 @@ use crate::{Error, Pid, SpawnFlags};
2323
#[cfg_attr(docsrs, doc(cfg(all(feature = "v2_58", not(windows)))))]
2424
#[allow(clippy::too_many_arguments)]
2525
#[doc(alias = "g_spawn_async_with_fds")]
26-
pub fn spawn_async_with_fds<P: AsRef<std::path::Path>, T: AsRawFd, U: AsRawFd, V: AsRawFd>(
26+
pub fn spawn_async_with_fds<P: AsRef<std::path::Path>, T: AsFd, U: AsFd, V: AsFd>(
2727
working_directory: P,
2828
argv: &[&str],
2929
envp: &[&str],
@@ -57,9 +57,9 @@ pub fn spawn_async_with_fds<P: AsRef<std::path::Path>, T: AsRawFd, U: AsRawFd, V
5757
child_setup,
5858
Box_::into_raw(super_callback0) as *mut _,
5959
child_pid.as_mut_ptr(),
60-
stdin_fd.as_raw_fd(),
61-
stdout_fd.as_raw_fd(),
62-
stderr_fd.as_raw_fd(),
60+
stdin_fd.as_fd().as_raw_fd(),
61+
stdout_fd.as_fd().as_raw_fd(),
62+
stderr_fd.as_fd().as_raw_fd(),
6363
&mut error,
6464
);
6565
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},
@@ -965,16 +965,20 @@ pub fn log_variant(log_domain: Option<&str>, log_level: LogLevel, fields: &crate
965965
#[cfg_attr(docsrs, doc(cfg(unix)))]
966966
#[doc(alias = "g_log_writer_supports_color")]
967967
#[inline]
968-
pub fn log_writer_supports_color<T: AsRawFd>(output_fd: T) -> bool {
969-
unsafe { from_glib(ffi::g_log_writer_supports_color(output_fd.as_raw_fd())) }
968+
pub fn log_writer_supports_color<T: AsFd>(output_fd: T) -> bool {
969+
unsafe {
970+
from_glib(ffi::g_log_writer_supports_color(
971+
output_fd.as_fd().as_raw_fd(),
972+
))
973+
}
970974
}
971975

972976
#[cfg(unix)]
973977
#[cfg_attr(docsrs, doc(cfg(unix)))]
974978
#[doc(alias = "g_log_writer_is_journald")]
975979
#[inline]
976-
pub fn log_writer_is_journald<T: AsRawFd>(output_fd: T) -> bool {
977-
unsafe { from_glib(ffi::g_log_writer_is_journald(output_fd.as_raw_fd())) }
980+
pub fn log_writer_is_journald<T: AsFd>(output_fd: T) -> bool {
981+
unsafe { from_glib(ffi::g_log_writer_is_journald(output_fd.as_fd().as_raw_fd())) }
978982
}
979983

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

0 commit comments

Comments
 (0)