Skip to content

Commit f7ab8f6

Browse files
committed
Impl AsFd for Unix Streams
1 parent 9d8f480 commit f7ab8f6

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

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::{ffi, InputStream, UnixInputStream};
1111

@@ -40,6 +40,15 @@ impl AsRawFd for UnixInputStream {
4040
}
4141
}
4242

43+
impl AsFd for UnixInputStream {
44+
fn as_fd(&self) -> BorrowedFd<'_> {
45+
unsafe {
46+
let raw_fd = self.as_raw_fd();
47+
BorrowedFd::borrow_raw(raw_fd)
48+
}
49+
}
50+
}
51+
4352
pub trait UnixInputStreamExtManual: IsA<UnixInputStream> + Sized {
4453
// rustdoc-stripper-ignore-next
4554
/// 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::{ffi, OutputStream, UnixOutputStream};
1111

@@ -40,6 +40,15 @@ impl AsRawFd for UnixOutputStream {
4040
}
4141
}
4242

43+
impl AsFd for UnixOutputStream {
44+
fn as_fd(&self) -> BorrowedFd<'_> {
45+
unsafe {
46+
let raw_fd = self.as_raw_fd();
47+
BorrowedFd::borrow_raw(raw_fd)
48+
}
49+
}
50+
}
51+
4352
pub trait UnixOutputStreamExtManual: IsA<UnixOutputStream> + Sized {
4453
// rustdoc-stripper-ignore-next
4554
/// Sets whether the fd of this stream will be closed when the stream is closed.

0 commit comments

Comments
 (0)