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