Skip to content

Commit 12365fc

Browse files
committed
Make Socket:from_fd use an OwnedFd
1 parent f93c9c0 commit 12365fc

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

gio/src/socket.rs

Lines changed: 17 additions & 11 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")]
@@ -18,15 +18,18 @@ use crate::{ffi, Cancellable, Socket, SocketAddress, SocketControlMessage};
1818
impl Socket {
1919
#[cfg(unix)]
2020
#[cfg_attr(docsrs, doc(cfg(unix)))]
21-
#[allow(clippy::missing_safety_doc)]
22-
pub unsafe fn from_fd(fd: impl IntoRawFd) -> Result<Socket, glib::Error> {
21+
#[doc(alias = "g_socket_new_from_fd")]
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+
libc::close(fd);
31+
Err(from_glib_full(error))
32+
}
3033
}
3134
}
3235
#[cfg(windows)]
@@ -801,7 +804,10 @@ mod tests {
801804
#[test]
802805
#[cfg(unix)]
803806
fn socket_messages() {
804-
use std::{io, os::unix::io::AsRawFd};
807+
use std::{
808+
io,
809+
os::unix::io::{AsRawFd, FromRawFd, OwnedFd},
810+
};
805811

806812
use super::Socket;
807813
use crate::{prelude::*, Cancellable, UnixFDMessage};
@@ -813,8 +819,8 @@ mod tests {
813819
panic!("{}", io::Error::last_os_error());
814820
}
815821
(
816-
Socket::from_fd(fds[0]).unwrap(),
817-
Socket::from_fd(fds[1]).unwrap(),
822+
Socket::from_fd(OwnedFd::from_raw_fd(fds[0])).unwrap(),
823+
Socket::from_fd(OwnedFd::from_raw_fd(fds[1])).unwrap(),
818824
)
819825
};
820826

gio/tests/dbus_peer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn test_gdbus_peer_connection() {
88
prelude::*,
99
DBusConnection, DBusConnectionFlags, DBusNodeInfo, Socket,
1010
};
11-
use std::os::{fd::IntoRawFd, unix::net::UnixStream};
11+
use std::os::unix::net::UnixStream;
1212

1313
const EXAMPLE_XML: &str = r#"
1414
<node>
@@ -23,7 +23,7 @@ fn test_gdbus_peer_connection() {
2323
"#;
2424

2525
pub async fn spawn_server(fd: UnixStream) -> DBusConnection {
26-
let socket = unsafe { Socket::from_fd(fd.into_raw_fd()) }.unwrap();
26+
let socket = Socket::from_fd(fd.into()).unwrap();
2727
let socket_connection = socket.connection_factory_create_connection();
2828

2929
let guid = gio::dbus_generate_guid();
@@ -112,7 +112,7 @@ fn test_gdbus_peer_connection() {
112112
}
113113

114114
pub async fn spawn_client(fd: UnixStream) -> DBusConnection {
115-
let socket_client = unsafe { Socket::from_fd(fd.into_raw_fd()) }.unwrap();
115+
let socket_client = Socket::from_fd(fd.into()).unwrap();
116116
let socket_connection_client = socket_client.connection_factory_create_connection();
117117

118118
dbg!("client connecting");

0 commit comments

Comments
 (0)