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::{AsyncResult, Cancellable, Icon, InputStream, ffi};
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            unsafe {
110                let mut error = std::ptr::null_mut();
111                let mut type_ = std::ptr::null_mut();
112                let ret = ffi::g_loadable_icon_load_finish(
113                    _source_object as *mut _,
114                    res,
115                    &mut type_,
116                    &mut error,
117                );
118                let result = if error.is_null() {
119                    Ok((from_glib_full(ret), from_glib_full(type_)))
120                } else {
121                    Err(from_glib_full(error))
122                };
123                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
124                    Box_::from_raw(user_data as *mut _);
125                let callback: P = callback.into_inner();
126                callback(result);
127            }
128        }
129        let callback = load_async_trampoline::<P>;
130        unsafe {
131            ffi::g_loadable_icon_load_async(
132                self.as_ref().to_glib_none().0,
133                size,
134                cancellable.map(|p| p.as_ref()).to_glib_none().0,
135                Some(callback),
136                Box_::into_raw(user_data) as *mut _,
137            );
138        }
139    }
140
141    fn load_future(
142        &self,
143        size: i32,
144    ) -> Pin<
145        Box_<
146            dyn std::future::Future<Output = Result<(InputStream, glib::GString), glib::Error>>
147                + 'static,
148        >,
149    > {
150        Box_::pin(crate::GioFuture::new(
151            self,
152            move |obj, cancellable, send| {
153                obj.load_async(size, Some(cancellable), move |res| {
154                    send.resolve(res);
155                });
156            },
157        ))
158    }
159}
160
161impl<O: IsA<LoadableIcon>> LoadableIconExt for O {}