gio/auto/
file_output_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, OutputStream, Seekable};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10    /// `GFileOutputStream` provides output streams that write their
11    /// content to a file.
12    ///
13    /// `GFileOutputStream` implements [`Seekable`][crate::Seekable], which allows the output
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 output stream, use [`SeekableExt::tell()`][crate::prelude::SeekableExt::tell()].
19    /// To find out if a file output stream supports seeking, use
20    /// [`SeekableExt::can_seek()`][crate::prelude::SeekableExt::can_seek()].To position a file output stream, use
21    /// [`SeekableExt::seek()`][crate::prelude::SeekableExt::seek()]. To find out if a file output stream supports
22    /// truncating, use [`SeekableExt::can_truncate()`][crate::prelude::SeekableExt::can_truncate()]. To truncate a file output
23    /// stream, use [`SeekableExt::truncate()`][crate::prelude::SeekableExt::truncate()].
24    ///
25    /// # Implements
26    ///
27    /// [`FileOutputStreamExt`][trait@crate::prelude::FileOutputStreamExt], [`OutputStreamExt`][trait@crate::prelude::OutputStreamExt], [`trait@glib::ObjectExt`], [`SeekableExt`][trait@crate::prelude::SeekableExt], [`OutputStreamExtManual`][trait@crate::prelude::OutputStreamExtManual]
28    #[doc(alias = "GFileOutputStream")]
29    pub struct FileOutputStream(Object<ffi::GFileOutputStream, ffi::GFileOutputStreamClass>) @extends OutputStream, @implements Seekable;
30
31    match fn {
32        type_ => || ffi::g_file_output_stream_get_type(),
33    }
34}
35
36impl FileOutputStream {
37    pub const NONE: Option<&'static FileOutputStream> = None;
38}
39
40mod sealed {
41    pub trait Sealed {}
42    impl<T: super::IsA<super::FileOutputStream>> Sealed for T {}
43}
44
45/// Trait containing all [`struct@FileOutputStream`] methods.
46///
47/// # Implementors
48///
49/// [`FileOutputStream`][struct@crate::FileOutputStream]
50pub trait FileOutputStreamExt: IsA<FileOutputStream> + sealed::Sealed + '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_output_stream_get_etag")]
59    #[doc(alias = "get_etag")]
60    fn etag(&self) -> Option<glib::GString> {
61        unsafe {
62            from_glib_full(ffi::g_file_output_stream_get_etag(
63                self.as_ref().to_glib_none().0,
64            ))
65        }
66    }
67
68    /// Queries a file output stream for the given @attributes.
69    /// This function blocks while querying the stream. For the asynchronous
70    /// version of this function, see g_file_output_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]). In
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_output_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_output_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_output_stream_query_info_finish().
118    ///
119    /// For the synchronous version of this function, see
120    /// g_file_output_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    /// callback to call when the request is satisfied
130    #[doc(alias = "g_file_output_stream_query_info_async")]
131    fn query_info_async<P: FnOnce(Result<FileInfo, glib::Error>) + 'static>(
132        &self,
133        attributes: &str,
134        io_priority: glib::Priority,
135        cancellable: Option<&impl IsA<Cancellable>>,
136        callback: P,
137    ) {
138        let main_context = glib::MainContext::ref_thread_default();
139        let is_main_context_owner = main_context.is_owner();
140        let has_acquired_main_context = (!is_main_context_owner)
141            .then(|| main_context.acquire().ok())
142            .flatten();
143        assert!(
144            is_main_context_owner || has_acquired_main_context.is_some(),
145            "Async operations only allowed if the thread is owning the MainContext"
146        );
147
148        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
149            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
150        unsafe extern "C" fn query_info_async_trampoline<
151            P: FnOnce(Result<FileInfo, glib::Error>) + 'static,
152        >(
153            _source_object: *mut glib::gobject_ffi::GObject,
154            res: *mut crate::ffi::GAsyncResult,
155            user_data: glib::ffi::gpointer,
156        ) {
157            let mut error = std::ptr::null_mut();
158            let ret = ffi::g_file_output_stream_query_info_finish(
159                _source_object as *mut _,
160                res,
161                &mut error,
162            );
163            let result = if error.is_null() {
164                Ok(from_glib_full(ret))
165            } else {
166                Err(from_glib_full(error))
167            };
168            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
169                Box_::from_raw(user_data as *mut _);
170            let callback: P = callback.into_inner();
171            callback(result);
172        }
173        let callback = query_info_async_trampoline::<P>;
174        unsafe {
175            ffi::g_file_output_stream_query_info_async(
176                self.as_ref().to_glib_none().0,
177                attributes.to_glib_none().0,
178                io_priority.into_glib(),
179                cancellable.map(|p| p.as_ref()).to_glib_none().0,
180                Some(callback),
181                Box_::into_raw(user_data) as *mut _,
182            );
183        }
184    }
185
186    fn query_info_future(
187        &self,
188        attributes: &str,
189        io_priority: glib::Priority,
190    ) -> Pin<Box_<dyn std::future::Future<Output = Result<FileInfo, glib::Error>> + 'static>> {
191        let attributes = String::from(attributes);
192        Box_::pin(crate::GioFuture::new(
193            self,
194            move |obj, cancellable, send| {
195                obj.query_info_async(&attributes, io_priority, Some(cancellable), move |res| {
196                    send.resolve(res);
197                });
198            },
199        ))
200    }
201}
202
203impl<O: IsA<FileOutputStream>> FileOutputStreamExt for O {}