libgweather/
location.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use crate::Location;
4
5use glib::object::IsA;
6use glib::translate::*;
7use std::boxed::Box as Box_;
8use std::pin::Pin;
9use std::ptr;
10
11impl Location {
12    #[doc(alias = "gweather_location_detect_nearest_city")]
13    pub fn detect_nearest_city<P: FnOnce(Result<Location, glib::Error>) + 'static>(
14        &self,
15        lat: f64,
16        lon: f64,
17        cancellable: Option<&impl IsA<gio::Cancellable>>,
18        callback: P,
19    ) {
20        let main_context = glib::MainContext::ref_thread_default();
21        let is_main_context_owner = main_context.is_owner();
22        let has_acquired_main_context = (!is_main_context_owner)
23            .then(|| main_context.acquire().ok())
24            .flatten();
25        assert!(
26            is_main_context_owner || has_acquired_main_context.is_some(),
27            "Async operations only allowed if the thread is owning the MainContext"
28        );
29
30        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
31            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
32        unsafe extern "C" fn detect_nearest_city_trampoline<
33            P: FnOnce(Result<Location, glib::Error>) + 'static,
34        >(
35            _source_object: *mut glib::gobject_ffi::GObject,
36            res: *mut gio::ffi::GAsyncResult,
37            user_data: glib::ffi::gpointer,
38        ) {
39            let mut error = ptr::null_mut();
40            let ret = ffi::gweather_location_detect_nearest_city_finish(res, &mut error);
41            let result = if error.is_null() {
42                Ok(from_glib_full(ret))
43            } else {
44                Err(from_glib_full(error))
45            };
46            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
47                Box_::from_raw(user_data as *mut _);
48            let callback: P = callback.into_inner();
49            callback(result);
50        }
51        let callback = detect_nearest_city_trampoline::<P>;
52        unsafe {
53            ffi::gweather_location_detect_nearest_city(
54                self.to_glib_none().0,
55                lat,
56                lon,
57                cancellable.map(|p| p.as_ref()).to_glib_none().0,
58                Some(callback),
59                Box_::into_raw(user_data) as *mut _,
60            );
61        }
62    }
63
64    pub fn detect_nearest_city_future(
65        &self,
66        lat: f64,
67        lon: f64,
68    ) -> Pin<Box_<dyn std::future::Future<Output = Result<Location, glib::Error>> + 'static>> {
69        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
70            obj.detect_nearest_city(lat, lon, Some(cancellable), move |res| {
71                send.resolve(res);
72            });
73        }))
74    }
75}