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
51mod sealed {
52 pub trait Sealed {}
53 impl<T: super::IsA<super::Proxy>> Sealed for T {}
54}
55
56pub trait ProxyExt: IsA<Proxy> + sealed::Sealed + 'static {
62 #[doc(alias = "g_proxy_connect")]
79 fn connect(
80 &self,
81 connection: &impl IsA<IOStream>,
82 proxy_address: &impl IsA<ProxyAddress>,
83 cancellable: Option<&impl IsA<Cancellable>>,
84 ) -> Result<IOStream, glib::Error> {
85 unsafe {
86 let mut error = std::ptr::null_mut();
87 let ret = ffi::g_proxy_connect(
88 self.as_ref().to_glib_none().0,
89 connection.as_ref().to_glib_none().0,
90 proxy_address.as_ref().to_glib_none().0,
91 cancellable.map(|p| p.as_ref()).to_glib_none().0,
92 &mut error,
93 );
94 if error.is_null() {
95 Ok(from_glib_full(ret))
96 } else {
97 Err(from_glib_full(error))
98 }
99 }
100 }
101
102 #[doc(alias = "g_proxy_connect_async")]
112 fn connect_async<P: FnOnce(Result<IOStream, glib::Error>) + 'static>(
113 &self,
114 connection: &impl IsA<IOStream>,
115 proxy_address: &impl IsA<ProxyAddress>,
116 cancellable: Option<&impl IsA<Cancellable>>,
117 callback: P,
118 ) {
119 let main_context = glib::MainContext::ref_thread_default();
120 let is_main_context_owner = main_context.is_owner();
121 let has_acquired_main_context = (!is_main_context_owner)
122 .then(|| main_context.acquire().ok())
123 .flatten();
124 assert!(
125 is_main_context_owner || has_acquired_main_context.is_some(),
126 "Async operations only allowed if the thread is owning the MainContext"
127 );
128
129 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
130 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
131 unsafe extern "C" fn connect_async_trampoline<
132 P: FnOnce(Result<IOStream, glib::Error>) + 'static,
133 >(
134 _source_object: *mut glib::gobject_ffi::GObject,
135 res: *mut crate::ffi::GAsyncResult,
136 user_data: glib::ffi::gpointer,
137 ) {
138 let mut error = std::ptr::null_mut();
139 let ret = ffi::g_proxy_connect_finish(_source_object as *mut _, res, &mut error);
140 let result = if error.is_null() {
141 Ok(from_glib_full(ret))
142 } else {
143 Err(from_glib_full(error))
144 };
145 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
146 Box_::from_raw(user_data as *mut _);
147 let callback: P = callback.into_inner();
148 callback(result);
149 }
150 let callback = connect_async_trampoline::<P>;
151 unsafe {
152 ffi::g_proxy_connect_async(
153 self.as_ref().to_glib_none().0,
154 connection.as_ref().to_glib_none().0,
155 proxy_address.as_ref().to_glib_none().0,
156 cancellable.map(|p| p.as_ref()).to_glib_none().0,
157 Some(callback),
158 Box_::into_raw(user_data) as *mut _,
159 );
160 }
161 }
162
163 fn connect_future(
164 &self,
165 connection: &(impl IsA<IOStream> + Clone + 'static),
166 proxy_address: &(impl IsA<ProxyAddress> + Clone + 'static),
167 ) -> Pin<Box_<dyn std::future::Future<Output = Result<IOStream, glib::Error>> + 'static>> {
168 let connection = connection.clone();
169 let proxy_address = proxy_address.clone();
170 Box_::pin(crate::GioFuture::new(
171 self,
172 move |obj, cancellable, send| {
173 obj.connect_async(&connection, &proxy_address, Some(cancellable), move |res| {
174 send.resolve(res);
175 });
176 },
177 ))
178 }
179
180 #[doc(alias = "g_proxy_supports_hostname")]
192 fn supports_hostname(&self) -> bool {
193 unsafe {
194 from_glib(ffi::g_proxy_supports_hostname(
195 self.as_ref().to_glib_none().0,
196 ))
197 }
198 }
199}
200
201impl<O: IsA<Proxy>> ProxyExt for O {}