gio/auto/file_input_stream.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, FileInfo, InputStream, Seekable};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10 /// `GFileInputStream` provides input streams that take their
11 /// content from a file.
12 ///
13 /// `GFileInputStream` implements [`Seekable`][crate::Seekable], which allows the input
14 /// stream to jump to arbitrary positions in the file, provided the
15 /// filesystem of the file allows it. To find the position of a file
16 /// input stream, use [`SeekableExt::tell()`][crate::prelude::SeekableExt::tell()]. To find out if a file input
17 /// stream supports seeking, use `vfunc::Gio::Seekable::can_seek`.
18 /// To position a file input stream, use `vfunc::Gio::Seekable::seek`.
19 ///
20 /// # Implements
21 ///
22 /// [`FileInputStreamExt`][trait@crate::prelude::FileInputStreamExt], [`InputStreamExt`][trait@crate::prelude::InputStreamExt], [`trait@glib::ObjectExt`], [`SeekableExt`][trait@crate::prelude::SeekableExt], [`InputStreamExtManual`][trait@crate::prelude::InputStreamExtManual]
23 #[doc(alias = "GFileInputStream")]
24 pub struct FileInputStream(Object<ffi::GFileInputStream, ffi::GFileInputStreamClass>) @extends InputStream, @implements Seekable;
25
26 match fn {
27 type_ => || ffi::g_file_input_stream_get_type(),
28 }
29}
30
31impl FileInputStream {
32 pub const NONE: Option<&'static FileInputStream> = None;
33}
34
35mod sealed {
36 pub trait Sealed {}
37 impl<T: super::IsA<super::FileInputStream>> Sealed for T {}
38}
39
40/// Trait containing all [`struct@FileInputStream`] methods.
41///
42/// # Implementors
43///
44/// [`FileInputStream`][struct@crate::FileInputStream]
45pub trait FileInputStreamExt: IsA<FileInputStream> + sealed::Sealed + 'static {
46 /// Queries a file input stream the given @attributes. This function blocks
47 /// while querying the stream. For the asynchronous (non-blocking) version
48 /// of this function, see g_file_input_stream_query_info_async(). While the
49 /// stream is blocked, the stream will set the pending flag internally, and
50 /// any other operations on the stream will fail with [`IOErrorEnum::Pending`][crate::IOErrorEnum::Pending].
51 /// ## `attributes`
52 /// a file attribute query string.
53 /// ## `cancellable`
54 /// optional #GCancellable object, [`None`] to ignore.
55 ///
56 /// # Returns
57 ///
58 /// a #GFileInfo, or [`None`] on error.
59 #[doc(alias = "g_file_input_stream_query_info")]
60 fn query_info(
61 &self,
62 attributes: &str,
63 cancellable: Option<&impl IsA<Cancellable>>,
64 ) -> Result<FileInfo, glib::Error> {
65 unsafe {
66 let mut error = std::ptr::null_mut();
67 let ret = ffi::g_file_input_stream_query_info(
68 self.as_ref().to_glib_none().0,
69 attributes.to_glib_none().0,
70 cancellable.map(|p| p.as_ref()).to_glib_none().0,
71 &mut error,
72 );
73 if error.is_null() {
74 Ok(from_glib_full(ret))
75 } else {
76 Err(from_glib_full(error))
77 }
78 }
79 }
80
81 /// Queries the stream information asynchronously.
82 /// When the operation is finished @callback will be called.
83 /// You can then call g_file_input_stream_query_info_finish()
84 /// to get the result of the operation.
85 ///
86 /// For the synchronous version of this function,
87 /// see g_file_input_stream_query_info().
88 ///
89 /// If @cancellable is not [`None`], then the operation can be cancelled by
90 /// triggering the cancellable object from another thread. If the operation
91 /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be set
92 /// ## `attributes`
93 /// a file attribute query string.
94 /// ## `io_priority`
95 /// the [I/O priority](iface.AsyncResult.html#io-priority) of the request
96 /// ## `cancellable`
97 /// optional #GCancellable object, [`None`] to ignore.
98 /// ## `callback`
99 /// a #GAsyncReadyCallback
100 /// to call when the request is satisfied
101 #[doc(alias = "g_file_input_stream_query_info_async")]
102 fn query_info_async<P: FnOnce(Result<FileInfo, glib::Error>) + 'static>(
103 &self,
104 attributes: &str,
105 io_priority: glib::Priority,
106 cancellable: Option<&impl IsA<Cancellable>>,
107 callback: P,
108 ) {
109 let main_context = glib::MainContext::ref_thread_default();
110 let is_main_context_owner = main_context.is_owner();
111 let has_acquired_main_context = (!is_main_context_owner)
112 .then(|| main_context.acquire().ok())
113 .flatten();
114 assert!(
115 is_main_context_owner || has_acquired_main_context.is_some(),
116 "Async operations only allowed if the thread is owning the MainContext"
117 );
118
119 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
120 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
121 unsafe extern "C" fn query_info_async_trampoline<
122 P: FnOnce(Result<FileInfo, glib::Error>) + 'static,
123 >(
124 _source_object: *mut glib::gobject_ffi::GObject,
125 res: *mut crate::ffi::GAsyncResult,
126 user_data: glib::ffi::gpointer,
127 ) {
128 let mut error = std::ptr::null_mut();
129 let ret = ffi::g_file_input_stream_query_info_finish(
130 _source_object as *mut _,
131 res,
132 &mut error,
133 );
134 let result = if error.is_null() {
135 Ok(from_glib_full(ret))
136 } else {
137 Err(from_glib_full(error))
138 };
139 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
140 Box_::from_raw(user_data as *mut _);
141 let callback: P = callback.into_inner();
142 callback(result);
143 }
144 let callback = query_info_async_trampoline::<P>;
145 unsafe {
146 ffi::g_file_input_stream_query_info_async(
147 self.as_ref().to_glib_none().0,
148 attributes.to_glib_none().0,
149 io_priority.into_glib(),
150 cancellable.map(|p| p.as_ref()).to_glib_none().0,
151 Some(callback),
152 Box_::into_raw(user_data) as *mut _,
153 );
154 }
155 }
156
157 fn query_info_future(
158 &self,
159 attributes: &str,
160 io_priority: glib::Priority,
161 ) -> Pin<Box_<dyn std::future::Future<Output = Result<FileInfo, glib::Error>> + 'static>> {
162 let attributes = String::from(attributes);
163 Box_::pin(crate::GioFuture::new(
164 self,
165 move |obj, cancellable, send| {
166 obj.query_info_async(&attributes, io_priority, Some(cancellable), move |res| {
167 send.resolve(res);
168 });
169 },
170 ))
171 }
172}
173
174impl<O: IsA<FileInputStream>> FileInputStreamExt for O {}