gio_unix/
input_stream.rs

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