1 // Take a look at the license at the top of the repository in the LICENSE file.
2 
3 use crate::OutputStream;
4 use crate::UnixOutputStream;
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 UnixOutputStream {
15     #[doc(alias = "g_unix_output_stream_new")]
take_fd<T: IntoRawFd>(fd: T) -> UnixOutputStream16     pub unsafe fn take_fd<T: IntoRawFd>(fd: T) -> UnixOutputStream {
17         let fd = fd.into_raw_fd();
18         let close_fd = true.into_glib();
19         OutputStream::from_glib_full(ffi::g_unix_output_stream_new(fd, close_fd)).unsafe_cast()
20     }
21 
22     #[doc(alias = "g_unix_output_stream_new")]
with_fd<T: AsRawFd>(fd: T) -> UnixOutputStream23     pub unsafe fn with_fd<T: AsRawFd>(fd: T) -> UnixOutputStream {
24         let fd = fd.as_raw_fd();
25         let close_fd = false.into_glib();
26         OutputStream::from_glib_full(ffi::g_unix_output_stream_new(fd, close_fd)).unsafe_cast()
27     }
28 }
29 
30 impl AsRawFd for UnixOutputStream {
as_raw_fd(&self) -> RawFd31     fn as_raw_fd(&self) -> RawFd {
32         unsafe { ffi::g_unix_output_stream_get_fd(self.to_glib_none().0) as _ }
33     }
34 }
35 
36 pub trait UnixOutputStreamExtManual: Sized {
37     #[doc(alias = "g_unix_output_stream_get_fd")]
38     #[doc(alias = "get_fd")]
fd<T: FromRawFd>(&self) -> T39     fn fd<T: FromRawFd>(&self) -> T;
40 
41     #[doc(alias = "g_unix_output_stream_set_close_fd")]
set_close_fd(&self, close_fd: bool)42     unsafe fn set_close_fd(&self, close_fd: bool);
43 }
44 
45 impl<O: IsA<UnixOutputStream>> UnixOutputStreamExtManual for O {
fd<T: FromRawFd>(&self) -> T46     fn fd<T: FromRawFd>(&self) -> T {
47         unsafe {
48             T::from_raw_fd(ffi::g_unix_output_stream_get_fd(
49                 self.as_ref().to_glib_none().0,
50             ))
51         }
52     }
53 
set_close_fd(&self, close_fd: bool)54     unsafe fn set_close_fd(&self, close_fd: bool) {
55         ffi::g_unix_output_stream_set_close_fd(
56             self.as_ref().to_glib_none().0,
57             close_fd.into_glib(),
58         );
59     }
60 }
61