gio/auto/
loadable_icon.rs1use crate::{AsyncResult, Cancellable, Icon, InputStream, ffi};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10 #[doc(alias = "GLoadableIcon")]
17 pub struct LoadableIcon(Interface<ffi::GLoadableIcon, ffi::GLoadableIconIface>) @requires Icon;
18
19 match fn {
20 type_ => || ffi::g_loadable_icon_get_type(),
21 }
22}
23
24impl LoadableIcon {
25 pub const NONE: Option<&'static LoadableIcon> = None;
26}
27
28pub trait LoadableIconExt: IsA<LoadableIcon> + 'static {
34 #[doc(alias = "g_loadable_icon_load")]
51 fn load(
52 &self,
53 size: i32,
54 cancellable: Option<&impl IsA<Cancellable>>,
55 ) -> Result<(InputStream, Option<glib::GString>), glib::Error> {
56 unsafe {
57 let mut type_ = std::ptr::null_mut();
58 let mut error = std::ptr::null_mut();
59 let ret = ffi::g_loadable_icon_load(
60 self.as_ref().to_glib_none().0,
61 size,
62 &mut type_,
63 cancellable.map(|p| p.as_ref()).to_glib_none().0,
64 &mut error,
65 );
66 if error.is_null() {
67 Ok((from_glib_full(ret), from_glib_full(type_)))
68 } else {
69 Err(from_glib_full(error))
70 }
71 }
72 }
73
74 #[doc(alias = "g_loadable_icon_load_async")]
85 fn load_async<
86 P: FnOnce(Result<(InputStream, Option<glib::GString>), glib::Error>) + 'static,
87 >(
88 &self,
89 size: i32,
90 cancellable: Option<&impl IsA<Cancellable>>,
91 callback: P,
92 ) {
93 let main_context = glib::MainContext::ref_thread_default();
94 let is_main_context_owner = main_context.is_owner();
95 let has_acquired_main_context = (!is_main_context_owner)
96 .then(|| main_context.acquire().ok())
97 .flatten();
98 assert!(
99 is_main_context_owner || has_acquired_main_context.is_some(),
100 "Async operations only allowed if the thread is owning the MainContext"
101 );
102
103 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
104 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
105 unsafe extern "C" fn load_async_trampoline<
106 P: FnOnce(Result<(InputStream, Option<glib::GString>), glib::Error>) + 'static,
107 >(
108 _source_object: *mut glib::gobject_ffi::GObject,
109 res: *mut crate::ffi::GAsyncResult,
110 user_data: glib::ffi::gpointer,
111 ) {
112 unsafe {
113 let mut error = std::ptr::null_mut();
114 let mut type_ = std::ptr::null_mut();
115 let ret = ffi::g_loadable_icon_load_finish(
116 _source_object as *mut _,
117 res,
118 &mut type_,
119 &mut error,
120 );
121 let result = if error.is_null() {
122 Ok((from_glib_full(ret), from_glib_full(type_)))
123 } else {
124 Err(from_glib_full(error))
125 };
126 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
127 Box_::from_raw(user_data as *mut _);
128 let callback: P = callback.into_inner();
129 callback(result);
130 }
131 }
132 let callback = load_async_trampoline::<P>;
133 unsafe {
134 ffi::g_loadable_icon_load_async(
135 self.as_ref().to_glib_none().0,
136 size,
137 cancellable.map(|p| p.as_ref()).to_glib_none().0,
138 Some(callback),
139 Box_::into_raw(user_data) as *mut _,
140 );
141 }
142 }
143
144 fn load_future(
145 &self,
146 size: i32,
147 ) -> Pin<
148 Box_<
149 dyn std::future::Future<
150 Output = Result<(InputStream, Option<glib::GString>), glib::Error>,
151 > + 'static,
152 >,
153 > {
154 Box_::pin(crate::GioFuture::new(
155 self,
156 move |obj, cancellable, send| {
157 obj.load_async(size, Some(cancellable), move |res| {
158 send.resolve(res);
159 });
160 },
161 ))
162 }
163}
164
165impl<O: IsA<LoadableIcon>> LoadableIconExt for O {}