Skip to content

Commit 5f60fec

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 59cb4d2 commit 5f60fec

File tree

6 files changed

+52
-31
lines changed

6 files changed

+52
-31
lines changed

gio/src/desktop_app_info.rs

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

@@ -48,12 +48,7 @@ pub trait DesktopAppInfoExtManual {
4848
#[cfg(any(all(feature = "v2_58", unix), all(unix, feature = "dox")))]
4949
#[cfg_attr(feature = "dox", 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>,
@@ -69,12 +64,7 @@ pub trait DesktopAppInfoExtManual {
6964
impl<O: IsA<DesktopAppInfo>> DesktopAppInfoExtManual for O {
7065
#[cfg(any(all(feature = "v2_58", unix), all(unix, feature = "dox")))]
7166
#[cfg_attr(feature = "dox", doc(cfg(all(feature = "v2_58", unix))))]
72-
fn launch_uris_as_manager_with_fds<
73-
P: IsA<AppLaunchContext>,
74-
T: AsRawFd,
75-
U: AsRawFd,
76-
V: AsRawFd,
77-
>(
67+
fn launch_uris_as_manager_with_fds<P: IsA<AppLaunchContext>, T: AsFd, U: AsFd, V: AsFd>(
7868
&self,
7969
uris: &[&str],
8070
launch_context: Option<&P>,
@@ -135,9 +125,9 @@ impl<O: IsA<DesktopAppInfo>> DesktopAppInfoExtManual for O {
135125
Box_::into_raw(super_callback0) as *mut _,
136126
pid_callback,
137127
super_callback1 as *const _ as usize as *mut _,
138-
stdin_fd.as_raw_fd(),
139-
stdout_fd.as_raw_fd(),
140-
stderr_fd.as_raw_fd(),
128+
stdin_fd.as_fd().as_raw_fd(),
129+
stdout_fd.as_fd().as_raw_fd(),
130+
stderr_fd.as_fd().as_raw_fd(),
141131
&mut error,
142132
);
143133
if error.is_null() {

gio/src/socket.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::os::raw::c_int;
55
#[cfg(all(not(windows), feature = "dox"))]
66
use std::os::raw::c_void;
77
#[cfg(unix)]
8-
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
8+
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
99
#[cfg(windows)]
1010
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
1111
#[cfg(any(feature = "v2_60", feature = "dox"))]
@@ -31,6 +31,13 @@ impl Socket {
3131
Err(from_glib_full(error))
3232
}
3333
}
34+
#[cfg(any(unix, feature = "dox"))]
35+
pub fn from_owned_fd(fd: OwnedFd) -> Result<Socket, glib::Error> {
36+
unsafe {
37+
let fd = fd.into_raw_fd();
38+
Socket::from_fd(fd)
39+
}
40+
}
3441
#[cfg(any(windows, feature = "dox"))]
3542
pub unsafe fn from_socket(socket: impl IntoRawSocket) -> Result<Socket, glib::Error> {
3643
let socket = socket.into_raw_socket();

gio/src/unix_input_stream.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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

66
use glib::{prelude::*, translate::*};
77
#[cfg(all(not(unix), feature = "dox"))]
8-
use socket::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
8+
use socket::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
99

1010
use crate::{InputStream, UnixInputStream};
1111

@@ -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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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

66
use glib::{prelude::*, translate::*};
77
#[cfg(all(not(unix), feature = "dox"))]
8-
use socket::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
8+
use socket::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
99

1010
use crate::{OutputStream, UnixOutputStream};
1111

@@ -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(any(feature = "v2_58", feature = "dox"))]
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(feature = "dox", 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(any(unix, feature = "dox"))]
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},
@@ -958,15 +958,19 @@ pub fn log_variant(log_domain: Option<&str>, log_level: LogLevel, fields: &crate
958958
#[cfg(any(unix, feature = "dox"))]
959959
#[doc(alias = "g_log_writer_supports_color")]
960960
#[inline]
961-
pub fn log_writer_supports_color<T: AsRawFd>(output_fd: T) -> bool {
962-
unsafe { from_glib(ffi::g_log_writer_supports_color(output_fd.as_raw_fd())) }
961+
pub fn log_writer_supports_color<T: AsFd>(output_fd: T) -> bool {
962+
unsafe {
963+
from_glib(ffi::g_log_writer_supports_color(
964+
output_fd.as_fd().as_raw_fd(),
965+
))
966+
}
963967
}
964968

965969
#[cfg(any(unix, feature = "dox"))]
966970
#[doc(alias = "g_log_writer_is_journald")]
967971
#[inline]
968-
pub fn log_writer_is_journald<T: AsRawFd>(output_fd: T) -> bool {
969-
unsafe { from_glib(ffi::g_log_writer_is_journald(output_fd.as_raw_fd())) }
972+
pub fn log_writer_is_journald<T: AsFd>(output_fd: T) -> bool {
973+
unsafe { from_glib(ffi::g_log_writer_is_journald(output_fd.as_fd().as_raw_fd())) }
970974
}
971975

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

0 commit comments

Comments
 (0)