1use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, IntoRawFd, OwnedFd, RawFd};
4
5use glib::{prelude::*, translate::*};
6
7use crate::{InputStream, ffi};
8
9impl InputStream {
10 #[doc(alias = "g_unix_input_stream_new")]
13 pub fn take_fd(fd: OwnedFd) -> InputStream {
14 let fd = fd.into_raw_fd();
15 let close_fd = true.into_glib();
16 unsafe {
17 gio::InputStream::from_glib_full(ffi::g_unix_input_stream_new(fd, close_fd))
18 .unsafe_cast()
19 }
20 }
21
22 #[doc(alias = "g_unix_input_stream_new")]
28 pub unsafe fn with_fd<T: AsRawFd>(fd: T) -> InputStream {
29 unsafe {
30 let fd = fd.as_raw_fd();
31 let close_fd = false.into_glib();
32 gio::InputStream::from_glib_full(ffi::g_unix_input_stream_new(fd, close_fd))
33 .unsafe_cast()
34 }
35 }
36}
37
38impl AsRawFd for InputStream {
39 fn as_raw_fd(&self) -> RawFd {
40 unsafe { ffi::g_unix_input_stream_get_fd(self.to_glib_none().0) as _ }
41 }
42}
43
44impl AsFd for InputStream {
45 fn as_fd(&self) -> BorrowedFd<'_> {
46 unsafe {
47 let raw_fd = self.as_raw_fd();
48 BorrowedFd::borrow_raw(raw_fd)
49 }
50 }
51}
52
53pub trait UnixInputStreamExtManual: IsA<InputStream> + Sized {
54 #[doc(alias = "g_unix_input_stream_set_close_fd")]
61 unsafe fn set_close_fd(&self, close_fd: bool) {
62 unsafe {
63 ffi::g_unix_input_stream_set_close_fd(
64 self.as_ref().to_glib_none().0,
65 close_fd.into_glib(),
66 );
67 }
68 }
69}
70
71impl<O: IsA<InputStream>> UnixInputStreamExtManual for O {}