1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Take a look at the license at the top of the repository in the LICENSE file.

#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, RawFd};

use glib::{prelude::*, translate::*};
#[cfg(all(not(unix), docsrs))]
use socket::{AsRawFd, FromRawFd, IntoRawFd, RawFd};

glib::wrapper! {
    /// `GFileDescriptorBased` is an interface for file descriptor based IO.
    ///
    /// It is implemented by streams (implementations of [`InputStream`][crate::InputStream] or
    /// [`OutputStream`][crate::OutputStream]) that are based on file descriptors.
    ///
    /// Note that `<gio/gfiledescriptorbased.h>` belongs to the UNIX-specific
    /// GIO interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
    /// file or the `GioUnix-2.0` GIR namespace when using it.
    ///
    /// # Implements
    ///
    /// [`FileDescriptorBasedExt`][trait@crate::prelude::FileDescriptorBasedExt], [`FileDescriptorBasedExtManual`][trait@crate::prelude::FileDescriptorBasedExtManual]
    #[doc(alias = "GFileDescriptorBased")]
    pub struct FileDescriptorBased(Interface<ffi::GFileDescriptorBased, ffi::GFileDescriptorBasedIface>);

    match fn {
        type_ => || ffi::g_file_descriptor_based_get_type(),
    }
}

impl FileDescriptorBased {
    pub const NONE: Option<&'static FileDescriptorBased> = None;
}

impl AsRawFd for FileDescriptorBased {
    fn as_raw_fd(&self) -> RawFd {
        unsafe { ffi::g_file_descriptor_based_get_fd(self.to_glib_none().0) as _ }
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::FileDescriptorBased>> Sealed for T {}
}

pub trait FileDescriptorBasedExtManual:
    sealed::Sealed + IsA<FileDescriptorBased> + 'static
{
    #[doc(alias = "g_file_descriptor_based_get_fd")]
    #[doc(alias = "get_fd")]
    fn fd<T: FromRawFd>(&self) -> T {
        unsafe {
            T::from_raw_fd(ffi::g_file_descriptor_based_get_fd(
                self.as_ref().to_glib_none().0,
            ))
        }
    }
}

impl<O: IsA<FileDescriptorBased>> FileDescriptorBasedExtManual for O {}