gio/auto/
unix_connection.rs1use crate::{ffi, AsyncResult, Cancellable, Credentials, IOStream, SocketConnection};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10 #[doc(alias = "GUnixConnection")]
11 pub struct UnixConnection(Object<ffi::GUnixConnection, ffi::GUnixConnectionClass>) @extends SocketConnection, IOStream;
12
13 match fn {
14 type_ => || ffi::g_unix_connection_get_type(),
15 }
16}
17
18impl UnixConnection {
19 pub const NONE: Option<&'static UnixConnection> = None;
20}
21
22pub trait UnixConnectionExt: IsA<UnixConnection> + 'static {
23 #[doc(alias = "g_unix_connection_receive_credentials")]
24 fn receive_credentials(
25 &self,
26 cancellable: Option<&impl IsA<Cancellable>>,
27 ) -> Result<Credentials, glib::Error> {
28 unsafe {
29 let mut error = std::ptr::null_mut();
30 let ret = ffi::g_unix_connection_receive_credentials(
31 self.as_ref().to_glib_none().0,
32 cancellable.map(|p| p.as_ref()).to_glib_none().0,
33 &mut error,
34 );
35 if error.is_null() {
36 Ok(from_glib_full(ret))
37 } else {
38 Err(from_glib_full(error))
39 }
40 }
41 }
42
43 #[doc(alias = "g_unix_connection_receive_credentials_async")]
44 fn receive_credentials_async<P: FnOnce(Result<Credentials, glib::Error>) + 'static>(
45 &self,
46 cancellable: Option<&impl IsA<Cancellable>>,
47 callback: P,
48 ) {
49 let main_context = glib::MainContext::ref_thread_default();
50 let is_main_context_owner = main_context.is_owner();
51 let has_acquired_main_context = (!is_main_context_owner)
52 .then(|| main_context.acquire().ok())
53 .flatten();
54 assert!(
55 is_main_context_owner || has_acquired_main_context.is_some(),
56 "Async operations only allowed if the thread is owning the MainContext"
57 );
58
59 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
60 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
61 unsafe extern "C" fn receive_credentials_async_trampoline<
62 P: FnOnce(Result<Credentials, glib::Error>) + 'static,
63 >(
64 _source_object: *mut glib::gobject_ffi::GObject,
65 res: *mut crate::ffi::GAsyncResult,
66 user_data: glib::ffi::gpointer,
67 ) {
68 let mut error = std::ptr::null_mut();
69 let ret = ffi::g_unix_connection_receive_credentials_finish(
70 _source_object as *mut _,
71 res,
72 &mut error,
73 );
74 let result = if error.is_null() {
75 Ok(from_glib_full(ret))
76 } else {
77 Err(from_glib_full(error))
78 };
79 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
80 Box_::from_raw(user_data as *mut _);
81 let callback: P = callback.into_inner();
82 callback(result);
83 }
84 let callback = receive_credentials_async_trampoline::<P>;
85 unsafe {
86 ffi::g_unix_connection_receive_credentials_async(
87 self.as_ref().to_glib_none().0,
88 cancellable.map(|p| p.as_ref()).to_glib_none().0,
89 Some(callback),
90 Box_::into_raw(user_data) as *mut _,
91 );
92 }
93 }
94
95 fn receive_credentials_future(
96 &self,
97 ) -> Pin<Box_<dyn std::future::Future<Output = Result<Credentials, glib::Error>> + 'static>>
98 {
99 Box_::pin(crate::GioFuture::new(
100 self,
101 move |obj, cancellable, send| {
102 obj.receive_credentials_async(Some(cancellable), move |res| {
103 send.resolve(res);
104 });
105 },
106 ))
107 }
108
109 #[doc(alias = "g_unix_connection_receive_fd")]
110 fn receive_fd(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<i32, glib::Error> {
111 unsafe {
112 let mut error = std::ptr::null_mut();
113 let ret = ffi::g_unix_connection_receive_fd(
114 self.as_ref().to_glib_none().0,
115 cancellable.map(|p| p.as_ref()).to_glib_none().0,
116 &mut error,
117 );
118 if error.is_null() {
119 Ok(ret)
120 } else {
121 Err(from_glib_full(error))
122 }
123 }
124 }
125
126 #[doc(alias = "g_unix_connection_send_credentials")]
127 fn send_credentials(
128 &self,
129 cancellable: Option<&impl IsA<Cancellable>>,
130 ) -> Result<(), glib::Error> {
131 unsafe {
132 let mut error = std::ptr::null_mut();
133 let is_ok = ffi::g_unix_connection_send_credentials(
134 self.as_ref().to_glib_none().0,
135 cancellable.map(|p| p.as_ref()).to_glib_none().0,
136 &mut error,
137 );
138 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
139 if error.is_null() {
140 Ok(())
141 } else {
142 Err(from_glib_full(error))
143 }
144 }
145 }
146
147 #[doc(alias = "g_unix_connection_send_credentials_async")]
148 fn send_credentials_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
149 &self,
150 cancellable: Option<&impl IsA<Cancellable>>,
151 callback: P,
152 ) {
153 let main_context = glib::MainContext::ref_thread_default();
154 let is_main_context_owner = main_context.is_owner();
155 let has_acquired_main_context = (!is_main_context_owner)
156 .then(|| main_context.acquire().ok())
157 .flatten();
158 assert!(
159 is_main_context_owner || has_acquired_main_context.is_some(),
160 "Async operations only allowed if the thread is owning the MainContext"
161 );
162
163 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
164 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
165 unsafe extern "C" fn send_credentials_async_trampoline<
166 P: FnOnce(Result<(), glib::Error>) + 'static,
167 >(
168 _source_object: *mut glib::gobject_ffi::GObject,
169 res: *mut crate::ffi::GAsyncResult,
170 user_data: glib::ffi::gpointer,
171 ) {
172 let mut error = std::ptr::null_mut();
173 ffi::g_unix_connection_send_credentials_finish(
174 _source_object as *mut _,
175 res,
176 &mut error,
177 );
178 let result = if error.is_null() {
179 Ok(())
180 } else {
181 Err(from_glib_full(error))
182 };
183 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
184 Box_::from_raw(user_data as *mut _);
185 let callback: P = callback.into_inner();
186 callback(result);
187 }
188 let callback = send_credentials_async_trampoline::<P>;
189 unsafe {
190 ffi::g_unix_connection_send_credentials_async(
191 self.as_ref().to_glib_none().0,
192 cancellable.map(|p| p.as_ref()).to_glib_none().0,
193 Some(callback),
194 Box_::into_raw(user_data) as *mut _,
195 );
196 }
197 }
198
199 fn send_credentials_future(
200 &self,
201 ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
202 Box_::pin(crate::GioFuture::new(
203 self,
204 move |obj, cancellable, send| {
205 obj.send_credentials_async(Some(cancellable), move |res| {
206 send.resolve(res);
207 });
208 },
209 ))
210 }
211
212 #[doc(alias = "g_unix_connection_send_fd")]
213 fn send_fd(
214 &self,
215 fd: i32,
216 cancellable: Option<&impl IsA<Cancellable>>,
217 ) -> Result<(), glib::Error> {
218 unsafe {
219 let mut error = std::ptr::null_mut();
220 let is_ok = ffi::g_unix_connection_send_fd(
221 self.as_ref().to_glib_none().0,
222 fd,
223 cancellable.map(|p| p.as_ref()).to_glib_none().0,
224 &mut error,
225 );
226 debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
227 if error.is_null() {
228 Ok(())
229 } else {
230 Err(from_glib_full(error))
231 }
232 }
233 }
234}
235
236impl<O: IsA<UnixConnection>> UnixConnectionExt for O {}