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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use translate::{FromGlib, ToGlib, from_glib};
use ffi as glib_ffi;
use gobject_ffi;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Type {
Invalid,
Unit,
I8,
U8,
Bool,
I32,
U32,
ILong,
ULong,
I64,
U64,
F32,
F64,
String,
Pointer,
Variant,
BaseInterface,
BaseEnum,
BaseFlags,
BaseBoxed,
BaseParamSpec,
BaseObject,
Other(usize),
}
pub trait StaticType {
fn static_type() -> Type;
}
impl<'a, T: ?Sized + StaticType> StaticType for &'a T {
fn static_type() -> Type {
T::static_type()
}
}
macro_rules! builtin {
($name:ident, $val:ident) => {
impl StaticType for $name {
fn static_type() -> Type {
Type::$val
}
}
}
}
builtin!(bool, Bool);
builtin!(i8, I8);
builtin!(u8, U8);
builtin!(i32, I32);
builtin!(u32, U32);
builtin!(i64, I64);
builtin!(u64, U64);
builtin!(f32, F32);
builtin!(f64, F64);
builtin!(str, String);
builtin!(String, String);
pub trait InstanceType {
fn instance_type(&self) -> Type;
}
#[inline]
pub fn instance_of<C: StaticType>(ptr: glib_ffi::gconstpointer) -> bool {
unsafe {
from_glib(
gobject_ffi::g_type_check_instance_is_a(
ptr as *mut _, <C as StaticType>::static_type().to_glib()))
}
}
impl FromGlib<glib_ffi::GType> for Type {
#[inline]
fn from_glib(val: glib_ffi::GType) -> Type {
use self::Type::*;
match val {
gobject_ffi::G_TYPE_INVALID => Invalid,
gobject_ffi::G_TYPE_NONE => Unit,
gobject_ffi::G_TYPE_INTERFACE => BaseInterface,
gobject_ffi::G_TYPE_CHAR => I8,
gobject_ffi::G_TYPE_UCHAR => U8,
gobject_ffi::G_TYPE_BOOLEAN => Bool,
gobject_ffi::G_TYPE_INT => I32,
gobject_ffi::G_TYPE_UINT => U32,
gobject_ffi::G_TYPE_LONG => ILong,
gobject_ffi::G_TYPE_ULONG => ULong,
gobject_ffi::G_TYPE_INT64 => I64,
gobject_ffi::G_TYPE_UINT64 => U64,
gobject_ffi::G_TYPE_ENUM => BaseEnum,
gobject_ffi::G_TYPE_FLAGS => BaseFlags,
gobject_ffi::G_TYPE_FLOAT => F32,
gobject_ffi::G_TYPE_DOUBLE => F64,
gobject_ffi::G_TYPE_STRING => String,
gobject_ffi::G_TYPE_POINTER => Pointer,
gobject_ffi::G_TYPE_BOXED => BaseBoxed,
gobject_ffi::G_TYPE_PARAM => BaseParamSpec,
gobject_ffi::G_TYPE_OBJECT => BaseObject,
gobject_ffi::G_TYPE_VARIANT => Variant,
x => Other(x as usize),
}
}
}
impl ToGlib for Type {
type GlibType = glib_ffi::GType;
fn to_glib(&self) -> glib_ffi::GType {
use self::Type::*;
match *self {
Invalid => gobject_ffi::G_TYPE_INVALID,
Unit => gobject_ffi::G_TYPE_NONE,
BaseInterface => gobject_ffi::G_TYPE_INTERFACE,
I8 => gobject_ffi::G_TYPE_CHAR,
U8 => gobject_ffi::G_TYPE_UCHAR,
Bool => gobject_ffi::G_TYPE_BOOLEAN,
I32 => gobject_ffi::G_TYPE_INT,
U32 => gobject_ffi::G_TYPE_UINT,
ILong => gobject_ffi::G_TYPE_LONG,
ULong => gobject_ffi::G_TYPE_ULONG,
I64 => gobject_ffi::G_TYPE_INT64,
U64 => gobject_ffi::G_TYPE_UINT64,
BaseEnum => gobject_ffi::G_TYPE_ENUM,
BaseFlags => gobject_ffi::G_TYPE_FLAGS,
F32 => gobject_ffi::G_TYPE_FLOAT,
F64 => gobject_ffi::G_TYPE_DOUBLE,
String => gobject_ffi::G_TYPE_STRING,
Pointer => gobject_ffi::G_TYPE_POINTER,
BaseBoxed => gobject_ffi::G_TYPE_BOXED,
BaseParamSpec => gobject_ffi::G_TYPE_PARAM,
BaseObject => gobject_ffi::G_TYPE_OBJECT,
Variant => gobject_ffi::G_TYPE_VARIANT,
Other(x) => x as glib_ffi::GType,
}
}
}