gio/auto/
loadable_icon.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, AsyncResult, Cancellable, Icon, InputStream};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10    /// `GLoadableIcon` extends the [`Icon`][crate::Icon] interface and adds the ability
11    /// to load icons from streams.
12    ///
13    /// # Implements
14    ///
15    /// [`LoadableIconExt`][trait@crate::prelude::LoadableIconExt], [`IconExt`][trait@crate::prelude::IconExt]
16    #[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
28/// Trait containing all [`struct@LoadableIcon`] methods.
29///
30/// # Implementors
31///
32/// [`BytesIcon`][struct@crate::BytesIcon], [`FileIcon`][struct@crate::FileIcon], [`LoadableIcon`][struct@crate::LoadableIcon]
33pub trait LoadableIconExt: IsA<LoadableIcon> + 'static {
34    /// Loads a loadable icon. For the asynchronous version of this function,
35    /// see g_loadable_icon_load_async().
36    /// ## `size`
37    /// an integer.
38    /// ## `cancellable`
39    /// optional #GCancellable object, [`None`] to
40    /// ignore.
41    ///
42    /// # Returns
43    ///
44    /// a #GInputStream to read the icon from.
45    ///
46    /// ## `type_`
47    /// a location to store the type of the loaded
48    /// icon, [`None`] to ignore.
49    #[doc(alias = "g_loadable_icon_load")]
50    fn load(
51        &self,
52        size: i32,
53        cancellable: Option<&impl IsA<Cancellable>>,
54    ) -> Result<(InputStream, glib::GString), glib::Error> {
55        unsafe {
56            let mut type_ = std::ptr::null_mut();
57            let mut error = std::ptr::null_mut();
58            let ret = ffi::g_loadable_icon_load(
59                self.as_ref().to_glib_none().0,
60                size,
61                &mut type_,
62                cancellable.map(|p| p.as_ref()).to_glib_none().0,
63                &mut error,
64            );
65            if error.is_null() {
66                Ok((from_glib_full(ret), from_glib_full(type_)))
67            } else {
68                Err(from_glib_full(error))
69            }
70        }
71    }
72
73    /// Loads an icon asynchronously. To finish this function, see
74    /// g_loadable_icon_load_finish(). For the synchronous, blocking
75    /// version of this function, see g_loadable_icon_load().
76    /// ## `size`
77    /// an integer.
78    /// ## `cancellable`
79    /// optional #GCancellable object, [`None`] to ignore.
80    /// ## `callback`
81    /// a #GAsyncReadyCallback
82    ///   to call when the request is satisfied
83    #[doc(alias = "g_loadable_icon_load_async")]
84    fn load_async<P: FnOnce(Result<(InputStream, glib::GString), glib::Error>) + 'static>(
85        &self,
86        size: i32,
87        cancellable: Option<&impl IsA<Cancellable>>,
88        callback: P,
89    ) {
90        let main_context = glib::MainContext::ref_thread_default();
91        let is_main_context_owner = main_context.is_owner();
92        let has_acquired_main_context = (!is_main_context_owner)
93            .then(|| main_context.acquire().ok())
94            .flatten();
95        assert!(
96            is_main_context_owner || has_acquired_main_context.is_some(),
97            "Async operations only allowed if the thread is owning the MainContext"
98        );
99
100        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
101            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
102        unsafe extern "C" fn load_async_trampoline<
103            P: FnOnce(Result<(InputStream, glib::GString), glib::Error>) + 'static,
104        >(
105            _source_object: *mut glib::gobject_ffi::GObject,
106            res: *mut crate::ffi::GAsyncResult,
107            user_data: glib::ffi::gpointer,
108        ) {
109            let mut error = std::ptr::null_mut();
110            let mut type_ = std::ptr::null_mut();
111            let ret = ffi::g_loadable_icon_load_finish(
112                _source_object as *mut _,
113                res,
114                &mut type_,
115                &mut error,
116            );
117            let result = if error.is_null() {
118                Ok((from_glib_full(ret), from_glib_full(type_)))
119            } else {
120                Err(from_glib_full(error))
121            };
122            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
123                Box_::from_raw(user_data as *mut _);
124            let callback: P = callback.into_inner();
125            callback(result);
126        }
127        let callback = load_async_trampoline::<P>;
128        unsafe {
129            ffi::g_loadable_icon_load_async(
130                self.as_ref().to_glib_none().0,
131                size,
132                cancellable.map(|p| p.as_ref()).to_glib_none().0,
133                Some(callback),
134                Box_::into_raw(user_data) as *mut _,
135            );
136        }
137    }
138
139    fn load_future(
140        &self,
141        size: i32,
142    ) -> Pin<
143        Box_<
144            dyn std::future::Future<Output = Result<(InputStream, glib::GString), glib::Error>>
145                + 'static,
146        >,
147    > {
148        Box_::pin(crate::GioFuture::new(
149            self,
150            move |obj, cancellable, send| {
151                obj.load_async(size, Some(cancellable), move |res| {
152                    send.resolve(res);
153                });
154            },
155        ))
156    }
157}
158
159impl<O: IsA<LoadableIcon>> LoadableIconExt for O {}