gio_unix/
output_stream.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3#[cfg(unix)]
4use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, IntoRawFd, OwnedFd, RawFd};
5
6use glib::{prelude::*, translate::*};
7#[cfg(all(not(unix), docsrs))]
8use socket::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
9
10use crate::{OutputStream, ffi};
11
12impl OutputStream {
13    // rustdoc-stripper-ignore-next
14    /// Creates a new [`Self`] that takes ownership of the passed in fd.
15    #[doc(alias = "g_unix_output_stream_new")]
16    pub fn take_fd(fd: OwnedFd) -> OutputStream {
17        let fd = fd.into_raw_fd();
18        let close_fd = true.into_glib();
19        unsafe {
20            gio::OutputStream::from_glib_full(ffi::g_unix_output_stream_new(fd, close_fd))
21                .unsafe_cast()
22        }
23    }
24
25    // rustdoc-stripper-ignore-next
26    /// Creates a new [`Self`] that does not take ownership of the passed in fd.
27    ///
28    /// # Safety
29    /// You may only close the fd if all references to this stream have been dropped.
30    #[doc(alias = "g_unix_output_stream_new")]
31    pub unsafe fn with_fd<T: AsRawFd>(fd: T) -> OutputStream {
32        unsafe {
33            let fd = fd.as_raw_fd();
34            let close_fd = false.into_glib();
35            gio::OutputStream::from_glib_full(ffi::g_unix_output_stream_new(fd, close_fd))
36                .unsafe_cast()
37        }
38    }
39}
40
41impl AsRawFd for OutputStream {
42    fn as_raw_fd(&self) -> RawFd {
43        unsafe { ffi::g_unix_output_stream_get_fd(self.to_glib_none().0) as _ }
44    }
45}
46
47impl AsFd for OutputStream {
48    fn as_fd(&self) -> BorrowedFd<'_> {
49        unsafe {
50            let raw_fd = self.as_raw_fd();
51            BorrowedFd::borrow_raw(raw_fd)
52        }
53    }
54}
55
56pub trait UnixOutputStreamExtManual: IsA<OutputStream> + Sized {
57    // rustdoc-stripper-ignore-next
58    /// Sets whether the fd of this stream will be closed when the stream is closed.
59    ///
60    /// # Safety
61    /// If you pass in `false` as the parameter, you may only close the fd if the all references
62    /// to the stream have been dropped. If you pass in `true`, you must never call close.
63    #[doc(alias = "g_unix_output_stream_set_close_fd")]
64    unsafe fn set_close_fd(&self, close_fd: bool) {
65        unsafe {
66            ffi::g_unix_output_stream_set_close_fd(
67                self.as_ref().to_glib_none().0,
68                close_fd.into_glib(),
69            );
70        }
71    }
72}
73
74impl<O: IsA<OutputStream>> UnixOutputStreamExtManual for O {}