gio/
unix_output_stream.rs

1// Copyright 2013-2018, The Gtk-rs Project Developers.
2// See the COPYRIGHT file at the top-level directory of this distribution.
3// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>
4
5use glib::object::{Cast, IsA};
6use glib::translate::*;
7use OutputStream;
8use UnixOutputStream;
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 UnixOutputStream {
17    pub unsafe fn new<T: IntoRawFd>(fd: T) -> UnixOutputStream {
18        let fd = fd.into_raw_fd();
19        let close_fd = true.to_glib();
20        OutputStream::from_glib_full(gio_sys::g_unix_output_stream_new(fd, close_fd)).unsafe_cast()
21    }
22}
23
24impl AsRawFd for UnixOutputStream {
25    fn as_raw_fd(&self) -> RawFd {
26        unsafe { gio_sys::g_unix_output_stream_get_fd(self.to_glib_none().0) as _ }
27    }
28}
29
30pub trait UnixOutputStreamExtManual: Sized {
31    fn get_fd<T: FromRawFd>(&self) -> T;
32    unsafe fn set_close_fd(&self, close_fd: bool);
33}
34
35impl<O: IsA<UnixOutputStream>> UnixOutputStreamExtManual for O {
36    fn get_fd<T: FromRawFd>(&self) -> T {
37        unsafe {
38            T::from_raw_fd(gio_sys::g_unix_output_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_output_stream_set_close_fd(
46            self.as_ref().to_glib_none().0,
47            close_fd.to_glib(),
48        );
49    }
50}