gio/auto/
initable.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, Cancellable};
6use glib::{prelude::*, translate::*};
7
8glib::wrapper! {
9    /// `GInitable` is implemented by objects that can fail during
10    /// initialization. If an object implements this interface then
11    /// it must be initialized as the first thing after construction,
12    /// either via [`InitableExt::init()`][crate::prelude::InitableExt::init()] or [`AsyncInitableExt::init_async()`][crate::prelude::AsyncInitableExt::init_async()]
13    /// (the latter is only available if it also implements [`AsyncInitable`][crate::AsyncInitable]).
14    ///
15    /// If the object is not initialized, or initialization returns with an
16    /// error, then all operations on the object except `g_object_ref()` and
17    /// `g_object_unref()` are considered to be invalid, and have undefined
18    /// behaviour. They will often fail with `critical()` or
19    /// `warning()`, but this must not be relied on.
20    ///
21    /// Users of objects implementing this are not intended to use
22    /// the interface method directly, instead it will be used automatically
23    /// in various ways. For C applications you generally just call
24    /// [`new()`][Self::new()] directly, or indirectly via a `foo_thing_new()` wrapper.
25    /// This will call [`InitableExt::init()`][crate::prelude::InitableExt::init()] under the cover, returning `NULL`
26    /// and setting a `GError` on failure (at which point the instance is
27    /// unreferenced).
28    ///
29    /// For bindings in languages where the native constructor supports
30    /// exceptions the binding could check for objects implementing `GInitable`
31    /// during normal construction and automatically initialize them, throwing
32    /// an exception on failure.
33    ///
34    /// # Implements
35    ///
36    /// [`InitableExt`][trait@crate::prelude::InitableExt]
37    #[doc(alias = "GInitable")]
38    pub struct Initable(Interface<ffi::GInitable, ffi::GInitableIface>);
39
40    match fn {
41        type_ => || ffi::g_initable_get_type(),
42    }
43}
44
45impl Initable {
46    pub const NONE: Option<&'static Initable> = None;
47}
48
49/// Trait containing all [`struct@Initable`] methods.
50///
51/// # Implementors
52///
53/// [`CharsetConverter`][struct@crate::CharsetConverter], [`DBusConnection`][struct@crate::DBusConnection], [`DBusProxy`][struct@crate::DBusProxy], [`DBusServer`][struct@crate::DBusServer], [`DebugControllerDBus`][struct@crate::DebugControllerDBus], [`DebugController`][struct@crate::DebugController], [`InetAddressMask`][struct@crate::InetAddressMask], [`Initable`][struct@crate::Initable], [`MemoryMonitor`][struct@crate::MemoryMonitor], [`NetworkMonitor`][struct@crate::NetworkMonitor], [`PowerProfileMonitor`][struct@crate::PowerProfileMonitor], [`Socket`][struct@crate::Socket], [`Subprocess`][struct@crate::Subprocess]
54pub trait InitableExt: IsA<Initable> + 'static {
55    /// Initializes the object implementing the interface.
56    ///
57    /// This method is intended for language bindings. If writing in C,
58    /// g_initable_new() should typically be used instead.
59    ///
60    /// The object must be initialized before any real use after initial
61    /// construction, either with this function or g_async_initable_init_async().
62    ///
63    /// Implementations may also support cancellation. If @cancellable is not [`None`],
64    /// then initialization can be cancelled by triggering the cancellable object
65    /// from another thread. If the operation was cancelled, the error
66    /// [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned. If @cancellable is not [`None`] and
67    /// the object doesn't support cancellable initialization the error
68    /// [`IOErrorEnum::NotSupported`][crate::IOErrorEnum::NotSupported] will be returned.
69    ///
70    /// If the object is not initialized, or initialization returns with an
71    /// error, then all operations on the object except g_object_ref() and
72    /// g_object_unref() are considered to be invalid, and have undefined
73    /// behaviour. See the [description][`Initable`][crate::Initable]#description] for more details.
74    ///
75    /// Callers should not assume that a class which implements #GInitable can be
76    /// initialized multiple times, unless the class explicitly documents itself as
77    /// supporting this. Generally, a class’ implementation of init() can assume
78    /// (and assert) that it will only be called once. Previously, this documentation
79    /// recommended all #GInitable implementations should be idempotent; that
80    /// recommendation was relaxed in GLib 2.54.
81    ///
82    /// If a class explicitly supports being initialized multiple times, it is
83    /// recommended that the method is idempotent: multiple calls with the same
84    /// arguments should return the same results. Only the first call initializes
85    /// the object; further calls return the result of the first call.
86    ///
87    /// One reason why a class might need to support idempotent initialization is if
88    /// it is designed to be used via the singleton pattern, with a
89    /// #GObjectClass.constructor that sometimes returns an existing instance.
90    /// In this pattern, a caller would expect to be able to call g_initable_init()
91    /// on the result of g_object_new(), regardless of whether it is in fact a new
92    /// instance.
93    /// ## `cancellable`
94    /// optional #GCancellable object, [`None`] to ignore.
95    ///
96    /// # Returns
97    ///
98    /// [`true`] if successful. If an error has occurred, this function will
99    ///     return [`false`] and set @error appropriately if present.
100    #[doc(alias = "g_initable_init")]
101    unsafe fn init(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<(), glib::Error> {
102        let mut error = std::ptr::null_mut();
103        let is_ok = ffi::g_initable_init(
104            self.as_ref().to_glib_none().0,
105            cancellable.map(|p| p.as_ref()).to_glib_none().0,
106            &mut error,
107        );
108        debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
109        if error.is_null() {
110            Ok(())
111        } else {
112            Err(from_glib_full(error))
113        }
114    }
115}
116
117impl<O: IsA<Initable>> InitableExt for O {}