gio/auto/
proxy_resolver.rs1#[cfg(feature = "futures")]
6use futures::future;
7use gio_sys;
8use glib::object::IsA;
9use glib::translate::*;
10use glib::GString;
11use glib_sys;
12use gobject_sys;
13#[cfg(feature = "futures")]
14use std::boxed::Box as Box_;
15use std::fmt;
16use std::ptr;
17use Cancellable;
18use Error;
19
20glib_wrapper! {
21 pub struct ProxyResolver(Interface<gio_sys::GProxyResolver>);
22
23 match fn {
24 get_type => || gio_sys::g_proxy_resolver_get_type(),
25 }
26}
27
28impl ProxyResolver {
29 pub fn get_default() -> Option<ProxyResolver> {
30 unsafe { from_glib_none(gio_sys::g_proxy_resolver_get_default()) }
31 }
32}
33
34pub const NONE_PROXY_RESOLVER: Option<&ProxyResolver> = None;
35
36pub trait ProxyResolverExt: 'static {
37 fn is_supported(&self) -> bool;
38
39 fn lookup<P: IsA<Cancellable>>(
40 &self,
41 uri: &str,
42 cancellable: Option<&P>,
43 ) -> Result<Vec<GString>, Error>;
44
45 fn lookup_async<P: IsA<Cancellable>, Q: FnOnce(Result<Vec<GString>, Error>) + Send + 'static>(
46 &self,
47 uri: &str,
48 cancellable: Option<&P>,
49 callback: Q,
50 );
51
52 #[cfg(feature = "futures")]
53 fn lookup_async_future(
54 &self,
55 uri: &str,
56 ) -> Box_<dyn future::Future<Output = Result<Vec<GString>, Error>> + std::marker::Unpin>;
57}
58
59impl<O: IsA<ProxyResolver>> ProxyResolverExt for O {
60 fn is_supported(&self) -> bool {
61 unsafe {
62 from_glib(gio_sys::g_proxy_resolver_is_supported(
63 self.as_ref().to_glib_none().0,
64 ))
65 }
66 }
67
68 fn lookup<P: IsA<Cancellable>>(
69 &self,
70 uri: &str,
71 cancellable: Option<&P>,
72 ) -> Result<Vec<GString>, Error> {
73 unsafe {
74 let mut error = ptr::null_mut();
75 let ret = gio_sys::g_proxy_resolver_lookup(
76 self.as_ref().to_glib_none().0,
77 uri.to_glib_none().0,
78 cancellable.map(|p| p.as_ref()).to_glib_none().0,
79 &mut error,
80 );
81 if error.is_null() {
82 Ok(FromGlibPtrContainer::from_glib_full(ret))
83 } else {
84 Err(from_glib_full(error))
85 }
86 }
87 }
88
89 fn lookup_async<
90 P: IsA<Cancellable>,
91 Q: FnOnce(Result<Vec<GString>, Error>) + Send + 'static,
92 >(
93 &self,
94 uri: &str,
95 cancellable: Option<&P>,
96 callback: Q,
97 ) {
98 let user_data: Box<Q> = Box::new(callback);
99 unsafe extern "C" fn lookup_async_trampoline<
100 Q: FnOnce(Result<Vec<GString>, Error>) + Send + 'static,
101 >(
102 _source_object: *mut gobject_sys::GObject,
103 res: *mut gio_sys::GAsyncResult,
104 user_data: glib_sys::gpointer,
105 ) {
106 let mut error = ptr::null_mut();
107 let ret =
108 gio_sys::g_proxy_resolver_lookup_finish(_source_object as *mut _, res, &mut error);
109 let result = if error.is_null() {
110 Ok(FromGlibPtrContainer::from_glib_full(ret))
111 } else {
112 Err(from_glib_full(error))
113 };
114 let callback: Box<Q> = Box::from_raw(user_data as *mut _);
115 callback(result);
116 }
117 let callback = lookup_async_trampoline::<Q>;
118 unsafe {
119 gio_sys::g_proxy_resolver_lookup_async(
120 self.as_ref().to_glib_none().0,
121 uri.to_glib_none().0,
122 cancellable.map(|p| p.as_ref()).to_glib_none().0,
123 Some(callback),
124 Box::into_raw(user_data) as *mut _,
125 );
126 }
127 }
128
129 #[cfg(feature = "futures")]
130 fn lookup_async_future(
131 &self,
132 uri: &str,
133 ) -> Box_<dyn future::Future<Output = Result<Vec<GString>, Error>> + std::marker::Unpin> {
134 use fragile::Fragile;
135 use GioFuture;
136
137 let uri = String::from(uri);
138 GioFuture::new(self, move |obj, send| {
139 let cancellable = Cancellable::new();
140 let send = Fragile::new(send);
141 obj.lookup_async(&uri, Some(&cancellable), move |res| {
142 let _ = send.into_inner().send(res);
143 });
144
145 cancellable
146 })
147 }
148}
149
150impl fmt::Display for ProxyResolver {
151 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
152 write!(f, "ProxyResolver")
153 }
154}