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::{AsRawFd, IntoRawFd, RawFd};
5
6use glib::{prelude::*, translate::*};
7#[cfg(all(not(unix), docsrs))]
8use socket::{AsRawFd, IntoRawFd, RawFd};
9
10use crate::{ffi, OutputStream, UnixOutputStream};
11
12impl UnixOutputStream {
13    // rustdoc-stripper-ignore-next
14    /// Creates a new [`Self`] that takes ownership of the passed in fd.
15    ///
16    /// # Safety
17    /// You must not close the fd unless you've previously called [`UnixOutputStreamExtManual::set_close_fd`]
18    /// on this stream. At which point you may only do so when all references to this stream have
19    /// been dropped.
20    #[doc(alias = "g_unix_output_stream_new")]
21    pub unsafe fn take_fd(fd: impl IntoRawFd) -> UnixOutputStream {
22        let fd = fd.into_raw_fd();
23        let close_fd = true.into_glib();
24        OutputStream::from_glib_full(ffi::g_unix_output_stream_new(fd, close_fd)).unsafe_cast()
25    }
26
27    // rustdoc-stripper-ignore-next
28    /// Creates a new [`Self`] that does not take ownership of the passed in fd.
29    ///
30    /// # Safety
31    /// You may only close the fd if all references to this stream have been dropped.
32    #[doc(alias = "g_unix_output_stream_new")]
33    pub unsafe fn with_fd<T: AsRawFd>(fd: T) -> UnixOutputStream {
34        let fd = fd.as_raw_fd();
35        let close_fd = false.into_glib();
36        OutputStream::from_glib_full(ffi::g_unix_output_stream_new(fd, close_fd)).unsafe_cast()
37    }
38}
39
40impl AsRawFd for UnixOutputStream {
41    fn as_raw_fd(&self) -> RawFd {
42        unsafe { ffi::g_unix_output_stream_get_fd(self.to_glib_none().0) as _ }
43    }
44}
45
46mod sealed {
47    pub trait Sealed {}
48    impl<T: super::IsA<super::UnixOutputStream>> Sealed for T {}
49}
50
51pub trait UnixOutputStreamExtManual: sealed::Sealed + IsA<UnixOutputStream> + Sized {
52    // rustdoc-stripper-ignore-next
53    /// Sets whether the fd of this stream will be closed when the stream is closed.
54    ///
55    /// # Safety
56    /// If you pass in `false` as the parameter, you may only close the fd if the all references
57    /// to the stream have been dropped. If you pass in `true`, you must never call close.
58    #[doc(alias = "g_unix_output_stream_set_close_fd")]
59    unsafe fn set_close_fd(&self, close_fd: bool) {
60        ffi::g_unix_output_stream_set_close_fd(
61            self.as_ref().to_glib_none().0,
62            close_fd.into_glib(),
63        );
64    }
65}
66
67impl<O: IsA<UnixOutputStream>> UnixOutputStreamExtManual for O {}