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