gio/auto/
loadable_icon.rs
1use 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")]
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
28mod sealed {
29 pub trait Sealed {}
30 impl<T: super::IsA<super::LoadableIcon>> Sealed for T {}
31}
32
33pub trait LoadableIconExt: IsA<LoadableIcon> + sealed::Sealed + 'static {
39 #[doc(alias = "g_loadable_icon_load")]
55 fn load(
56 &self,
57 size: i32,
58 cancellable: Option<&impl IsA<Cancellable>>,
59 ) -> Result<(InputStream, glib::GString), glib::Error> {
60 unsafe {
61 let mut type_ = std::ptr::null_mut();
62 let mut error = std::ptr::null_mut();
63 let ret = ffi::g_loadable_icon_load(
64 self.as_ref().to_glib_none().0,
65 size,
66 &mut type_,
67 cancellable.map(|p| p.as_ref()).to_glib_none().0,
68 &mut error,
69 );
70 if error.is_null() {
71 Ok((from_glib_full(ret), from_glib_full(type_)))
72 } else {
73 Err(from_glib_full(error))
74 }
75 }
76 }
77
78 #[doc(alias = "g_loadable_icon_load_async")]
89 fn load_async<P: FnOnce(Result<(InputStream, glib::GString), glib::Error>) + 'static>(
90 &self,
91 size: i32,
92 cancellable: Option<&impl IsA<Cancellable>>,
93 callback: P,
94 ) {
95 let main_context = glib::MainContext::ref_thread_default();
96 let is_main_context_owner = main_context.is_owner();
97 let has_acquired_main_context = (!is_main_context_owner)
98 .then(|| main_context.acquire().ok())
99 .flatten();
100 assert!(
101 is_main_context_owner || has_acquired_main_context.is_some(),
102 "Async operations only allowed if the thread is owning the MainContext"
103 );
104
105 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
106 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
107 unsafe extern "C" fn load_async_trampoline<
108 P: FnOnce(Result<(InputStream, glib::GString), glib::Error>) + 'static,
109 >(
110 _source_object: *mut glib::gobject_ffi::GObject,
111 res: *mut crate::ffi::GAsyncResult,
112 user_data: glib::ffi::gpointer,
113 ) {
114 let mut error = std::ptr::null_mut();
115 let mut type_ = std::ptr::null_mut();
116 let ret = ffi::g_loadable_icon_load_finish(
117 _source_object as *mut _,
118 res,
119 &mut type_,
120 &mut error,
121 );
122 let result = if error.is_null() {
123 Ok((from_glib_full(ret), from_glib_full(type_)))
124 } else {
125 Err(from_glib_full(error))
126 };
127 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
128 Box_::from_raw(user_data as *mut _);
129 let callback: P = callback.into_inner();
130 callback(result);
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<Output = Result<(InputStream, glib::GString), glib::Error>>
150 + 'static,
151 >,
152 > {
153 Box_::pin(crate::GioFuture::new(
154 self,
155 move |obj, cancellable, send| {
156 obj.load_async(size, Some(cancellable), move |res| {
157 send.resolve(res);
158 });
159 },
160 ))
161 }
162}
163
164impl<O: IsA<LoadableIcon>> LoadableIconExt for O {}