gio/
file_descriptor_based.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, FromRawFd, RawFd};
5
6use crate::ffi;
7use glib::{prelude::*, translate::*};
8#[cfg(all(not(unix), docsrs))]
9use socket::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
10
11glib::wrapper! {
12    /// `GFileDescriptorBased` is an interface for file descriptor based IO.
13    ///
14    /// It is implemented by streams (implementations of [`InputStream`][crate::InputStream] or
15    /// [`OutputStream`][crate::OutputStream]) that are based on file descriptors.
16    ///
17    /// Note that `<gio/gfiledescriptorbased.h>` belongs to the UNIX-specific
18    /// GIO interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
19    /// file or the `GioUnix-2.0` GIR namespace when using it.
20    ///
21    /// # Implements
22    ///
23    /// [`FileDescriptorBasedExt`][trait@crate::prelude::FileDescriptorBasedExt], [`FileDescriptorBasedExtManual`][trait@crate::prelude::FileDescriptorBasedExtManual]
24    #[doc(alias = "GFileDescriptorBased")]
25    pub struct FileDescriptorBased(Interface<ffi::GFileDescriptorBased, ffi::GFileDescriptorBasedIface>);
26
27    match fn {
28        type_ => || ffi::g_file_descriptor_based_get_type(),
29    }
30}
31
32impl FileDescriptorBased {
33    pub const NONE: Option<&'static FileDescriptorBased> = None;
34}
35
36impl AsRawFd for FileDescriptorBased {
37    fn as_raw_fd(&self) -> RawFd {
38        unsafe { ffi::g_file_descriptor_based_get_fd(self.to_glib_none().0) as _ }
39    }
40}
41
42pub trait FileDescriptorBasedExtManual: IsA<FileDescriptorBased> + 'static {
43    #[doc(alias = "g_file_descriptor_based_get_fd")]
44    #[doc(alias = "get_fd")]
45    fn fd<T: FromRawFd>(&self) -> T {
46        unsafe {
47            T::from_raw_fd(ffi::g_file_descriptor_based_get_fd(
48                self.as_ref().to_glib_none().0,
49            ))
50        }
51    }
52}
53
54impl<O: IsA<FileDescriptorBased>> FileDescriptorBasedExtManual for O {}