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
// Take a look at the license at the top of the repository in the LICENSE file.
use crate::traits::InitableExt;
use crate::Cancellable;
use crate::Initable;
use glib::object::IsA;
use glib::object::IsClass;
use glib::value::ToValue;
use glib::{Cast, Object, StaticType, Type};
impl Initable {
// rustdoc-stripper-ignore-next
/// Create a new instance of an initable object with the given properties.
///
/// Similar to [`Object::new`] but can fail either because the object
/// creation failed or because `Initable::init` failed.
// rustdoc-stripper-ignore-next-stop
/// Helper function for constructing [`Initable`][crate::Initable] object. This is
/// similar to [`glib::Object::new()`][crate::glib::Object::new()] but also initializes the object
/// and returns [`None`], setting an error on failure.
/// ## `object_type`
/// a `GType` supporting [`Initable`][crate::Initable].
/// ## `cancellable`
/// optional [`Cancellable`][crate::Cancellable] object, [`None`] to ignore.
/// ## `first_property_name`
/// the name of the first property, or [`None`] if no
/// properties
///
/// # Returns
///
/// a newly allocated
/// [`glib::Object`][crate::glib::Object], or [`None`] on error
#[allow(clippy::new_ret_no_self)]
pub fn new<O: Sized + IsClass + IsA<Object> + IsA<Initable>, P: IsA<Cancellable>>(
properties: &[(&str, &dyn ToValue)],
cancellable: Option<&P>,
) -> Result<O, InitableError> {
let object = Object::new::<O>(properties)?;
unsafe { object.init(cancellable)? };
Ok(object)
}
// rustdoc-stripper-ignore-next
/// Create a new instance of an initable object of the given type with the given properties.
///
/// Similar to [`Object::with_type`] but can fail either because the object
/// creation failed or because `Initable::init` failed.
pub fn with_type(
type_: Type,
properties: &[(&str, &dyn ToValue)],
cancellable: Option<&impl IsA<Cancellable>>,
) -> Result<Object, InitableError> {
if !type_.is_a(Initable::static_type()) {
return Err(InitableError::NewObjectFailed(glib::bool_error!(
"Type '{}' is not initable",
type_
)));
}
let object = Object::with_type(type_, properties)?;
unsafe { object.unsafe_cast_ref::<Self>().init(cancellable)? };
Ok(object)
}
}
#[derive(thiserror::Error, Debug)]
pub enum InitableError {
#[error("Object::new failed with {0:?}")]
NewObjectFailed(#[from] glib::error::BoolError),
#[error("Initable::init failed with {0:?}")]
InitFailed(#[from] glib::Error),
}