gio/auto/
file_input_stream.rs1use crate::{ffi, AsyncResult, Cancellable, FileInfo, InputStream, Seekable};
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 let mut error = std::ptr::null_mut();
73 let ret = ffi::g_file_input_stream_query_info_finish(
74 _source_object as *mut _,
75 res,
76 &mut error,
77 );
78 let result = if error.is_null() {
79 Ok(from_glib_full(ret))
80 } else {
81 Err(from_glib_full(error))
82 };
83 let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
84 Box_::from_raw(user_data as *mut _);
85 let callback: P = callback.into_inner();
86 callback(result);
87 }
88 let callback = query_info_async_trampoline::<P>;
89 unsafe {
90 ffi::g_file_input_stream_query_info_async(
91 self.as_ref().to_glib_none().0,
92 attributes.to_glib_none().0,
93 io_priority.into_glib(),
94 cancellable.map(|p| p.as_ref()).to_glib_none().0,
95 Some(callback),
96 Box_::into_raw(user_data) as *mut _,
97 );
98 }
99 }
100
101 fn query_info_future(
102 &self,
103 attributes: &str,
104 io_priority: glib::Priority,
105 ) -> Pin<Box_<dyn std::future::Future<Output = Result<FileInfo, glib::Error>> + 'static>> {
106 let attributes = String::from(attributes);
107 Box_::pin(crate::GioFuture::new(
108 self,
109 move |obj, cancellable, send| {
110 obj.query_info_async(&attributes, io_priority, Some(cancellable), move |res| {
111 send.resolve(res);
112 });
113 },
114 ))
115 }
116}
117
118impl<O: IsA<FileInputStream>> FileInputStreamExt for O {}