gio/auto/
file_output_stream.rs1use crate::{ffi, AsyncResult, Cancellable, FileInfo, OutputStream, Seekable};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10 #[doc(alias = "GFileOutputStream")]
11 pub struct FileOutputStream(Object<ffi::GFileOutputStream, ffi::GFileOutputStreamClass>) @extends OutputStream, @implements Seekable;
12
13 match fn {
14 type_ => || ffi::g_file_output_stream_get_type(),
15 }
16}
17
18impl FileOutputStream {
19 pub const NONE: Option<&'static FileOutputStream> = None;
20}
21
22pub trait FileOutputStreamExt: IsA<FileOutputStream> + 'static {
23 #[doc(alias = "g_file_output_stream_get_etag")]
24 #[doc(alias = "get_etag")]
25 fn etag(&self) -> Option<glib::GString> {
26 unsafe {
27 from_glib_full(ffi::g_file_output_stream_get_etag(
28 self.as_ref().to_glib_none().0,
29 ))
30 }
31 }
32
33 #[doc(alias = "g_file_output_stream_query_info")]
34 fn query_info(
35 &self,
36 attributes: &str,
37 cancellable: Option<&impl IsA<Cancellable>>,
38 ) -> Result<FileInfo, glib::Error> {
39 unsafe {
40 let mut error = std::ptr::null_mut();
41 let ret = ffi::g_file_output_stream_query_info(
42 self.as_ref().to_glib_none().0,
43 attributes.to_glib_none().0,
44 cancellable.map(|p| p.as_ref()).to_glib_none().0,
45 &mut error,
46 );
47 if error.is_null() {
48 Ok(from_glib_full(ret))
49 } else {
50 Err(from_glib_full(error))
51 }
52 }
53 }
54
55 #[doc(alias = "g_file_output_stream_query_info_async")]
56 fn query_info_async<P: FnOnce(Result<FileInfo, glib::Error>) + 'static>(
57 &self,
58 attributes: &str,
59 io_priority: glib::Priority,
60 cancellable: Option<&impl IsA<Cancellable>>,
61 callback: P,
62 ) {
63 let main_context = glib::MainContext::ref_thread_default();
64 let is_main_context_owner = main_context.is_owner();
65 let has_acquired_main_context = (!is_main_context_owner)
66 .then(|| main_context.acquire().ok())
67 .flatten();
68 assert!(
69 is_main_context_owner || has_acquired_main_context.is_some(),
70 "Async operations only allowed if the thread is owning the MainContext"
71 );
72
73 let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
74 Box_::new(glib::thread_guard::ThreadGuard::new(callback));
75 unsafe extern "C" fn query_info_async_trampoline<
76 P: FnOnce(Result<FileInfo, glib::Error>) + 'static,
77 >(
78 _source_object: *mut glib::gobject_ffi::GObject,
79 res: *mut crate::ffi::GAsyncResult,
80 user_data: glib::ffi::gpointer,
81 ) {
82 let mut error = std::ptr::null_mut();
83 let ret = ffi::g_file_output_stream_query_info_finish(
84 _source_object as *mut _,
85 res,
86 &mut error,
87 );
88 let result = if error.is_null() {
89 Ok(from_glib_full(ret))
90 } else {
91 Err(from_glib_full(error))
92 };
93 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
94 Box_::from_raw(user_data as *mut _);
95 let callback: P = callback.into_inner();
96 callback(result);
97 }
98 let callback = query_info_async_trampoline::<P>;
99 unsafe {
100 ffi::g_file_output_stream_query_info_async(
101 self.as_ref().to_glib_none().0,
102 attributes.to_glib_none().0,
103 io_priority.into_glib(),
104 cancellable.map(|p| p.as_ref()).to_glib_none().0,
105 Some(callback),
106 Box_::into_raw(user_data) as *mut _,
107 );
108 }
109 }
110
111 fn query_info_future(
112 &self,
113 attributes: &str,
114 io_priority: glib::Priority,
115 ) -> Pin<Box_<dyn std::future::Future<Output = Result<FileInfo, glib::Error>> + 'static>> {
116 let attributes = String::from(attributes);
117 Box_::pin(crate::GioFuture::new(
118 self,
119 move |obj, cancellable, send| {
120 obj.query_info_async(&attributes, io_priority, Some(cancellable), move |res| {
121 send.resolve(res);
122 });
123 },
124 ))
125 }
126}
127
128impl<O: IsA<FileOutputStream>> FileOutputStreamExt for O {}