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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::borrow::Cow;
use std::fmt;
use clang::{Entity, Type};
use crate::type_ref::{ConstnessOverride, CppNameStyle, FishStyle, NameStyle, TemplateArg};
use crate::{
DefaultElement, DefinitionLocation, DependentType, DependentTypeMode, Element, EntityElement, GeneratorEnv, ReturnTypeWrapper,
TypeRef,
};
#[derive(Clone)]
pub struct Vector<'tu, 'ge> {
type_ref: Type<'tu>,
gen_env: &'ge GeneratorEnv<'tu>,
}
impl<'tu, 'ge> Vector<'tu, 'ge> {
pub fn new(type_ref: Type<'tu>, gen_env: &'ge GeneratorEnv<'tu>) -> Self {
Self { type_ref, gen_env }
}
pub fn is_data_type(&self, type_ref: &TypeRef) -> bool {
type_ref.is_data_type() || type_ref.as_vector().map_or(false, |v| v.element_type().is_data_type())
}
pub fn type_ref(&self) -> TypeRef<'tu, 'ge> {
TypeRef::new(self.type_ref, self.gen_env)
}
pub fn element_type(&self) -> TypeRef<'tu, 'ge> {
self
.type_ref()
.template_specialization_args()
.into_iter()
.find_map(TemplateArg::into_typename)
.expect("vector template argument list is empty")
}
pub fn dependent_types(&self) -> Vec<DependentType<'tu, 'ge>> {
let element_type = self.element_type();
let is_data_type = self.is_data_type(&element_type);
let mut out = element_type.dependent_types(DependentTypeMode::ForReturn(DefinitionLocation::Type));
out.reserve(
1 + if is_data_type {
3
} else {
0
},
);
if element_type.as_string().is_some() {
out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
TypeRef::new(
self
.gen_env
.resolve_type(&element_type.cpp_extern_return(ConstnessOverride::No))
.expect("Can't resolve string cpp_extern_return()"),
self.gen_env,
),
DefinitionLocation::Custom(element_type.rust_module().into_owned()),
self.gen_env,
)));
} else {
out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
element_type.canonical_clang(),
DefinitionLocation::Module,
self.gen_env,
)));
}
if is_data_type {
out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
TypeRef::new(
self
.gen_env
.resolve_type("cv::_InputArray")
.expect("Can't resolve _InputArray"),
self.gen_env,
),
DefinitionLocation::Custom(element_type.rust_module().into_owned()),
self.gen_env,
)));
out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
TypeRef::new(
self
.gen_env
.resolve_type("cv::_OutputArray")
.expect("Can't resolve _OutputArray"),
self.gen_env,
),
DefinitionLocation::Custom(element_type.rust_module().into_owned()),
self.gen_env,
)));
out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
TypeRef::new(
self
.gen_env
.resolve_type("cv::_InputOutputArray")
.expect("Can't resolve _InputOutputArray"),
self.gen_env,
),
DefinitionLocation::Custom(element_type.rust_module().into_owned()),
self.gen_env,
)));
}
out
}
pub fn rust_localalias(&self) -> Cow<str> {
format!("VectorOf{typ}", typ = self.element_type().rust_safe_id(true)).into()
}
}
impl<'tu> EntityElement<'tu> for Vector<'tu, '_> {
fn entity(&self) -> Entity<'tu> {
self.type_ref.get_declaration().expect("Can't get declaration")
}
}
impl Element for Vector<'_, '_> {
fn is_ignored(&self) -> bool {
DefaultElement::is_ignored(self) || self.element_type().is_ignored()
}
fn is_system(&self) -> bool {
DefaultElement::is_system(self)
}
fn is_public(&self) -> bool {
DefaultElement::is_public(self)
}
fn usr(&self) -> Cow<str> {
DefaultElement::usr(self)
}
fn rendered_doc_comment_with_prefix(&self, prefix: &str, opencv_version: &str) -> String {
DefaultElement::rendered_doc_comment_with_prefix(self, prefix, opencv_version)
}
fn cpp_namespace(&self) -> Cow<str> {
"std".into()
}
fn cpp_name(&self, style: CppNameStyle) -> Cow<str> {
DefaultElement::cpp_name(self, style)
}
fn rust_module(&self) -> Cow<str> {
self.element_type().rust_module().into_owned().into()
}
fn rust_namespace(&self) -> Cow<str> {
"core".into()
}
fn rust_name(&self, style: NameStyle) -> Cow<str> {
DefaultElement::rust_name(self, style)
}
fn rust_leafname(&self, fish_style: FishStyle) -> Cow<str> {
let mut inner_typ = self.element_type();
if let Some(inner) = inner_typ.as_pointer() {
inner_typ = inner;
}
format!(
"Vector{fish}<{typ}>",
fish = fish_style.rust_qual(),
typ = inner_typ.rust_name(NameStyle::ref_()),
)
.into()
}
}
impl fmt::Display for Vector<'_, '_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.entity().get_display_name().expect("Can't get display name"))
}
}
impl fmt::Debug for Vector<'_, '_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut debug_struct = f.debug_struct("Vector");
self
.update_debug_struct(&mut debug_struct)
.field("export_config", &self.gen_env.get_export_config(self.entity()))
.field("element_type", &self.element_type())
.finish()
}
}