Skip to content

Commit 0e65c0a

Browse files
committed
Cleanup
1 parent 6784a7e commit 0e65c0a

File tree

18 files changed

+174
-158
lines changed

18 files changed

+174
-158
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
* 0.93.3
2-
* Fix issue with using Option<Ptr>, now behaves as expected and doesn't segfault.
2+
* Fix issue with using `Option<Ptr>`, now behaves as expected and doesn't segfault.
33
* Introduce `Mat::get_data_dump()` to get the dump of the Mat data, it's also included in the `Debug` output.
44
* Improve formatting of the generated bindings.
55

binding-generator/src/class.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,16 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
5757
/// Checks whether a class can be simple on Rust side, i.e. represented by plain struct with public fields
5858
pub fn can_be_simple(&self) -> bool {
5959
let cpp_refname = self.cpp_name(CppNameStyle::Reference);
60-
if settings::IMPLEMENTED_GENERICS.contains(cpp_refname.as_ref()) {
61-
return true;
62-
}
63-
self.has_fields()
64-
&& !self.has_descendants()
65-
&& !self.has_bases()
66-
&& !self
67-
.for_each_field(|field| {
68-
let type_ref = field.type_ref();
69-
ControlFlow::continue_until(!type_ref.kind().is_copy(type_ref.type_hint()))
70-
})
71-
.is_break()
60+
settings::IMPLEMENTED_GENERICS.contains(cpp_refname.as_ref())
61+
|| self.has_fields()
62+
&& !self.has_descendants()
63+
&& !self.has_bases()
64+
&& !self
65+
.for_each_field(|field| {
66+
let type_ref = field.type_ref();
67+
ControlFlow::continue_until(!type_ref.kind().is_copy(type_ref.type_hint()))
68+
})
69+
.is_break()
7270
}
7371

7472
pub fn kind(&self) -> ClassKind {
@@ -502,13 +500,13 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
502500
&& !fld_type_kind.as_fixed_array().is_some()
503501
{
504502
let cpp_name = fld.cpp_name(CppNameStyle::Declaration);
505-
let (first_letter, rest) = cpp_name.split_at(1);
503+
let (first_letter, rest) = cpp_name.capitalize_first_ascii_letter().expect("Empty fld_declname");
506504
Some(Func::new_desc(
507505
FuncDesc::new(
508506
FuncKind::FieldAccessor(self.clone(), fld.clone()),
509507
Constness::Mut,
510508
ReturnKind::InfallibleNaked,
511-
format!("set{}{rest}", first_letter.to_uppercase()),
509+
format!("set{first_letter}{rest}"),
512510
rust_module.clone(),
513511
[Field::new_desc(FieldDesc {
514512
cpp_fullname: "val".into(),

binding-generator/src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn render_constant_rust(tokens: &[Token]) -> Option<Value> {
4646
out.kind = ValueKind::String;
4747
} else if spelling.contains('.') {
4848
out.kind = ValueKind::Float;
49-
} else if let Some(unsigned_value) = spelling.strip_suffix(&['U', 'u']) {
49+
} else if let Some(unsigned_value) = spelling.strip_suffix(['U', 'u']) {
5050
out.kind = ValueKind::UnsignedInteger;
5151
out.value += unsigned_value;
5252
continue;

binding-generator/src/element.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@ pub struct DefaultElement;
1515
impl DefaultElement {
1616
pub fn exclude_kind(this: &(impl Element + ?Sized)) -> ExcludeKind {
1717
let cpp_refname = this.cpp_name(CppNameStyle::Reference);
18-
ExcludeKind::Included
19-
.with_is_ignored(|| {
20-
!this.is_public()
21-
|| settings::ELEMENT_EXCLUDE_KIND
22-
.get(cpp_refname.as_ref())
23-
.map_or(false, |ek| ek.is_ignored())
24-
})
25-
.with_is_excluded(|| {
26-
(this.is_system() && !settings::IMPLEMENTED_SYSTEM_CLASSES.contains(cpp_refname.as_ref()))
27-
|| settings::ELEMENT_EXCLUDE_KIND
28-
.get(cpp_refname.as_ref())
29-
.map_or(false, |ek| ek.is_excluded())
30-
})
18+
ExcludeKind::Included.with_is_ignored(|| {
19+
!this.is_public()
20+
|| settings::ELEMENT_EXCLUDE_KIND
21+
.get(cpp_refname.as_ref())
22+
.map_or(false, |ek| ek.is_ignored())
23+
})
3124
}
3225

3326
pub fn is_system(entity: Entity) -> bool {

binding-generator/src/field.rs

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -192,42 +192,41 @@ impl<'tu, 'ge> Field<'tu, 'ge> {
192192
pub fn slice_arg_eligibility(&self) -> SliceArgEligibility {
193193
let type_ref = self.type_ref();
194194
let kind = type_ref.kind();
195-
kind
196-
.as_pointer()
197-
.filter(|inner| inner.kind().is_copy(inner.type_hint()))
198-
.or_else(|| kind.as_variable_array())
199-
.map_or_else(
200-
|| {
201-
// check if still can be a slice arg length
202-
let can_be_slice_arg_len = kind.as_primitive().map_or(false, |(_, cpp)| {
203-
if cpp == "int" || cpp == "size_t" {
204-
let name = self.cpp_name(CppNameStyle::Declaration);
205-
name.ends_with('s') && name.contains('n') && name != "thickness" // fixme: have to exclude thickness
206-
|| ["size", "len", "argc"].contains(&name.as_ref())
207-
|| name.contains("dims")
208-
|| name.ends_with("Size")
209-
} else {
210-
false
211-
}
212-
});
213-
if can_be_slice_arg_len {
214-
SliceArgEligibility::SliceArgLength
215-
} else {
216-
SliceArgEligibility::None
217-
}
218-
},
219-
|_| {
195+
let is_copy_ptr = || {
196+
kind
197+
.as_pointer()
198+
.filter(|inner| inner.kind().is_copy(inner.type_hint()))
199+
.is_some()
200+
};
201+
if kind.as_variable_array().is_some() || is_copy_ptr() {
202+
let name = self.cpp_name(CppNameStyle::Declaration);
203+
let name = name.as_ref();
204+
if ARGUMENT_NAMES_NOT_SLICE.contains(name) {
205+
SliceArgEligibility::None
206+
} else if ARGUMENT_NAMES_MULTIPLE_SLICE.contains(name) {
207+
SliceArgEligibility::SliceArgMultiple
208+
} else {
209+
SliceArgEligibility::SliceArgSingle
210+
}
211+
} else {
212+
// check if still can be a slice arg length
213+
let can_be_slice_arg_len = kind.as_primitive().map_or(false, |(_, cpp)| {
214+
if cpp == "int" || cpp == "size_t" {
220215
let name = self.cpp_name(CppNameStyle::Declaration);
221-
let name = name.as_ref();
222-
if ARGUMENT_NAMES_NOT_SLICE.contains(name) {
223-
SliceArgEligibility::None
224-
} else if ARGUMENT_NAMES_MULTIPLE_SLICE.contains(name) {
225-
SliceArgEligibility::SliceArgMultiple
226-
} else {
227-
SliceArgEligibility::SliceArgSingle
228-
}
229-
},
230-
)
216+
name.ends_with('s') && name.contains('n') && name != "thickness" // fixme: have to exclude thickness
217+
|| ["size", "len", "argc"].contains(&name.as_ref())
218+
|| name.contains("dims")
219+
|| name.ends_with("Size")
220+
} else {
221+
false
222+
}
223+
});
224+
if can_be_slice_arg_len {
225+
SliceArgEligibility::SliceArgLength
226+
} else {
227+
SliceArgEligibility::None
228+
}
229+
}
231230
}
232231
}
233232

binding-generator/src/func.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,9 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
566566
}
567567
let decl_name = fld.cpp_name(CppNameStyle::Declaration);
568568
name.reserve(decl_name.len() + 4);
569-
let (first_letter, rest) = decl_name.split_at(1);
570-
let [first_letter] = <[u8; 1]>::try_from(first_letter.as_bytes()).expect("first part of split_at(1)");
571569
name.push_str("prop");
572-
name.push(first_letter.to_ascii_uppercase().into());
570+
let (first_letter, rest) = decl_name.capitalize_first_ascii_letter().expect("Empty decl_name");
571+
name.push(first_letter);
573572
name.push_str(rest);
574573
name
575574
} else {

binding-generator/src/generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'tu, V: GeneratorVisitor<'tu>> EntityWalkerVisitor<'tu> for OpenCvWalker<'t
195195
self.visitor.visit_func(inject_func);
196196
}
197197
}
198-
for generated in &self.gen_env.settings.generator_module_tweaks.generate_types {
198+
for generated in self.gen_env.settings.generator_module_tweaks.generate_types {
199199
if let Ok(generated) = GeneratedType::try_from(generated()) {
200200
self.visitor.visit_generated_type(generated);
201201
}

binding-generator/src/settings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ mod implemented;
4040
#[derive(Debug)]
4141
pub struct Settings {
4242
pub func_inject: Vec<FuncFactory>,
43-
pub generator_module_tweaks: ModuleTweak,
43+
pub generator_module_tweaks: ModuleTweak<'static>,
4444
}
4545

4646
impl Settings {

binding-generator/src/settings/element_exclude_kind.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ pub static ELEMENT_EXCLUDE_KIND: Lazy<HashMap<&str, ExcludeKind>> = Lazy::new(||
1818
("FILE", ExcludeKind::Ignored),
1919
("HG_AUTOSIZE", ExcludeKind::Ignored), // 3.2
2020
("cv::ErrorCallback", ExcludeKind::Ignored),
21-
("cv::MatAllocator", ExcludeKind::Ignored), // doesn't handle cpp part too well
22-
("cv::NAryMatIterator", ExcludeKind::Ignored), // uses pointers of pointers
23-
("cv::Node", ExcludeKind::Ignored), // template class
24-
("cv::gapi::own::Mat", ExcludeKind::Ignored), // internal alias to Mat
21+
("cv::MatAllocator", ExcludeKind::Ignored), // doesn't handle cpp part too well
22+
("cv::MatExpr::op", ExcludeKind::Ignored), // fixme implement PointerOf types
23+
("cv::NAryMatIterator", ExcludeKind::Ignored), // uses pointers of pointers
24+
("cv::Node", ExcludeKind::Ignored), // template class
25+
("cv::gapi::own::Mat", ExcludeKind::Ignored), // internal alias to Mat
26+
("cv::text::ERStat::pixels", ExcludeKind::Ignored), // fixme: reference to a vector, we don't handle it too well yet
2527
("std::exception_ptr", ExcludeKind::Ignored),
2628
("std::random_access_iterator_tag", ExcludeKind::Ignored),
2729
])

binding-generator/src/settings/func_exclude.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub static FUNC_EXCLUDE: Lazy<HashSet<&str>> = Lazy::new(|| {
77
HashSet::from([
88
// ### core ###
99
"cv_AsyncArray__getImpl_const",
10-
"cv_MatExpr_propOp_const", // fixme implement PointerOf types
1110
"cv_Mat_Mat_const_MatR_const_RangeX", // duplicate of cv_Mat_Mat_Mat_VectorOfRange, but with pointers
1211
"cv_Mat_at_const_VecLint__cnGR", // fixme: due to FuncId only taking into account arg name we generate extra Mat::at with VecN args
1312
"cv_Mat_at_const_const_VecLint__cnGR", // fixme: due to FuncId only taking into account arg name we generate extra Mat::at with VecN args
@@ -75,8 +74,6 @@ pub static FUNC_EXCLUDE: Lazy<HashSet<&str>> = Lazy::new(|| {
7574
// ### optflow ###
7675
"cv_optflow_GPCTrainingSamples_operator_cv_optflow_GPCSamplesVector", // support of "operator &" missing
7776
// ### text ###
78-
"cv_text_ERStat_propPixels", // fixme: reference to a vector, we don't handle it too well yet
79-
"cv_text_ERStat_propPixels_vectorLintGX", // fixme: reference to a vector, we don't handle it too well yet
8077
"cv_text_OCRBeamSearchDecoder_create_const_PtrLClassifierCallbackG_const_stringR_const__InputArrayR_const__InputArrayR", // with OpenCV 4.2 this leads to https://github.com/twistedfall/opencv-rust/issues/505
8178
"cv_text_OCRBeamSearchDecoder_create_const_PtrLClassifierCallbackG_const_StringR_const__InputArrayR_const__InputArrayR", // with OpenCV 4.2 this leads to https://github.com/twistedfall/opencv-rust/issues/505
8279
// those function are marked as CV_EXPORTS, but they are missing from the shared libraries

0 commit comments

Comments
 (0)