Skip to main content

gio/auto/
cancellable.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;
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    /// `GCancellable` allows operations to be cancelled.
10    ///
11    /// `GCancellable` is a thread-safe operation cancellation stack used
12    /// throughout GIO to allow for cancellation of synchronous and
13    /// asynchronous operations.
14    ///
15    /// ## Signals
16    ///
17    ///
18    /// #### `cancelled`
19    ///
20    ///     // Make sure we don't do unnecessary work if already cancelled
21    ///     if (g_cancellable_set_error_if_cancelled (cancellable, error))
22    ///       return;
23    ///
24    ///     // Set up all the data needed to be able to handle cancellation
25    ///     // of the operation
26    ///     my_data = my_data_new (...);
27    ///
28    ///     id = 0;
29    ///     if (cancellable)
30    ///       id = g_cancellable_connect (cancellable,
31    ///                       G_CALLBACK (cancelled_handler)
32    ///                       data, NULL);
33    ///
34    ///     // cancellable operation here...
35    ///
36    ///     g_cancellable_disconnect (cancellable, id);
37    ///
38    ///     // cancelled_handler is never called after this, it is now safe
39    ///     // to free the data
40    ///     my_data_free (my_data);
41    /// ]|
42    ///
43    /// Note that the cancelled signal is emitted in the thread that
44    /// the user cancelled from, which may be the main thread. So, the
45    /// cancellable signal should not do something that can block.
46    ///
47    ///
48    ///
49    /// # Implements
50    ///
51    /// [`CancellableExt`][trait@crate::prelude::CancellableExt], [`trait@glib::ObjectExt`], [`CancellableExtManual`][trait@crate::prelude::CancellableExtManual]
52    #[doc(alias = "GCancellable")]
53    pub struct Cancellable(Object<ffi::GCancellable, ffi::GCancellableClass>);
54
55    match fn {
56        type_ => || ffi::g_cancellable_get_type(),
57    }
58}
59
60impl Cancellable {
61    pub const NONE: Option<&'static Cancellable> = None;
62
63    /// Creates a new #GCancellable object.
64    ///
65    /// Applications that want to start one or more operations
66    /// that should be cancellable should create a #GCancellable
67    /// and pass it to the operations.
68    ///
69    /// One #GCancellable can be used in multiple consecutive
70    /// operations or in multiple concurrent operations.
71    ///
72    /// # Returns
73    ///
74    /// a #GCancellable.
75    #[doc(alias = "g_cancellable_new")]
76    pub fn new() -> Cancellable {
77        unsafe { from_glib_full(ffi::g_cancellable_new()) }
78    }
79
80    /// Gets the top cancellable from the stack.
81    ///
82    /// # Returns
83    ///
84    /// a #GCancellable from the top
85    /// of the stack, or [`None`] if the stack is empty.
86    #[doc(alias = "g_cancellable_get_current")]
87    #[doc(alias = "get_current")]
88    pub fn current() -> Option<Cancellable> {
89        unsafe { from_glib_none(ffi::g_cancellable_get_current()) }
90    }
91}
92
93impl Default for Cancellable {
94    fn default() -> Self {
95        Self::new()
96    }
97}
98
99unsafe impl Send for Cancellable {}
100unsafe impl Sync for Cancellable {}
101
102/// Trait containing all [`struct@Cancellable`] methods.
103///
104/// # Implementors
105///
106/// [`Cancellable`][struct@crate::Cancellable]
107pub trait CancellableExt: IsA<Cancellable> + 'static {
108    /// Will set @self to cancelled, and will emit the
109    /// #GCancellable::cancelled signal. (However, see the warning about
110    /// race conditions in the documentation for that signal if you are
111    /// planning to connect to it.)
112    ///
113    /// This function is thread-safe. In other words, you can safely call
114    /// it from a thread other than the one running the operation that was
115    /// passed the @self.
116    ///
117    /// If @self is [`None`], this function returns immediately for convenience.
118    ///
119    /// The convention within GIO is that cancelling an asynchronous
120    /// operation causes it to complete asynchronously. That is, if you
121    /// cancel the operation from the same thread in which it is running,
122    /// then the operation's #GAsyncReadyCallback will not be invoked until
123    /// the application returns to the main loop.
124    ///
125    /// It is safe (although useless, since it will be a no-op) to call
126    /// this function from a [`cancelled`][struct@crate::Cancellable#cancelled] signal handler.
127    #[doc(alias = "g_cancellable_cancel")]
128    fn cancel(&self) {
129        unsafe {
130            ffi::g_cancellable_cancel(self.as_ref().to_glib_none().0);
131        }
132    }
133
134    /// Gets the file descriptor for a cancellable job. This can be used to
135    /// implement cancellable operations on Unix systems. The returned fd will
136    /// turn readable when @self is cancelled.
137    ///
138    /// You are not supposed to read from the fd yourself, just check for
139    /// readable status. Reading to unset the readable status is done
140    /// with g_cancellable_reset().
141    ///
142    /// After a successful return from this function, you should use
143    /// g_cancellable_release_fd() to free up resources allocated for
144    /// the returned file descriptor.
145    ///
146    /// See also g_cancellable_make_pollfd().
147    ///
148    /// # Returns
149    ///
150    /// A valid file descriptor. `-1` if the file descriptor
151    /// is not supported, or on errors.
152    #[doc(alias = "g_cancellable_get_fd")]
153    #[doc(alias = "get_fd")]
154    fn fd(&self) -> i32 {
155        unsafe { ffi::g_cancellable_get_fd(self.as_ref().to_glib_none().0) }
156    }
157
158    /// Checks if a cancellable job has been cancelled.
159    ///
160    /// # Returns
161    ///
162    /// [`true`] if @self is cancelled,
163    /// FALSE if called with [`None`] or if item is not cancelled.
164    #[doc(alias = "g_cancellable_is_cancelled")]
165    fn is_cancelled(&self) -> bool {
166        unsafe {
167            from_glib(ffi::g_cancellable_is_cancelled(
168                self.as_ref().to_glib_none().0,
169            ))
170        }
171    }
172
173    //#[doc(alias = "g_cancellable_make_pollfd")]
174    //fn make_pollfd(&self, pollfd: /*Ignored*/&mut glib::PollFD) -> bool {
175    //    unsafe { TODO: call ffi:g_cancellable_make_pollfd() }
176    //}
177
178    /// Pops @self off the cancellable stack (verifying that @self
179    /// is on the top of the stack).
180    #[doc(alias = "g_cancellable_pop_current")]
181    fn pop_current(&self) {
182        unsafe {
183            ffi::g_cancellable_pop_current(self.as_ref().to_glib_none().0);
184        }
185    }
186
187    /// Pushes @self onto the cancellable stack. The current
188    /// cancellable can then be received using g_cancellable_get_current().
189    ///
190    /// This is useful when implementing cancellable operations in
191    /// code that does not allow you to pass down the cancellable object.
192    ///
193    /// This is typically called automatically by e.g. #GFile operations,
194    /// so you rarely have to call this yourself.
195    #[doc(alias = "g_cancellable_push_current")]
196    fn push_current(&self) {
197        unsafe {
198            ffi::g_cancellable_push_current(self.as_ref().to_glib_none().0);
199        }
200    }
201
202    /// Releases a resources previously allocated by g_cancellable_get_fd()
203    /// or g_cancellable_make_pollfd().
204    ///
205    /// For compatibility reasons with older releases, calling this function
206    /// is not strictly required, the resources will be automatically freed
207    /// when the @self is finalized. However, the @self will
208    /// block scarce file descriptors until it is finalized if this function
209    /// is not called. This can cause the application to run out of file
210    /// descriptors when many #GCancellables are used at the same time.
211    ///
212    /// Note that in the event that a [`cancelled`][struct@crate::Cancellable#cancelled] signal handler is
213    /// currently running, this call will block until the handler has finished.
214    /// Calling this function from a signal handler will therefore result in a
215    /// deadlock.
216    #[doc(alias = "g_cancellable_release_fd")]
217    fn release_fd(&self) {
218        unsafe {
219            ffi::g_cancellable_release_fd(self.as_ref().to_glib_none().0);
220        }
221    }
222}
223
224impl<O: IsA<Cancellable>> CancellableExt for O {}