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