1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::Cancellable;
use glib::{prelude::*, translate::*};
use std::{fmt, ptr};

glib::wrapper! {
    /// [`Initable`][crate::Initable] is implemented by objects that can fail during
    /// initialization. If an object implements this interface then
    /// it must be initialized as the first thing after construction,
    /// either via [`InitableExt::init()`][crate::prelude::InitableExt::init()] or [`AsyncInitableExt::init_async()`][crate::prelude::AsyncInitableExt::init_async()]
    /// (the latter is only available if it also implements [`AsyncInitable`][crate::AsyncInitable]).
    ///
    /// If the object is not initialized, or initialization returns with an
    /// error, then all operations on the object except `g_object_ref()` and
    /// `g_object_unref()` are considered to be invalid, and have undefined
    /// behaviour. They will often fail with `g_critical()` or `g_warning()`, but
    /// this must not be relied on.
    ///
    /// Users of objects implementing this are not intended to use
    /// the interface method directly, instead it will be used automatically
    /// in various ways. For C applications you generally just call
    /// [`new()`][Self::new()] directly, or indirectly via a `foo_thing_new()` wrapper.
    /// This will call [`InitableExt::init()`][crate::prelude::InitableExt::init()] under the cover, returning [`None`] and
    /// setting a [`glib::Error`][crate::glib::Error] on failure (at which point the instance is
    /// unreferenced).
    ///
    /// For bindings in languages where the native constructor supports
    /// exceptions the binding could check for objects implementing `GInitable`
    /// during normal construction and automatically initialize them, throwing
    /// an exception on failure.
    ///
    /// # Implements
    ///
    /// [`InitableExt`][trait@crate::prelude::InitableExt]
    #[doc(alias = "GInitable")]
    pub struct Initable(Interface<ffi::GInitable, ffi::GInitableIface>);

    match fn {
        type_ => || ffi::g_initable_get_type(),
    }
}

impl Initable {
    pub const NONE: Option<&'static Initable> = None;
}

/// Trait containing all [`struct@Initable`] methods.
///
/// # Implementors
///
/// [`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]
pub trait InitableExt: 'static {
    /// Initializes the object implementing the interface.
    ///
    /// This method is intended for language bindings. If writing in C,
    /// [`Initable::new()`][crate::Initable::new()] should typically be used instead.
    ///
    /// The object must be initialized before any real use after initial
    /// construction, either with this function or [`AsyncInitableExt::init_async()`][crate::prelude::AsyncInitableExt::init_async()].
    ///
    /// Implementations may also support cancellation. If `cancellable` is not [`None`],
    /// then initialization can be cancelled by triggering the cancellable object
    /// from another thread. If the operation was cancelled, the error
    /// [`IOErrorEnum::Cancelled`][crate::IOErrorEnum::Cancelled] will be returned. If `cancellable` is not [`None`] and
    /// the object doesn't support cancellable initialization the error
    /// [`IOErrorEnum::NotSupported`][crate::IOErrorEnum::NotSupported] will be returned.
    ///
    /// If the object is not initialized, or initialization returns with an
    /// error, then all operations on the object except `g_object_ref()` and
    /// `g_object_unref()` are considered to be invalid, and have undefined
    /// behaviour. See the [introduction][ginitable] for more details.
    ///
    /// Callers should not assume that a class which implements [`Initable`][crate::Initable] can be
    /// initialized multiple times, unless the class explicitly documents itself as
    /// supporting this. Generally, a class’ implementation of `init()` can assume
    /// (and assert) that it will only be called once. Previously, this documentation
    /// recommended all [`Initable`][crate::Initable] implementations should be idempotent; that
    /// recommendation was relaxed in GLib 2.54.
    ///
    /// If a class explicitly supports being initialized multiple times, it is
    /// recommended that the method is idempotent: multiple calls with the same
    /// arguments should return the same results. Only the first call initializes
    /// the object; further calls return the result of the first call.
    ///
    /// One reason why a class might need to support idempotent initialization is if
    /// it is designed to be used via the singleton pattern, with a
    /// `GObjectClass.constructor` that sometimes returns an existing instance.
    /// In this pattern, a caller would expect to be able to call [`init()`][Self::init()]
    /// on the result of [`glib::Object::new()`][crate::glib::Object::new()], regardless of whether it is in fact a new
    /// instance.
    /// ## `cancellable`
    /// optional [`Cancellable`][crate::Cancellable] object, [`None`] to ignore.
    ///
    /// # Returns
    ///
    /// [`true`] if successful. If an error has occurred, this function will
    ///  return [`false`] and set `error` appropriately if present.
    #[doc(alias = "g_initable_init")]
    unsafe fn init(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<(), glib::Error>;
}

impl<O: IsA<Initable>> InitableExt for O {
    unsafe fn init(&self, cancellable: Option<&impl IsA<Cancellable>>) -> Result<(), glib::Error> {
        let mut error = ptr::null_mut();
        let is_ok = ffi::g_initable_init(
            self.as_ref().to_glib_none().0,
            cancellable.map(|p| p.as_ref()).to_glib_none().0,
            &mut error,
        );
        debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
        if error.is_null() {
            Ok(())
        } else {
            Err(from_glib_full(error))
        }
    }
}

impl fmt::Display for Initable {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("Initable")
    }
}