Skip to main content

gio/auto/
file_enumerator.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::{AsyncResult, Cancellable, File, FileInfo, ffi};
6use glib::{prelude::*, translate::*};
7use std::{boxed::Box as Box_, pin::Pin};
8
9glib::wrapper! {
10    #[doc(alias = "GFileEnumerator")]
11    pub struct FileEnumerator(Object<ffi::GFileEnumerator, ffi::GFileEnumeratorClass>);
12
13    match fn {
14        type_ => || ffi::g_file_enumerator_get_type(),
15    }
16}
17
18impl FileEnumerator {
19    pub const NONE: Option<&'static FileEnumerator> = None;
20}
21
22pub trait FileEnumeratorExt: IsA<FileEnumerator> + 'static {
23    #[doc(alias = "g_file_enumerator_close_async")]
24    fn close_async<P: FnOnce(Result<(), glib::Error>) + 'static>(
25        &self,
26        io_priority: glib::Priority,
27        cancellable: Option<&impl IsA<Cancellable>>,
28        callback: P,
29    ) {
30        let main_context = glib::MainContext::ref_thread_default();
31        let is_main_context_owner = main_context.is_owner();
32        let has_acquired_main_context = (!is_main_context_owner)
33            .then(|| main_context.acquire().ok())
34            .flatten();
35        assert!(
36            is_main_context_owner || has_acquired_main_context.is_some(),
37            "Async operations only allowed if the thread is owning the MainContext"
38        );
39
40        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
41            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
42        unsafe extern "C" fn close_async_trampoline<
43            P: FnOnce(Result<(), glib::Error>) + 'static,
44        >(
45            _source_object: *mut glib::gobject_ffi::GObject,
46            res: *mut crate::ffi::GAsyncResult,
47            user_data: glib::ffi::gpointer,
48        ) {
49            unsafe {
50                let mut error = std::ptr::null_mut();
51                ffi::g_file_enumerator_close_finish(_source_object as *mut _, res, &mut error);
52                let result = if error.is_null() {
53                    Ok(())
54                } else {
55                    Err(from_glib_full(error))
56                };
57                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
58                    Box_::from_raw(user_data as *mut _);
59                let callback: P = callback.into_inner();
60                callback(result);
61            }
62        }
63        let callback = close_async_trampoline::<P>;
64        unsafe {
65            ffi::g_file_enumerator_close_async(
66                self.as_ref().to_glib_none().0,
67                io_priority.into_glib(),
68                cancellable.map(|p| p.as_ref()).to_glib_none().0,
69                Some(callback),
70                Box_::into_raw(user_data) as *mut _,
71            );
72        }
73    }
74
75    fn close_future(
76        &self,
77        io_priority: glib::Priority,
78    ) -> Pin<Box_<dyn std::future::Future<Output = Result<(), glib::Error>> + 'static>> {
79        Box_::pin(crate::GioFuture::new(
80            self,
81            move |obj, cancellable, send| {
82                obj.close_async(io_priority, Some(cancellable), move |res| {
83                    send.resolve(res);
84                });
85            },
86        ))
87    }
88
89    #[doc(alias = "g_file_enumerator_get_child")]
90    #[doc(alias = "get_child")]
91    fn child(&self, info: &FileInfo) -> File {
92        unsafe {
93            from_glib_full(ffi::g_file_enumerator_get_child(
94                self.as_ref().to_glib_none().0,
95                info.to_glib_none().0,
96            ))
97        }
98    }
99
100    #[doc(alias = "g_file_enumerator_get_container")]
101    #[doc(alias = "get_container")]
102    fn container(&self) -> File {
103        unsafe {
104            from_glib_none(ffi::g_file_enumerator_get_container(
105                self.as_ref().to_glib_none().0,
106            ))
107        }
108    }
109
110    #[doc(alias = "g_file_enumerator_has_pending")]
111    fn has_pending(&self) -> bool {
112        unsafe {
113            from_glib(ffi::g_file_enumerator_has_pending(
114                self.as_ref().to_glib_none().0,
115            ))
116        }
117    }
118
119    #[doc(alias = "g_file_enumerator_is_closed")]
120    fn is_closed(&self) -> bool {
121        unsafe {
122            from_glib(ffi::g_file_enumerator_is_closed(
123                self.as_ref().to_glib_none().0,
124            ))
125        }
126    }
127
128    #[doc(alias = "g_file_enumerator_next_file")]
129    fn next_file(
130        &self,
131        cancellable: Option<&impl IsA<Cancellable>>,
132    ) -> Result<Option<FileInfo>, glib::Error> {
133        unsafe {
134            let mut error = std::ptr::null_mut();
135            let ret = ffi::g_file_enumerator_next_file(
136                self.as_ref().to_glib_none().0,
137                cancellable.map(|p| p.as_ref()).to_glib_none().0,
138                &mut error,
139            );
140            if error.is_null() {
141                Ok(from_glib_full(ret))
142            } else {
143                Err(from_glib_full(error))
144            }
145        }
146    }
147
148    #[doc(alias = "g_file_enumerator_next_files_async")]
149    fn next_files_async<P: FnOnce(Result<Vec<FileInfo>, glib::Error>) + 'static>(
150        &self,
151        num_files: i32,
152        io_priority: glib::Priority,
153        cancellable: Option<&impl IsA<Cancellable>>,
154        callback: P,
155    ) {
156        let main_context = glib::MainContext::ref_thread_default();
157        let is_main_context_owner = main_context.is_owner();
158        let has_acquired_main_context = (!is_main_context_owner)
159            .then(|| main_context.acquire().ok())
160            .flatten();
161        assert!(
162            is_main_context_owner || has_acquired_main_context.is_some(),
163            "Async operations only allowed if the thread is owning the MainContext"
164        );
165
166        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
167            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
168        unsafe extern "C" fn next_files_async_trampoline<
169            P: FnOnce(Result<Vec<FileInfo>, glib::Error>) + 'static,
170        >(
171            _source_object: *mut glib::gobject_ffi::GObject,
172            res: *mut crate::ffi::GAsyncResult,
173            user_data: glib::ffi::gpointer,
174        ) {
175            unsafe {
176                let mut error = std::ptr::null_mut();
177                let ret = ffi::g_file_enumerator_next_files_finish(
178                    _source_object as *mut _,
179                    res,
180                    &mut error,
181                );
182                let result = if error.is_null() {
183                    Ok(FromGlibPtrContainer::from_glib_full(ret))
184                } else {
185                    Err(from_glib_full(error))
186                };
187                let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
188                    Box_::from_raw(user_data as *mut _);
189                let callback: P = callback.into_inner();
190                callback(result);
191            }
192        }
193        let callback = next_files_async_trampoline::<P>;
194        unsafe {
195            ffi::g_file_enumerator_next_files_async(
196                self.as_ref().to_glib_none().0,
197                num_files,
198                io_priority.into_glib(),
199                cancellable.map(|p| p.as_ref()).to_glib_none().0,
200                Some(callback),
201                Box_::into_raw(user_data) as *mut _,
202            );
203        }
204    }
205
206    fn next_files_future(
207        &self,
208        num_files: i32,
209        io_priority: glib::Priority,
210    ) -> Pin<Box_<dyn std::future::Future<Output = Result<Vec<FileInfo>, glib::Error>> + 'static>>
211    {
212        Box_::pin(crate::GioFuture::new(
213            self,
214            move |obj, cancellable, send| {
215                obj.next_files_async(num_files, io_priority, Some(cancellable), move |res| {
216                    send.resolve(res);
217                });
218            },
219        ))
220    }
221
222    #[doc(alias = "g_file_enumerator_set_pending")]
223    fn set_pending(&self, pending: bool) {
224        unsafe {
225            ffi::g_file_enumerator_set_pending(self.as_ref().to_glib_none().0, pending.into_glib());
226        }
227    }
228}
229
230impl<O: IsA<FileEnumerator>> FileEnumeratorExt for O {}