gio/auto/
file_io_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, IOStream, Seekable};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10    /// `GFileIOStream` provides I/O streams that both read and write to the same
11    /// file handle.
12    ///
13    /// `GFileIOStream` implements [`Seekable`][crate::Seekable], which allows the I/O
14    /// stream to jump to arbitrary positions in the file and to truncate
15    /// the file, provided the filesystem of the file supports these
16    /// operations.
17    ///
18    /// To find the position of a file I/O stream, use [`SeekableExt::tell()`][crate::prelude::SeekableExt::tell()].
19    ///
20    /// To find out if a file I/O stream supports seeking, use
21    /// [`SeekableExt::can_seek()`][crate::prelude::SeekableExt::can_seek()]. To position a file I/O stream, use
22    /// [`SeekableExt::seek()`][crate::prelude::SeekableExt::seek()]. To find out if a file I/O stream supports
23    /// truncating, use [`SeekableExt::can_truncate()`][crate::prelude::SeekableExt::can_truncate()]. To truncate a file I/O
24    /// stream, use [`SeekableExt::truncate()`][crate::prelude::SeekableExt::truncate()].
25    ///
26    /// The default implementation of all the `GFileIOStream` operations
27    /// and the implementation of [`Seekable`][crate::Seekable] just call into the same
28    /// operations on the output stream.
29    ///
30    /// # Implements
31    ///
32    /// [`FileIOStreamExt`][trait@crate::prelude::FileIOStreamExt], [`IOStreamExt`][trait@crate::prelude::IOStreamExt], [`trait@glib::ObjectExt`], [`SeekableExt`][trait@crate::prelude::SeekableExt], [`IOStreamExtManual`][trait@crate::prelude::IOStreamExtManual]
33    #[doc(alias = "GFileIOStream")]
34    pub struct FileIOStream(Object<ffi::GFileIOStream, ffi::GFileIOStreamClass>) @extends IOStream, @implements Seekable;
35
36    match fn {
37        type_ => || ffi::g_file_io_stream_get_type(),
38    }
39}
40
41impl FileIOStream {
42    pub const NONE: Option<&'static FileIOStream> = None;
43}
44
45/// Trait containing all [`struct@FileIOStream`] methods.
46///
47/// # Implementors
48///
49/// [`FileIOStream`][struct@crate::FileIOStream]
50pub trait FileIOStreamExt: IsA<FileIOStream> + 'static {
51    /// Gets the entity tag for the file when it has been written.
52    /// This must be called after the stream has been written
53    /// and closed, as the etag can change while writing.
54    ///
55    /// # Returns
56    ///
57    /// the entity tag for the stream.
58    #[doc(alias = "g_file_io_stream_get_etag")]
59    #[doc(alias = "get_etag")]
60    fn etag(&self) -> Option<glib::GString> {
61        unsafe {
62            from_glib_full(ffi::g_file_io_stream_get_etag(
63                self.as_ref().to_glib_none().0,
64            ))
65        }
66    }
67
68    /// Queries a file io stream for the given @attributes.
69    /// This function blocks while querying the stream. For the asynchronous
70    /// version of this function, see g_file_io_stream_query_info_async().
71    /// While the stream is blocked, the stream will set the pending flag
72    /// internally, and any other operations on the stream will fail with
73    /// [`IOErrorEnum::Pending`][crate::IOErrorEnum::Pending].
74    ///
75    /// Can fail if the stream was already closed (with @error being set to
76    /// [`IOErrorEnum::Closed`][crate::IOErrorEnum::Closed]), the stream has pending operations (with @error being
77    /// set to [`IOErrorEnum::Pending`][crate::IOErrorEnum::Pending]), or if querying info is not supported for
78    /// the stream's interface (with @error being set to [`IOErrorEnum::NotSupported`][crate::IOErrorEnum::NotSupported]). I
79    /// all cases of failure, [`None`] will be returned.
80    ///
81    /// If @cancellable is not [`None`], then the operation can be cancelled by
82    /// triggering the cancellable object from another thread. If the operation
83    /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be set, and [`None`] will
84    /// be returned.
85    /// ## `attributes`
86    /// a file attribute query string.
87    /// ## `cancellable`
88    /// optional #GCancellable object, [`None`] to ignore.
89    ///
90    /// # Returns
91    ///
92    /// a #GFileInfo for the @self, or [`None`] on error.
93    #[doc(alias = "g_file_io_stream_query_info")]
94    fn query_info(
95        &self,
96        attributes: &str,
97        cancellable: Option<&impl IsA<Cancellable>>,
98    ) -> Result<FileInfo, glib::Error> {
99        unsafe {
100            let mut error = std::ptr::null_mut();
101            let ret = ffi::g_file_io_stream_query_info(
102                self.as_ref().to_glib_none().0,
103                attributes.to_glib_none().0,
104                cancellable.map(|p| p.as_ref()).to_glib_none().0,
105                &mut error,
106            );
107            if error.is_null() {
108                Ok(from_glib_full(ret))
109            } else {
110                Err(from_glib_full(error))
111            }
112        }
113    }
114
115    /// Asynchronously queries the @self for a #GFileInfo. When completed,
116    /// @callback will be called with a #GAsyncResult which can be used to
117    /// finish the operation with g_file_io_stream_query_info_finish().
118    ///
119    /// For the synchronous version of this function, see
120    /// g_file_io_stream_query_info().
121    /// ## `attributes`
122    /// a file attribute query string.
123    /// ## `io_priority`
124    /// the [I/O priority](iface.AsyncResult.html#io-priority) of the
125    ///   request
126    /// ## `cancellable`
127    /// optional #GCancellable object, [`None`] to ignore.
128    /// ## `callback`
129    /// a #GAsyncReadyCallback
130    ///   to call when the request is satisfied
131    #[doc(alias = "g_file_io_stream_query_info_async")]
132    fn query_info_async<P: FnOnce(Result<FileInfo, glib::Error>) + 'static>(
133        &self,
134        attributes: &str,
135        io_priority: glib::Priority,
136        cancellable: Option<&impl IsA<Cancellable>>,
137        callback: P,
138    ) {
139        let main_context = glib::MainContext::ref_thread_default();
140        let is_main_context_owner = main_context.is_owner();
141        let has_acquired_main_context = (!is_main_context_owner)
142            .then(|| main_context.acquire().ok())
143            .flatten();
144        assert!(
145            is_main_context_owner || has_acquired_main_context.is_some(),
146            "Async operations only allowed if the thread is owning the MainContext"
147        );
148
149        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
150            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
151        unsafe extern "C" fn query_info_async_trampoline<
152            P: FnOnce(Result<FileInfo, glib::Error>) + 'static,
153        >(
154            _source_object: *mut glib::gobject_ffi::GObject,
155            res: *mut crate::ffi::GAsyncResult,
156            user_data: glib::ffi::gpointer,
157        ) {
158            let mut error = std::ptr::null_mut();
159            let ret =
160                ffi::g_file_io_stream_query_info_finish(_source_object as *mut _, res, &mut error);
161            let result = if error.is_null() {
162                Ok(from_glib_full(ret))
163            } else {
164                Err(from_glib_full(error))
165            };
166            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
167                Box_::from_raw(user_data as *mut _);
168            let callback: P = callback.into_inner();
169            callback(result);
170        }
171        let callback = query_info_async_trampoline::<P>;
172        unsafe {
173            ffi::g_file_io_stream_query_info_async(
174                self.as_ref().to_glib_none().0,
175                attributes.to_glib_none().0,
176                io_priority.into_glib(),
177                cancellable.map(|p| p.as_ref()).to_glib_none().0,
178                Some(callback),
179                Box_::into_raw(user_data) as *mut _,
180            );
181        }
182    }
183
184    fn query_info_future(
185        &self,
186        attributes: &str,
187        io_priority: glib::Priority,
188    ) -> Pin<Box_<dyn std::future::Future<Output = Result<FileInfo, glib::Error>> + 'static>> {
189        let attributes = String::from(attributes);
190        Box_::pin(crate::GioFuture::new(
191            self,
192            move |obj, cancellable, send| {
193                obj.query_info_async(&attributes, io_priority, Some(cancellable), move |res| {
194                    send.resolve(res);
195                });
196            },
197        ))
198    }
199}
200
201impl<O: IsA<FileIOStream>> FileIOStreamExt for O {}