gio/auto/
proxy_resolver.rs1use crate::{ffi, AsyncResult, Cancellable};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10 #[doc(alias = "GProxyResolver")]
11 pub struct ProxyResolver(Interface<ffi::GProxyResolver, ffi::GProxyResolverInterface>);
12
13 match fn {
14 type_ => || ffi::g_proxy_resolver_get_type(),
15 }
16}
17
18impl ProxyResolver {
19 pub const NONE: Option<&'static ProxyResolver> = None;
20
21 #[doc(alias = "g_proxy_resolver_get_default")]
22 #[doc(alias = "get_default")]
23 #[allow(clippy::should_implement_trait)]
24 pub fn default() -> ProxyResolver {
25 unsafe { from_glib_none(ffi::g_proxy_resolver_get_default()) }
26 }
27}
28
29pub trait ProxyResolverExt: IsA<ProxyResolver> + 'static {
30 #[doc(alias = "g_proxy_resolver_is_supported")]
31 fn is_supported(&self) -> bool {
32 unsafe {
33 from_glib(ffi::g_proxy_resolver_is_supported(
34 self.as_ref().to_glib_none().0,
35 ))
36 }
37 }
38
39 #[doc(alias = "g_proxy_resolver_lookup")]
40 fn lookup(
41 &self,
42 uri: &str,
43 cancellable: Option<&impl IsA<Cancellable>>,
44 ) -> Result<Vec<glib::GString>, glib::Error> {
45 unsafe {
46 let mut error = std::ptr::null_mut();
47 let ret = ffi::g_proxy_resolver_lookup(
48 self.as_ref().to_glib_none().0,
49 uri.to_glib_none().0,
50 cancellable.map(|p| p.as_ref()).to_glib_none().0,
51 &mut error,
52 );
53 if error.is_null() {
54 Ok(FromGlibPtrContainer::from_glib_full(ret))
55 } else {
56 Err(from_glib_full(error))
57 }
58 }
59 }
60
61 #[doc(alias = "g_proxy_resolver_lookup_async")]
62 fn lookup_async<P: FnOnce(Result<Vec<glib::GString>, glib::Error>) + 'static>(
63 &self,
64 uri: &str,
65 cancellable: Option<&impl IsA<Cancellable>>,
66 callback: P,
67 ) {
68 let main_context = glib::MainContext::ref_thread_default();
69 let is_main_context_owner = main_context.is_owner();
70 let has_acquired_main_context = (!is_main_context_owner)
71 .then(|| main_context.acquire().ok())
72 .flatten();
73 assert!(
74 is_main_context_owner || has_acquired_main_context.is_some(),
75 "Async operations only allowed if the thread is owning the MainContext"
76 );
77
78 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
79 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
80 unsafe extern "C" fn lookup_async_trampoline<
81 P: FnOnce(Result<Vec<glib::GString>, glib::Error>) + 'static,
82 >(
83 _source_object: *mut glib::gobject_ffi::GObject,
84 res: *mut crate::ffi::GAsyncResult,
85 user_data: glib::ffi::gpointer,
86 ) {
87 let mut error = std::ptr::null_mut();
88 let ret =
89 ffi::g_proxy_resolver_lookup_finish(_source_object as *mut _, res, &mut error);
90 let result = if error.is_null() {
91 Ok(FromGlibPtrContainer::from_glib_full(ret))
92 } else {
93 Err(from_glib_full(error))
94 };
95 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
96 Box_::from_raw(user_data as *mut _);
97 let callback: P = callback.into_inner();
98 callback(result);
99 }
100 let callback = lookup_async_trampoline::<P>;
101 unsafe {
102 ffi::g_proxy_resolver_lookup_async(
103 self.as_ref().to_glib_none().0,
104 uri.to_glib_none().0,
105 cancellable.map(|p| p.as_ref()).to_glib_none().0,
106 Some(callback),
107 Box_::into_raw(user_data) as *mut _,
108 );
109 }
110 }
111
112 fn lookup_future(
113 &self,
114 uri: &str,
115 ) -> Pin<
116 Box_<dyn std::future::Future<Output = Result<Vec<glib::GString>, glib::Error>> + 'static>,
117 > {
118 let uri = String::from(uri);
119 Box_::pin(crate::GioFuture::new(
120 self,
121 move |obj, cancellable, send| {
122 obj.lookup_async(&uri, Some(cancellable), move |res| {
123 send.resolve(res);
124 });
125 },
126 ))
127 }
128}
129
130impl<O: IsA<ProxyResolver>> ProxyResolverExt for O {}