Skip to content

Port some functions to safe-io: pt II #1696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions gio/src/subprocess_launcher.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Take a look at the license at the top of the repository in the LICENSE file.

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

use glib::translate::*;

Expand All @@ -26,40 +26,41 @@ impl SubprocessLauncher {
#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
#[doc(alias = "g_subprocess_launcher_take_fd")]
pub fn take_fd(&self, source_fd: impl IntoRawFd, target_fd: impl IntoRawFd) {
pub fn take_fd(&self, source_fd: OwnedFd, target_fd: impl AsFd) {
let source_raw_fd = source_fd.into_raw_fd();
let target_raw_fd = target_fd.as_fd().as_raw_fd();
unsafe {
ffi::g_subprocess_launcher_take_fd(
self.to_glib_none().0,
source_fd.into_raw_fd(),
target_fd.into_raw_fd(),
);
ffi::g_subprocess_launcher_take_fd(self.to_glib_none().0, source_raw_fd, target_raw_fd);
}
}

#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
#[doc(alias = "g_subprocess_launcher_take_stderr_fd")]
pub fn take_stderr_fd(&self, fd: impl IntoRawFd) {
pub fn take_stderr_fd(&self, fd: Option<OwnedFd>) {
unsafe {
ffi::g_subprocess_launcher_take_stderr_fd(self.to_glib_none().0, fd.into_raw_fd());
let raw_fd = fd.map_or(-1, |fd| fd.into_raw_fd());
ffi::g_subprocess_launcher_take_stderr_fd(self.to_glib_none().0, raw_fd);
}
}

#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
#[doc(alias = "g_subprocess_launcher_take_stdin_fd")]
pub fn take_stdin_fd(&self, fd: impl IntoRawFd) {
pub fn take_stdin_fd(&self, fd: Option<OwnedFd>) {
let raw_fd = fd.map_or(-1, |fd| fd.into_raw_fd());
unsafe {
ffi::g_subprocess_launcher_take_stdin_fd(self.to_glib_none().0, fd.into_raw_fd());
ffi::g_subprocess_launcher_take_stdin_fd(self.to_glib_none().0, raw_fd);
}
}

#[cfg(unix)]
#[cfg_attr(docsrs, doc(cfg(unix)))]
#[doc(alias = "g_subprocess_launcher_take_stdout_fd")]
pub fn take_stdout_fd(&self, fd: impl IntoRawFd) {
pub fn take_stdout_fd(&self, fd: Option<OwnedFd>) {
let raw_fd = fd.map_or(-1, |fd| fd.into_raw_fd());
unsafe {
ffi::g_subprocess_launcher_take_stdout_fd(self.to_glib_none().0, fd.into_raw_fd());
ffi::g_subprocess_launcher_take_stdout_fd(self.to_glib_none().0, raw_fd);
}
}
}
16 changes: 9 additions & 7 deletions gio/src/unix_fd_list.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(unix)]
use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd};
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
use std::{mem, ptr};

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

use crate::{ffi, UnixFDList};

Expand All @@ -25,12 +25,12 @@ impl UnixFDList {

pub trait UnixFDListExtManual: IsA<UnixFDList> + Sized {
#[doc(alias = "g_unix_fd_list_append")]
fn append<T: AsRawFd>(&self, fd: T) -> Result<i32, glib::Error> {
fn append(&self, fd: impl AsFd) -> Result<i32, glib::Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = ffi::g_unix_fd_list_append(
self.as_ref().to_glib_none().0,
fd.as_raw_fd(),
fd.as_fd().as_raw_fd(),
&mut error,
);
if error.is_null() {
Expand All @@ -42,12 +42,14 @@ pub trait UnixFDListExtManual: IsA<UnixFDList> + Sized {
}

#[doc(alias = "g_unix_fd_list_get")]
fn get(&self, index_: i32) -> Result<RawFd, glib::Error> {
fn get(&self, index_: i32) -> Result<OwnedFd, glib::Error> {
unsafe {
let mut error = ptr::null_mut();
let ret = ffi::g_unix_fd_list_get(self.as_ref().to_glib_none().0, index_, &mut error);
let raw_fd =
ffi::g_unix_fd_list_get(self.as_ref().to_glib_none().0, index_, &mut error);
if error.is_null() {
Ok(ret)
let fd = OwnedFd::from_raw_fd(raw_fd);
Ok(fd)
} else {
Err(from_glib_full(error))
}
Expand Down
6 changes: 3 additions & 3 deletions glib/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::mem;
#[cfg(feature = "v2_58")]
use std::os::unix::io::{AsFd, AsRawFd};
#[cfg(not(windows))]
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
use std::os::unix::io::{FromRawFd, OwnedFd, RawFd};
use std::ptr;

// #[cfg(windows)]
Expand Down Expand Up @@ -261,7 +261,7 @@ pub fn unix_open_pipe(flags: i32) -> Result<(RawFd, RawFd), Error> {
#[doc(alias = "g_file_open_tmp")]
pub fn file_open_tmp(
tmpl: Option<impl AsRef<std::path::Path>>,
) -> Result<(RawFd, std::path::PathBuf), crate::Error> {
) -> Result<(OwnedFd, std::path::PathBuf), crate::Error> {
unsafe {
let mut name_used = ptr::null_mut();
let mut error = ptr::null_mut();
Expand All @@ -271,7 +271,7 @@ pub fn file_open_tmp(
&mut error,
);
if error.is_null() {
Ok((ret.into_raw_fd(), from_glib_full(name_used)))
Ok((OwnedFd::from_raw_fd(ret), from_glib_full(name_used)))
} else {
Err(from_glib_full(error))
}
Expand Down
Loading