1 // Take a look at the license at the top of the repository in the LICENSE file.
2 
3 use crate::InputStream;
4 use crate::UnixInputStream;
5 use glib::object::{Cast, IsA};
6 use glib::translate::*;
7 
8 #[cfg(unix)]
9 use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
10 
11 #[cfg(all(not(unix), feature = "dox"))]
12 use socket::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
13 
14 impl UnixInputStream {
15     #[allow(clippy::missing_safety_doc)]
16     #[doc(alias = "g_unix_input_stream_new")]
take_fd<T: IntoRawFd>(fd: T) -> UnixInputStream17     pub unsafe fn take_fd<T: IntoRawFd>(fd: T) -> UnixInputStream {
18         let fd = fd.into_raw_fd();
19         let close_fd = true.into_glib();
20         InputStream::from_glib_full(ffi::g_unix_input_stream_new(fd, close_fd)).unsafe_cast()
21     }
22 
23     #[allow(clippy::missing_safety_doc)]
24     #[doc(alias = "g_unix_input_stream_new")]
with_fd<T: AsRawFd>(fd: T) -> UnixInputStream25     pub unsafe fn with_fd<T: AsRawFd>(fd: T) -> UnixInputStream {
26         let fd = fd.as_raw_fd();
27         let close_fd = false.into_glib();
28         InputStream::from_glib_full(ffi::g_unix_input_stream_new(fd, close_fd)).unsafe_cast()
29     }
30 }
31 
32 impl AsRawFd for UnixInputStream {
as_raw_fd(&self) -> RawFd33     fn as_raw_fd(&self) -> RawFd {
34         unsafe { ffi::g_unix_input_stream_get_fd(self.to_glib_none().0) as _ }
35     }
36 }
37 
38 pub trait UnixInputStreamExtManual: Sized {
39     #[doc(alias = "g_unix_input_stream_get_fd")]
40     #[doc(alias = "get_fd")]
fd<T: FromRawFd>(&self) -> T41     fn fd<T: FromRawFd>(&self) -> T;
42     #[allow(clippy::missing_safety_doc)]
43     #[doc(alias = "g_unix_input_stream_set_close_fd")]
set_close_fd(&self, close_fd: bool)44     unsafe fn set_close_fd(&self, close_fd: bool);
45 }
46 
47 impl<O: IsA<UnixInputStream>> UnixInputStreamExtManual for O {
fd<T: FromRawFd>(&self) -> T48     fn fd<T: FromRawFd>(&self) -> T {
49         unsafe {
50             T::from_raw_fd(ffi::g_unix_input_stream_get_fd(
51                 self.as_ref().to_glib_none().0,
52             ))
53         }
54     }
55 
set_close_fd(&self, close_fd: bool)56     unsafe fn set_close_fd(&self, close_fd: bool) {
57         ffi::g_unix_input_stream_set_close_fd(self.as_ref().to_glib_none().0, close_fd.into_glib());
58     }
59 }
60