Skip to content

Commit d2c7f58

Browse files
committed
Impl AsFd for types that implement AsRawFd
1 parent a07806c commit d2c7f58

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

gio/src/socket.rs

Lines changed: 12 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, OwnedFd, RawFd};
6+
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
77
#[cfg(windows)]
88
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket, RawSocket};
99
#[cfg(feature = "v2_60")]
@@ -62,6 +62,17 @@ impl AsRawFd for Socket {
6262
}
6363
}
6464

65+
#[cfg(any(unix, docsrs))]
66+
#[cfg_attr(docsrs, doc(cfg(unix)))]
67+
impl AsFd for Socket {
68+
fn as_fd(&self) -> BorrowedFd<'_> {
69+
unsafe {
70+
let raw_fd = self.as_raw_fd();
71+
BorrowedFd::borrow_raw(raw_fd)
72+
}
73+
}
74+
}
75+
6576
#[cfg(any(windows, docsrs))]
6677
#[cfg_attr(docsrs, doc(cfg(windows)))]
6778
impl AsRawSocket for Socket {

gio/src/unix_input_stream.rs

Lines changed: 11 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, OwnedFd, RawFd};
4+
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, IntoRawFd, OwnedFd, RawFd};
55

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

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

@@ -53,6 +53,15 @@ impl AsRawFd for UnixInputStream {
5353
}
5454
}
5555

56+
impl AsFd for UnixInputStream {
57+
fn as_fd(&self) -> BorrowedFd<'_> {
58+
unsafe {
59+
let raw_fd = self.as_raw_fd();
60+
BorrowedFd::borrow_raw(raw_fd)
61+
}
62+
}
63+
}
64+
5665
pub trait UnixInputStreamExtManual: IsA<UnixInputStream> + Sized {
5766
// rustdoc-stripper-ignore-next
5867
/// Sets whether the fd of this stream will be closed when the stream is closed.

gio/src/unix_output_stream.rs

Lines changed: 11 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, OwnedFd, RawFd};
4+
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, IntoRawFd, OwnedFd, RawFd};
55

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

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

@@ -53,6 +53,15 @@ impl AsRawFd for UnixOutputStream {
5353
}
5454
}
5555

56+
impl AsFd for UnixOutputStream {
57+
fn as_fd(&self) -> BorrowedFd<'_> {
58+
unsafe {
59+
let raw_fd = self.as_raw_fd();
60+
BorrowedFd::borrow_raw(raw_fd)
61+
}
62+
}
63+
}
64+
5665
pub trait UnixOutputStreamExtManual: IsA<UnixOutputStream> + Sized {
5766
// rustdoc-stripper-ignore-next
5867
/// Sets whether the fd of this stream will be closed when the stream is closed.

0 commit comments

Comments
 (0)