Skip to content

Commit 452a038

Browse files
committed
gio: Use OwnedSocket / BorrowedSocket for Windows APIs on GSocket
1 parent 8042abb commit 452a038

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

gio/src/socket.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#[cfg(unix)]
44
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
55
#[cfg(windows)]
6-
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
6+
use std::os::windows::io::{
7+
AsRawSocket, AsSocket, BorrowedSocket, FromRawSocket, IntoRawSocket, OwnedSocket, RawSocket,
8+
};
79
#[cfg(feature = "v2_60")]
810
use std::time::Duration;
911
use std::{cell::RefCell, marker::PhantomData, mem::transmute, pin::Pin, ptr};
@@ -34,15 +36,17 @@ impl Socket {
3436
}
3537
#[cfg(windows)]
3638
#[cfg_attr(docsrs, doc(cfg(windows)))]
37-
#[allow(clippy::missing_safety_doc)]
38-
pub unsafe fn from_socket(socket: impl IntoRawSocket) -> Result<Socket, glib::Error> {
39+
pub fn from_socket(socket: OwnedSocket) -> Result<Socket, glib::Error> {
3940
let socket = socket.into_raw_socket();
4041
let mut error = ptr::null_mut();
41-
let ret = ffi::g_socket_new_from_fd(socket as i32, &mut error);
42-
if error.is_null() {
43-
Ok(from_glib_full(ret))
44-
} else {
45-
Err(from_glib_full(error))
42+
unsafe {
43+
let ret = ffi::g_socket_new_from_fd(socket as i32, &mut error);
44+
if error.is_null() {
45+
Ok(from_glib_full(ret))
46+
} else {
47+
let _ = OwnedSocket::from_raw_socket(socket);
48+
Err(from_glib_full(error))
49+
}
4650
}
4751
}
4852
}
@@ -74,6 +78,17 @@ impl AsRawSocket for Socket {
7478
}
7579
}
7680

81+
#[cfg(windows)]
82+
#[cfg_attr(docsrs, doc(cfg(windows)))]
83+
impl AsSocket for Socket {
84+
fn as_socket(&self) -> BorrowedSocket<'_> {
85+
unsafe {
86+
let raw_socket = self.as_raw_socket();
87+
BorrowedSocket::borrow_raw(raw_socket)
88+
}
89+
}
90+
}
91+
7792
#[doc(alias = "GInputVector")]
7893
#[repr(transparent)]
7994
#[derive(Debug)]

0 commit comments

Comments
 (0)