1use glib::object::{Cast, IsA};
6use glib::translate::*;
7use InputStream;
8use UnixInputStream;
9
10#[cfg(unix)]
11use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
12
13#[cfg(all(not(unix), feature = "dox"))]
14use socket::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
15
16impl UnixInputStream {
17 pub unsafe fn new<T: IntoRawFd>(fd: T) -> UnixInputStream {
18 let fd = fd.into_raw_fd();
19 let close_fd = true.to_glib();
20 InputStream::from_glib_full(gio_sys::g_unix_input_stream_new(fd, close_fd)).unsafe_cast()
21 }
22}
23
24impl AsRawFd for UnixInputStream {
25 fn as_raw_fd(&self) -> RawFd {
26 unsafe { gio_sys::g_unix_input_stream_get_fd(self.to_glib_none().0) as _ }
27 }
28}
29
30pub trait UnixInputStreamExtManual: Sized {
31 fn get_fd<T: FromRawFd>(&self) -> T;
32 unsafe fn set_close_fd(&self, close_fd: bool);
33}
34
35impl<O: IsA<UnixInputStream>> UnixInputStreamExtManual for O {
36 fn get_fd<T: FromRawFd>(&self) -> T {
37 unsafe {
38 T::from_raw_fd(gio_sys::g_unix_input_stream_get_fd(
39 self.as_ref().to_glib_none().0,
40 ))
41 }
42 }
43
44 unsafe fn set_close_fd(&self, close_fd: bool) {
45 gio_sys::g_unix_input_stream_set_close_fd(
46 self.as_ref().to_glib_none().0,
47 close_fd.to_glib(),
48 );
49 }
50}