gio/unix_fd_message.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, RawFd};
5use std::{mem, ptr};
6
7use glib::{prelude::*, translate::*};
8#[cfg(all(not(unix), docsrs))]
9use socket::{AsRawFd, RawFd};
10
11use crate::{ffi, UnixFDMessage};
12
13mod sealed {
14 pub trait Sealed {}
15 impl<T: super::IsA<super::UnixFDMessage>> Sealed for T {}
16}
17
18pub trait UnixFDMessageExtManual: sealed::Sealed + IsA<UnixFDMessage> + Sized {
19 /// Adds a file descriptor to @self.
20 ///
21 /// The file descriptor is duplicated using dup(). You keep your copy
22 /// of the descriptor and the copy contained in @self will be closed
23 /// when @self is finalized.
24 ///
25 /// A possible cause of failure is exceeding the per-process or
26 /// system-wide file descriptor limit.
27 /// ## `fd`
28 /// a valid open file descriptor
29 ///
30 /// # Returns
31 ///
32 /// [`true`] in case of success, else [`false`] (and @error is set)
33 #[doc(alias = "g_unix_fd_message_append_fd")]
34 fn append_fd<T: AsRawFd>(&self, fd: T) -> Result<(), glib::Error> {
35 unsafe {
36 let mut error = ptr::null_mut();
37 ffi::g_unix_fd_message_append_fd(
38 self.as_ref().to_glib_none().0,
39 fd.as_raw_fd(),
40 &mut error,
41 );
42 if error.is_null() {
43 Ok(())
44 } else {
45 Err(from_glib_full(error))
46 }
47 }
48 }
49 /// Returns the array of file descriptors that is contained in this
50 /// object.
51 ///
52 /// After this call, the descriptors are no longer contained in
53 /// @self. Further calls will return an empty list (unless more
54 /// descriptors have been added).
55 ///
56 /// The return result of this function must be freed with g_free().
57 /// The caller is also responsible for closing all of the file
58 /// descriptors.
59 ///
60 /// If @length is non-[`None`] then it is set to the number of file
61 /// descriptors in the returned array. The returned array is also
62 /// terminated with -1.
63 ///
64 /// This function never returns [`None`]. In case there are no file
65 /// descriptors contained in @self, an empty array is returned.
66 ///
67 /// # Returns
68 ///
69 /// an array of file
70 /// descriptors
71 #[doc(alias = "g_unix_fd_message_steal_fds")]
72 fn steal_fds(&self) -> Vec<RawFd> {
73 unsafe {
74 let mut length = mem::MaybeUninit::uninit();
75 let ret = FromGlibContainer::from_glib_full_num(
76 ffi::g_unix_fd_message_steal_fds(
77 self.as_ref().to_glib_none().0,
78 length.as_mut_ptr(),
79 ),
80 length.assume_init() as usize,
81 );
82 ret
83 }
84 }
85}
86
87impl<O: IsA<UnixFDMessage>> UnixFDMessageExtManual for O {}