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
45mod sealed {
46    pub trait Sealed {}
47    impl<T: super::IsA<super::FileIOStream>> Sealed for T {}
48}
49
50/// Trait containing all [`struct@FileIOStream`] methods.
51///
52/// # Implementors
53///
54/// [`FileIOStream`][struct@crate::FileIOStream]
55pub trait FileIOStreamExt: IsA<FileIOStream> + sealed::Sealed + 'static {
56    /// Gets the entity tag for the file when it has been written.
57    /// This must be called after the stream has been written
58    /// and closed, as the etag can change while writing.
59    ///
60    /// # Returns
61    ///
62    /// the entity tag for the stream.
63    #[doc(alias = "g_file_io_stream_get_etag")]
64    #[doc(alias = "get_etag")]
65    fn etag(&self) -> Option<glib::GString> {
66        unsafe {
67            from_glib_full(ffi::g_file_io_stream_get_etag(
68                self.as_ref().to_glib_none().0,
69            ))
70        }
71    }
72
73    /// Queries a file io stream for the given @attributes.
74    /// This function blocks while querying the stream. For the asynchronous
75    /// version of this function, see g_file_io_stream_query_info_async().
76    /// While the stream is blocked, the stream will set the pending flag
77    /// internally, and any other operations on the stream will fail with
78    /// [`IOErrorEnum::Pending`][crate::IOErrorEnum::Pending].
79    ///
80    /// Can fail if the stream was already closed (with @error being set to
81    /// [`IOErrorEnum::Closed`][crate::IOErrorEnum::Closed]), the stream has pending operations (with @error being
82    /// set to [`IOErrorEnum::Pending`][crate::IOErrorEnum::Pending]), or if querying info is not supported for
83    /// the stream's interface (with @error being set to [`IOErrorEnum::NotSupported`][crate::IOErrorEnum::NotSupported]). I
84    /// all cases of failure, [`None`] will be returned.
85    ///
86    /// If @cancellable is not [`None`], then the operation can be cancelled by
87    /// triggering the cancellable object from another thread. If the operation
88    /// was cancelled, the error [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be set, and [`None`] will
89    /// be returned.
90    /// ## `attributes`
91    /// a file attribute query string.
92    /// ## `cancellable`
93    /// optional #GCancellable object, [`None`] to ignore.
94    ///
95    /// # Returns
96    ///
97    /// a #GFileInfo for the @self, or [`None`] on error.
98    #[doc(alias = "g_file_io_stream_query_info")]
99    fn query_info(
100        &self,
101        attributes: &str,
102        cancellable: Option<&impl IsA<Cancellable>>,
103    ) -> Result<FileInfo, glib::Error> {
104        unsafe {
105            let mut error = std::ptr::null_mut();
106            let ret = ffi::g_file_io_stream_query_info(
107                self.as_ref().to_glib_none().0,
108                attributes.to_glib_none().0,
109                cancellable.map(|p| p.as_ref()).to_glib_none().0,
110                &mut error,
111            );
112            if error.is_null() {
113                Ok(from_glib_full(ret))
114            } else {
115                Err(from_glib_full(error))
116            }
117        }
118    }
119
120    /// Asynchronously queries the @self for a #GFileInfo. When completed,
121    /// @callback will be called with a #GAsyncResult which can be used to
122    /// finish the operation with g_file_io_stream_query_info_finish().
123    ///
124    /// For the synchronous version of this function, see
125    /// g_file_io_stream_query_info().
126    /// ## `attributes`
127    /// a file attribute query string.
128    /// ## `io_priority`
129    /// the [I/O priority](iface.AsyncResult.html#io-priority) of the
130    ///   request
131    /// ## `cancellable`
132    /// optional #GCancellable object, [`None`] to ignore.
133    /// ## `callback`
134    /// a #GAsyncReadyCallback
135    ///   to call when the request is satisfied
136    #[doc(alias = "g_file_io_stream_query_info_async")]
137    fn query_info_async<P: FnOnce(Result<FileInfo, glib::Error>) + 'static>(
138        &self,
139        attributes: &str,
140        io_priority: glib::Priority,
141        cancellable: Option<&impl IsA<Cancellable>>,
142        callback: P,
143    ) {
144        let main_context = glib::MainContext::ref_thread_default();
145        let is_main_context_owner = main_context.is_owner();
146        let has_acquired_main_context = (!is_main_context_owner)
147            .then(|| main_context.acquire().ok())
148            .flatten();
149        assert!(
150            is_main_context_owner || has_acquired_main_context.is_some(),
151            "Async operations only allowed if the thread is owning the MainContext"
152        );
153
154        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
155            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
156        unsafe extern "C" fn query_info_async_trampoline<
157            P: FnOnce(Result<FileInfo, glib::Error>) + 'static,
158        >(
159            _source_object: *mut glib::gobject_ffi::GObject,
160            res: *mut crate::ffi::GAsyncResult,
161            user_data: glib::ffi::gpointer,
162        ) {
163            let mut error = std::ptr::null_mut();
164            let ret =
165                ffi::g_file_io_stream_query_info_finish(_source_object as *mut _, res, &mut error);
166            let result = if error.is_null() {
167                Ok(from_glib_full(ret))
168            } else {
169                Err(from_glib_full(error))
170            };
171            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
172                Box_::from_raw(user_data as *mut _);
173            let callback: P = callback.into_inner();
174            callback(result);
175        }
176        let callback = query_info_async_trampoline::<P>;
177        unsafe {
178            ffi::g_file_io_stream_query_info_async(
179                self.as_ref().to_glib_none().0,
180                attributes.to_glib_none().0,
181                io_priority.into_glib(),
182                cancellable.map(|p| p.as_ref()).to_glib_none().0,
183                Some(callback),
184                Box_::into_raw(user_data) as *mut _,
185            );
186        }
187    }
188
189    fn query_info_future(
190        &self,
191        attributes: &str,
192        io_priority: glib::Priority,
193    ) -> Pin<Box_<dyn std::future::Future<Output = Result<FileInfo, glib::Error>> + 'static>> {
194        let attributes = String::from(attributes);
195        Box_::pin(crate::GioFuture::new(
196            self,
197            move |obj, cancellable, send| {
198                obj.query_info_async(&attributes, io_priority, Some(cancellable), move |res| {
199                    send.resolve(res);
200                });
201            },
202        ))
203    }
204}
205
206impl<O: IsA<FileIOStream>> FileIOStreamExt for O {}