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