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
35/// Trait containing all [`struct@FileInputStream`] methods.
36///
37/// # Implementors
38///
39/// [`FileInputStream`][struct@crate::FileInputStream]
40pub trait FileInputStreamExt: IsA<FileInputStream> + 'static {
41    /// Queries a file input stream the given @attributes. This function blocks
42    /// while querying the stream. For the asynchronous (non-blocking) version
43    /// of this function, see g_file_input_stream_query_info_async(). While the
44    /// stream is blocked, the stream will set the pending flag internally, and
45    /// any other operations on the stream will fail with [`IOErrorEnum::Pending`][crate::IOErrorEnum::Pending].
46    /// ## `attributes`
47    /// a file attribute query string.
48    /// ## `cancellable`
49    /// optional #GCancellable object, [`None`] to ignore.
50    ///
51    /// # Returns
52    ///
53    /// a #GFileInfo, or [`None`] on error.
54    #[doc(alias = "g_file_input_stream_query_info")]
55    fn query_info(
56        &self,
57        attributes: &str,
58        cancellable: Option<&impl IsA<Cancellable>>,
59    ) -> Result<FileInfo, glib::Error> {
60        unsafe {
61            let mut error = std::ptr::null_mut();
62            let ret = ffi::g_file_input_stream_query_info(
63                self.as_ref().to_glib_none().0,
64                attributes.to_glib_none().0,
65                cancellable.map(|p| p.as_ref()).to_glib_none().0,
66                &mut error,
67            );
68            if error.is_null() {
69                Ok(from_glib_full(ret))
70            } else {
71                Err(from_glib_full(error))
72            }
73        }
74    }
75
76    /// Queries the stream information asynchronously.
77    /// When the operation is finished @callback will be called.
78    /// You can then call g_file_input_stream_query_info_finish()
79    /// to get the result of the operation.
80    ///
81    /// For the synchronous version of this function,
82    /// see g_file_input_stream_query_info().
83    ///
84    /// If @cancellable is not [`None`], then the operation can be cancelled by
85    /// triggering the cancellable object from another thread. If the operation
86    /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be set
87    /// ## `attributes`
88    /// a file attribute query string.
89    /// ## `io_priority`
90    /// the [I/O priority](iface.AsyncResult.html#io-priority) of the request
91    /// ## `cancellable`
92    /// optional #GCancellable object, [`None`] to ignore.
93    /// ## `callback`
94    /// a #GAsyncReadyCallback
95    ///   to call when the request is satisfied
96    #[doc(alias = "g_file_input_stream_query_info_async")]
97    fn query_info_async<P: FnOnce(Result<FileInfo, glib::Error>) + 'static>(
98        &self,
99        attributes: &str,
100        io_priority: glib::Priority,
101        cancellable: Option<&impl IsA<Cancellable>>,
102        callback: P,
103    ) {
104        let main_context = glib::MainContext::ref_thread_default();
105        let is_main_context_owner = main_context.is_owner();
106        let has_acquired_main_context = (!is_main_context_owner)
107            .then(|| main_context.acquire().ok())
108            .flatten();
109        assert!(
110            is_main_context_owner || has_acquired_main_context.is_some(),
111            "Async operations only allowed if the thread is owning the MainContext"
112        );
113
114        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
115            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
116        unsafe extern "C" fn query_info_async_trampoline<
117            P: FnOnce(Result<FileInfo, glib::Error>) + 'static,
118        >(
119            _source_object: *mut glib::gobject_ffi::GObject,
120            res: *mut crate::ffi::GAsyncResult,
121            user_data: glib::ffi::gpointer,
122        ) {
123            let mut error = std::ptr::null_mut();
124            let ret = ffi::g_file_input_stream_query_info_finish(
125                _source_object as *mut _,
126                res,
127                &mut error,
128            );
129            let result = if error.is_null() {
130                Ok(from_glib_full(ret))
131            } else {
132                Err(from_glib_full(error))
133            };
134            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
135                Box_::from_raw(user_data as *mut _);
136            let callback: P = callback.into_inner();
137            callback(result);
138        }
139        let callback = query_info_async_trampoline::<P>;
140        unsafe {
141            ffi::g_file_input_stream_query_info_async(
142                self.as_ref().to_glib_none().0,
143                attributes.to_glib_none().0,
144                io_priority.into_glib(),
145                cancellable.map(|p| p.as_ref()).to_glib_none().0,
146                Some(callback),
147                Box_::into_raw(user_data) as *mut _,
148            );
149        }
150    }
151
152    fn query_info_future(
153        &self,
154        attributes: &str,
155        io_priority: glib::Priority,
156    ) -> Pin<Box_<dyn std::future::Future<Output = Result<FileInfo, glib::Error>> + 'static>> {
157        let attributes = String::from(attributes);
158        Box_::pin(crate::GioFuture::new(
159            self,
160            move |obj, cancellable, send| {
161                obj.query_info_async(&attributes, io_priority, Some(cancellable), move |res| {
162                    send.resolve(res);
163                });
164            },
165        ))
166    }
167}
168
169impl<O: IsA<FileInputStream>> FileInputStreamExt for O {}