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
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 {
#[allow(clippy::new_ret_no_self)]
#[track_caller]
pub fn new<O: Sized + IsClass + IsA<Object> + IsA<Initable>, P: IsA<Cancellable>>(
properties: &[(&str, &dyn ToValue)],
cancellable: Option<&P>,
) -> Result<O, glib::Error> {
Self::with_type(O::static_type(), properties, cancellable)
.map(|o| unsafe { o.unsafe_cast() })
}
#[track_caller]
pub fn with_type(
type_: Type,
properties: &[(&str, &dyn ToValue)],
cancellable: Option<&impl IsA<Cancellable>>,
) -> Result<Object, glib::Error> {
if !type_.is_a(Initable::static_type()) {
panic!("Type '{type_}' is not initable");
}
let mut property_values = smallvec::SmallVec::<[_; 16]>::with_capacity(properties.len());
for (name, value) in properties {
property_values.push((*name, value.to_value()));
}
unsafe {
let object = Object::new_internal(type_, &mut property_values);
object.unsafe_cast_ref::<Self>().init(cancellable)?;
Ok(object)
}
}
#[track_caller]
pub fn with_values(
type_: Type,
properties: &[(&str, glib::Value)],
cancellable: Option<&impl IsA<Cancellable>>,
) -> Result<Object, glib::Error> {
if !type_.is_a(Initable::static_type()) {
panic!("Type '{type_}' is not initable");
}
let mut property_values = smallvec::SmallVec::<[_; 16]>::with_capacity(properties.len());
for (name, value) in properties {
property_values.push((*name, value.clone()));
}
unsafe {
let object = Object::new_internal(type_, &mut property_values);
object.unsafe_cast_ref::<Self>().init(cancellable)?;
Ok(object)
}
}
}