gio/auto/
proxy.rs
1use crate::{ffi, AsyncResult, Cancellable, IOStream, ProxyAddress};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10 #[doc(alias = "GProxy")]
21 pub struct Proxy(Interface<ffi::GProxy, ffi::GProxyInterface>);
22
23 match fn {
24 type_ => || ffi::g_proxy_get_type(),
25 }
26}
27
28impl Proxy {
29 pub const NONE: Option<&'static Proxy> = None;
30
31 #[doc(alias = "g_proxy_get_default_for_protocol")]
41 #[doc(alias = "get_default_for_protocol")]
42 pub fn default_for_protocol(protocol: &str) -> Option<Proxy> {
43 unsafe {
44 from_glib_full(ffi::g_proxy_get_default_for_protocol(
45 protocol.to_glib_none().0,
46 ))
47 }
48 }
49}
50
51pub trait ProxyExt: IsA<Proxy> + 'static {
57 #[doc(alias = "g_proxy_connect")]
74 fn connect(
75 &self,
76 connection: &impl IsA<IOStream>,
77 proxy_address: &impl IsA<ProxyAddress>,
78 cancellable: Option<&impl IsA<Cancellable>>,
79 ) -> Result<IOStream, glib::Error> {
80 unsafe {
81 let mut error = std::ptr::null_mut();
82 let ret = ffi::g_proxy_connect(
83 self.as_ref().to_glib_none().0,
84 connection.as_ref().to_glib_none().0,
85 proxy_address.as_ref().to_glib_none().0,
86 cancellable.map(|p| p.as_ref()).to_glib_none().0,
87 &mut error,
88 );
89 if error.is_null() {
90 Ok(from_glib_full(ret))
91 } else {
92 Err(from_glib_full(error))
93 }
94 }
95 }
96
97 #[doc(alias = "g_proxy_connect_async")]
107 fn connect_async<P: FnOnce(Result<IOStream, glib::Error>) + 'static>(
108 &self,
109 connection: &impl IsA<IOStream>,
110 proxy_address: &impl IsA<ProxyAddress>,
111 cancellable: Option<&impl IsA<Cancellable>>,
112 callback: P,
113 ) {
114 let main_context = glib::MainContext::ref_thread_default();
115 let is_main_context_owner = main_context.is_owner();
116 let has_acquired_main_context = (!is_main_context_owner)
117 .then(|| main_context.acquire().ok())
118 .flatten();
119 assert!(
120 is_main_context_owner || has_acquired_main_context.is_some(),
121 "Async operations only allowed if the thread is owning the MainContext"
122 );
123
124 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
125 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
126 unsafe extern "C" fn connect_async_trampoline<
127 P: FnOnce(Result<IOStream, glib::Error>) + 'static,
128 >(
129 _source_object: *mut glib::gobject_ffi::GObject,
130 res: *mut crate::ffi::GAsyncResult,
131 user_data: glib::ffi::gpointer,
132 ) {
133 let mut error = std::ptr::null_mut();
134 let ret = ffi::g_proxy_connect_finish(_source_object as *mut _, res, &mut error);
135 let result = if error.is_null() {
136 Ok(from_glib_full(ret))
137 } else {
138 Err(from_glib_full(error))
139 };
140 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
141 Box_::from_raw(user_data as *mut _);
142 let callback: P = callback.into_inner();
143 callback(result);
144 }
145 let callback = connect_async_trampoline::<P>;
146 unsafe {
147 ffi::g_proxy_connect_async(
148 self.as_ref().to_glib_none().0,
149 connection.as_ref().to_glib_none().0,
150 proxy_address.as_ref().to_glib_none().0,
151 cancellable.map(|p| p.as_ref()).to_glib_none().0,
152 Some(callback),
153 Box_::into_raw(user_data) as *mut _,
154 );
155 }
156 }
157
158 fn connect_future(
159 &self,
160 connection: &(impl IsA<IOStream> + Clone + 'static),
161 proxy_address: &(impl IsA<ProxyAddress> + Clone + 'static),
162 ) -> Pin<Box_<dyn std::future::Future<Output = Result<IOStream, glib::Error>> + 'static>> {
163 let connection = connection.clone();
164 let proxy_address = proxy_address.clone();
165 Box_::pin(crate::GioFuture::new(
166 self,
167 move |obj, cancellable, send| {
168 obj.connect_async(&connection, &proxy_address, Some(cancellable), move |res| {
169 send.resolve(res);
170 });
171 },
172 ))
173 }
174
175 #[doc(alias = "g_proxy_supports_hostname")]
187 fn supports_hostname(&self) -> bool {
188 unsafe {
189 from_glib(ffi::g_proxy_supports_hostname(
190 self.as_ref().to_glib_none().0,
191 ))
192 }
193 }
194}
195
196impl<O: IsA<Proxy>> ProxyExt for O {}