-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathconversion_type.rs
111 lines (104 loc) · 4.18 KB
/
conversion_type.rs
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
use crate::{env, library::*};
use std::sync::Arc;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ConversionType {
/// Coded without conversion.
Direct,
/// Coded with from_glib.
Scalar,
/// Type implementing TryFromGlib<Error=GlibNoneError>.
Option,
/// Type implementing TryFromGlib<Err> where Err is neither GlibNoneError
/// nor GlibNoneOrInvalidError. Embeds the Error type name.
/// Defaults to the object's type for the `Ok` variant if `ok_type` is `None`.
Result {
ok_type: Arc<str>,
err_type: Arc<str>,
},
/// Coded with from_glib_xxx.
Pointer,
// Same as Pointer, except that use from_glib_borrow instead from_glib_none.
Borrow,
Unknown,
}
impl Default for ConversionType {
fn default() -> Self {
ConversionType::Unknown
}
}
impl ConversionType {
pub fn of(env: &env::Env, type_id: TypeId) -> ConversionType {
let library = &env.library;
if let Some(conversion_type) = env
.config
.objects
.get(&type_id.full_name(library))
.and_then(|gobject| gobject.conversion_type.clone())
{
return conversion_type;
}
use crate::library::{Basic::*, Type::*};
match library.type_(type_id) {
Basic(fund) => match fund {
Boolean => ConversionType::Scalar,
Int8 => ConversionType::Direct,
UInt8 => ConversionType::Direct,
Int16 => ConversionType::Direct,
UInt16 => ConversionType::Direct,
Int32 => ConversionType::Direct,
UInt32 => ConversionType::Direct,
Int64 => ConversionType::Direct,
UInt64 => ConversionType::Direct,
Char => ConversionType::Scalar,
UChar => ConversionType::Scalar,
Short => ConversionType::Direct,
UShort => ConversionType::Direct,
Int => ConversionType::Direct,
UInt => ConversionType::Direct,
Long => ConversionType::Direct,
ULong => ConversionType::Direct,
Size => ConversionType::Direct,
SSize => ConversionType::Direct,
Float => ConversionType::Direct,
Double => ConversionType::Direct,
UniChar => ConversionType::Scalar,
Pointer => ConversionType::Pointer,
VarArgs => ConversionType::Unknown,
Utf8 => ConversionType::Pointer,
Filename => ConversionType::Pointer,
OsString => ConversionType::Pointer,
Type => ConversionType::Scalar,
None => ConversionType::Unknown,
IntPtr => ConversionType::Direct,
UIntPtr => ConversionType::Direct,
Bool => ConversionType::Direct,
Typedef(_) => ConversionType::Direct,
Unsupported => ConversionType::Unknown,
},
Alias(alias) if alias.c_identifier == "GQuark" => ConversionType::Scalar,
Alias(alias) => ConversionType::of(env, alias.typ),
Bitfield(_) => ConversionType::Scalar,
Record(_) => ConversionType::Pointer,
Union(_) => ConversionType::Pointer,
Enumeration(_) => ConversionType::Scalar,
Interface(_) => ConversionType::Pointer,
Class(_) => ConversionType::Pointer,
CArray(_) => ConversionType::Pointer,
FixedArray(..) => ConversionType::Pointer,
List(_) => ConversionType::Pointer,
SList(_) => ConversionType::Pointer,
PtrArray(_) => ConversionType::Pointer,
Function(super::library::Function { name, .. }) if name == "AsyncReadyCallback" => {
ConversionType::Direct
}
Function(_) => ConversionType::Direct,
Custom(super::library::Custom {
conversion_type, ..
}) => conversion_type.clone(),
_ => ConversionType::Unknown,
}
}
pub fn can_use_to_generate(&self) -> bool {
matches!(self, ConversionType::Option | ConversionType::Result { .. })
}
}