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