gio/auto/
file_input_stream.rs1#[cfg(feature = "futures")]
6use futures::future;
7use gio_sys;
8use glib;
9use glib::object::IsA;
10use glib::translate::*;
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 FileInfo;
20use InputStream;
21use Seekable;
22
23glib_wrapper! {
24 pub struct FileInputStream(Object<gio_sys::GFileInputStream, gio_sys::GFileInputStreamClass, FileInputStreamClass>) @extends InputStream, @implements Seekable;
25
26 match fn {
27 get_type => || gio_sys::g_file_input_stream_get_type(),
28 }
29}
30
31pub const NONE_FILE_INPUT_STREAM: Option<&FileInputStream> = None;
32
33pub trait FileInputStreamExt: 'static {
34 fn query_info<P: IsA<Cancellable>>(
35 &self,
36 attributes: &str,
37 cancellable: Option<&P>,
38 ) -> Result<FileInfo, Error>;
39
40 fn query_info_async<P: IsA<Cancellable>, Q: FnOnce(Result<FileInfo, Error>) + Send + 'static>(
41 &self,
42 attributes: &str,
43 io_priority: glib::Priority,
44 cancellable: Option<&P>,
45 callback: Q,
46 );
47
48 #[cfg(feature = "futures")]
49 fn query_info_async_future(
50 &self,
51 attributes: &str,
52 io_priority: glib::Priority,
53 ) -> Box_<dyn future::Future<Output = Result<FileInfo, Error>> + std::marker::Unpin>;
54}
55
56impl<O: IsA<FileInputStream>> FileInputStreamExt for O {
57 fn query_info<P: IsA<Cancellable>>(
58 &self,
59 attributes: &str,
60 cancellable: Option<&P>,
61 ) -> Result<FileInfo, Error> {
62 unsafe {
63 let mut error = ptr::null_mut();
64 let ret = gio_sys::g_file_input_stream_query_info(
65 self.as_ref().to_glib_none().0,
66 attributes.to_glib_none().0,
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))
72 } else {
73 Err(from_glib_full(error))
74 }
75 }
76 }
77
78 fn query_info_async<
79 P: IsA<Cancellable>,
80 Q: FnOnce(Result<FileInfo, Error>) + Send + 'static,
81 >(
82 &self,
83 attributes: &str,
84 io_priority: glib::Priority,
85 cancellable: Option<&P>,
86 callback: Q,
87 ) {
88 let user_data: Box<Q> = Box::new(callback);
89 unsafe extern "C" fn query_info_async_trampoline<
90 Q: FnOnce(Result<FileInfo, Error>) + Send + 'static,
91 >(
92 _source_object: *mut gobject_sys::GObject,
93 res: *mut gio_sys::GAsyncResult,
94 user_data: glib_sys::gpointer,
95 ) {
96 let mut error = ptr::null_mut();
97 let ret = gio_sys::g_file_input_stream_query_info_finish(
98 _source_object as *mut _,
99 res,
100 &mut error,
101 );
102 let result = if error.is_null() {
103 Ok(from_glib_full(ret))
104 } else {
105 Err(from_glib_full(error))
106 };
107 let callback: Box<Q> = Box::from_raw(user_data as *mut _);
108 callback(result);
109 }
110 let callback = query_info_async_trampoline::<Q>;
111 unsafe {
112 gio_sys::g_file_input_stream_query_info_async(
113 self.as_ref().to_glib_none().0,
114 attributes.to_glib_none().0,
115 io_priority.to_glib(),
116 cancellable.map(|p| p.as_ref()).to_glib_none().0,
117 Some(callback),
118 Box::into_raw(user_data) as *mut _,
119 );
120 }
121 }
122
123 #[cfg(feature = "futures")]
124 fn query_info_async_future(
125 &self,
126 attributes: &str,
127 io_priority: glib::Priority,
128 ) -> Box_<dyn future::Future<Output = Result<FileInfo, Error>> + std::marker::Unpin> {
129 use fragile::Fragile;
130 use GioFuture;
131
132 let attributes = String::from(attributes);
133 GioFuture::new(self, move |obj, send| {
134 let cancellable = Cancellable::new();
135 let send = Fragile::new(send);
136 obj.query_info_async(&attributes, io_priority, Some(&cancellable), move |res| {
137 let _ = send.into_inner().send(res);
138 });
139
140 cancellable
141 })
142 }
143}
144
145impl fmt::Display for FileInputStream {
146 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
147 write!(f, "FileInputStream")
148 }
149}