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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Take a look at the license at the top of the repository in the LICENSE file.

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

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

use crate::UnixFDMessage;

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

pub trait UnixFDMessageExtManual: sealed::Sealed + IsA<UnixFDMessage> + Sized {
    /// Adds a file descriptor to `self`.
    ///
    /// The file descriptor is duplicated using `dup()`. You keep your copy
    /// of the descriptor and the copy contained in `self` will be closed
    /// when `self` is finalized.
    ///
    /// A possible cause of failure is exceeding the per-process or
    /// system-wide file descriptor limit.
    /// ## `fd`
    /// a valid open file descriptor
    ///
    /// # Returns
    ///
    /// [`true`] in case of success, else [`false`] (and `error` is set)
    #[doc(alias = "g_unix_fd_message_append_fd")]
    fn append_fd<T: AsRawFd>(&self, fd: T) -> Result<(), glib::Error> {
        unsafe {
            let mut error = ptr::null_mut();
            ffi::g_unix_fd_message_append_fd(
                self.as_ref().to_glib_none().0,
                fd.as_raw_fd(),
                &mut error,
            );
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }
    /// Returns the array of file descriptors that is contained in this
    /// object.
    ///
    /// After this call, the descriptors are no longer contained in
    /// `self`. Further calls will return an empty list (unless more
    /// descriptors have been added).
    ///
    /// The return result of this function must be freed with `g_free()`.
    /// The caller is also responsible for closing all of the file
    /// descriptors.
    ///
    /// If `length` is non-[`None`] then it is set to the number of file
    /// descriptors in the returned array. The returned array is also
    /// terminated with -1.
    ///
    /// This function never returns [`None`]. In case there are no file
    /// descriptors contained in `self`, an empty array is returned.
    ///
    /// # Returns
    ///
    /// an array of file
    ///  descriptors
    #[doc(alias = "g_unix_fd_message_steal_fds")]
    fn steal_fds(&self) -> Vec<RawFd> {
        unsafe {
            let mut length = mem::MaybeUninit::uninit();
            let ret = FromGlibContainer::from_glib_full_num(
                ffi::g_unix_fd_message_steal_fds(
                    self.as_ref().to_glib_none().0,
                    length.as_mut_ptr(),
                ),
                length.assume_init() as usize,
            );
            ret
        }
    }
}

impl<O: IsA<UnixFDMessage>> UnixFDMessageExtManual for O {}