gio/auto/
loadable_icon.rs1#[cfg(feature = "futures")]
6use futures::future;
7use gio_sys;
8use glib::object::IsA;
9use glib::translate::*;
10use glib::GString;
11use glib_sys;
12use gobject_sys;
13#[cfg(feature = "futures")]
14use std::boxed::Box as Box_;
15use std::fmt;
16use std::ptr;
17use Cancellable;
18use Error;
19use Icon;
20use InputStream;
21
22glib_wrapper! {
23 pub struct LoadableIcon(Interface<gio_sys::GLoadableIcon>) @requires Icon;
24
25 match fn {
26 get_type => || gio_sys::g_loadable_icon_get_type(),
27 }
28}
29
30pub const NONE_LOADABLE_ICON: Option<&LoadableIcon> = None;
31
32pub trait LoadableIconExt: 'static {
33 fn load<P: IsA<Cancellable>>(
34 &self,
35 size: i32,
36 cancellable: Option<&P>,
37 ) -> Result<(InputStream, GString), Error>;
38
39 fn load_async<
40 P: IsA<Cancellable>,
41 Q: FnOnce(Result<(InputStream, GString), Error>) + Send + 'static,
42 >(
43 &self,
44 size: i32,
45 cancellable: Option<&P>,
46 callback: Q,
47 );
48
49 #[cfg(feature = "futures")]
50 fn load_async_future(
51 &self,
52 size: i32,
53 ) -> Box_<dyn future::Future<Output = Result<(InputStream, GString), Error>> + std::marker::Unpin>;
54}
55
56impl<O: IsA<LoadableIcon>> LoadableIconExt for O {
57 fn load<P: IsA<Cancellable>>(
58 &self,
59 size: i32,
60 cancellable: Option<&P>,
61 ) -> Result<(InputStream, GString), Error> {
62 unsafe {
63 let mut type_ = ptr::null_mut();
64 let mut error = ptr::null_mut();
65 let ret = gio_sys::g_loadable_icon_load(
66 self.as_ref().to_glib_none().0,
67 size,
68 &mut type_,
69 cancellable.map(|p| p.as_ref()).to_glib_none().0,
70 &mut error,
71 );
72 if error.is_null() {
73 Ok((from_glib_full(ret), from_glib_full(type_)))
74 } else {
75 Err(from_glib_full(error))
76 }
77 }
78 }
79
80 fn load_async<
81 P: IsA<Cancellable>,
82 Q: FnOnce(Result<(InputStream, GString), Error>) + Send + 'static,
83 >(
84 &self,
85 size: i32,
86 cancellable: Option<&P>,
87 callback: Q,
88 ) {
89 let user_data: Box<Q> = Box::new(callback);
90 unsafe extern "C" fn load_async_trampoline<
91 Q: FnOnce(Result<(InputStream, GString), Error>) + Send + 'static,
92 >(
93 _source_object: *mut gobject_sys::GObject,
94 res: *mut gio_sys::GAsyncResult,
95 user_data: glib_sys::gpointer,
96 ) {
97 let mut error = ptr::null_mut();
98 let mut type_ = ptr::null_mut();
99 let ret = gio_sys::g_loadable_icon_load_finish(
100 _source_object as *mut _,
101 res,
102 &mut type_,
103 &mut error,
104 );
105 let result = if error.is_null() {
106 Ok((from_glib_full(ret), from_glib_full(type_)))
107 } else {
108 Err(from_glib_full(error))
109 };
110 let callback: Box<Q> = Box::from_raw(user_data as *mut _);
111 callback(result);
112 }
113 let callback = load_async_trampoline::<Q>;
114 unsafe {
115 gio_sys::g_loadable_icon_load_async(
116 self.as_ref().to_glib_none().0,
117 size,
118 cancellable.map(|p| p.as_ref()).to_glib_none().0,
119 Some(callback),
120 Box::into_raw(user_data) as *mut _,
121 );
122 }
123 }
124
125 #[cfg(feature = "futures")]
126 fn load_async_future(
127 &self,
128 size: i32,
129 ) -> Box_<dyn future::Future<Output = Result<(InputStream, GString), Error>> + std::marker::Unpin>
130 {
131 use fragile::Fragile;
132 use GioFuture;
133
134 GioFuture::new(self, move |obj, send| {
135 let cancellable = Cancellable::new();
136 let send = Fragile::new(send);
137 obj.load_async(size, Some(&cancellable), move |res| {
138 let _ = send.into_inner().send(res);
139 });
140
141 cancellable
142 })
143 }
144}
145
146impl fmt::Display for LoadableIcon {
147 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
148 write!(f, "LoadableIcon")
149 }
150}