Skip to content

Commit 2b3712c

Browse files
committed
Cleanup
1 parent 24b47a8 commit 2b3712c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+829
-782
lines changed

.github/workflows/opencv-rust.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
- windows-2022
6868
- macos-14
6969
vcpkg-version:
70-
- 2024.03.25
70+
- 2024.04.26
7171
runs-on: ${{ matrix.os-image }}
7272
env:
7373
VCPKG_VERSION: ${{ matrix.vcpkg-version }}

binding-generator/src/class.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
168168
let children = entity.get_children();
169169
if let [single] = children.as_slice() {
170170
if matches!(single.get_kind(), EntityKind::EnumDecl) {
171-
Some(Enum::new_ext(*single, self.cpp_name(CppNameStyle::Declaration).into_owned()))
171+
Some(Enum::new_ext(*single, self.cpp_name(CppNameStyle::Declaration).as_ref()))
172172
} else {
173173
None
174174
}
@@ -416,16 +416,16 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
416416
out.extend(fields.flat_map(|fld| {
417417
iter::from_fn({
418418
let doc_comment = Rc::from(fld.doc_comment());
419-
let mut fld_type_ref = fld.type_ref().into_owned();
419+
let mut fld_type_ref = fld.type_ref();
420420
let fld_type_kind = fld_type_ref.kind();
421421
if fld_type_kind
422422
.as_pointer()
423423
.map_or(false, |inner| inner.kind().as_primitive().is_some())
424424
&& !fld_type_kind.is_char_ptr_string(fld_type_ref.type_hint())
425425
{
426-
fld_type_ref.set_type_hint(TypeRefTypeHint::PrimitivePtrAsRaw);
426+
fld_type_ref.to_mut().set_type_hint(TypeRefTypeHint::PrimitivePtrAsRaw);
427427
} else if fld_type_kind.as_class().map_or(false, |cls| cls.kind().is_trait()) {
428-
fld_type_ref.set_type_hint(TypeRefTypeHint::TraitClassConcrete);
428+
fld_type_ref.to_mut().set_type_hint(TypeRefTypeHint::TraitClassConcrete);
429429
}
430430
let fld_type_kind = fld_type_ref.kind();
431431
let fld_type_ref_return_as_naked = fld_type_kind.return_as_naked(fld_type_ref.type_hint());
@@ -490,7 +490,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
490490
def_loc: fld.file_line_name().location,
491491
rust_generic_decls: Rc::new([]),
492492
arguments: Rc::new([]),
493-
return_type_ref: fld_type_ref.clone(),
493+
return_type_ref: fld_type_ref.as_ref().clone(),
494494
cpp_body: FuncCppBody::ManualCall("{{name}}".into()),
495495
rust_body: FuncRustBody::Auto,
496496
rust_extern_definition: FuncRustExtern::Auto,

binding-generator/src/field.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,10 @@ impl<'tu, 'ge> Field<'tu, 'ge> {
187187
.filter(|inner| inner.kind().is_copy(inner.type_hint()))
188188
.map_or(SliceArgEligibility::NotEligible, |_| {
189189
let name = self.cpp_name(CppNameStyle::Declaration);
190-
if ARGUMENT_NAMES_NOT_SLICE.contains(name.as_ref()) {
190+
let name = name.as_ref();
191+
if ARGUMENT_NAMES_NOT_SLICE.contains(name) {
191192
SliceArgEligibility::NotEligible
192-
} else if ARGUMENT_NAMES_MULTIPLE_SLICE.contains(name.as_ref()) {
193+
} else if ARGUMENT_NAMES_MULTIPLE_SLICE.contains(name) {
193194
SliceArgEligibility::EligibleWithMultiple
194195
} else {
195196
SliceArgEligibility::Eligible
@@ -198,17 +199,19 @@ impl<'tu, 'ge> Field<'tu, 'ge> {
198199
}
199200

200201
pub fn can_be_slice_arg_len(&self) -> bool {
201-
let name = self.cpp_name(CppNameStyle::Declaration);
202202
let type_ref = self.type_ref();
203-
type_ref
204-
.kind()
205-
.as_primitive()
206-
.map_or(false, |(_, cpp)| cpp == "int" || cpp == "size_t")
207-
&& (name.ends_with('s') && name.contains('n') && name != "thickness" // fixme: have to exclude thickness
208-
|| name.contains("dims")
209-
|| name == "size"
210-
|| name.ends_with("Size")
211-
|| name == "len")
203+
type_ref.kind().as_primitive().map_or(false, |(_, cpp)| {
204+
if cpp == "int" || cpp == "size_t" {
205+
let name = self.cpp_name(CppNameStyle::Declaration);
206+
name.ends_with('s') && name.contains('n') && name != "thickness" // fixme: have to exclude thickness
207+
|| name.contains("dims")
208+
|| name == "size"
209+
|| name.ends_with("Size")
210+
|| name == "len"
211+
} else {
212+
false
213+
}
214+
})
212215
}
213216
}
214217

binding-generator/src/func.rs

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::borrow::Cow::{Borrowed, Owned};
23
use std::collections::HashMap;
34
use std::fmt;
45
use std::rc::Rc;
@@ -22,8 +23,8 @@ use crate::type_ref::{Constness, CppNameStyle, TypeRefDesc, TypeRefTypeHint};
2223
use crate::writer::rust_native::element::RustElement;
2324
use crate::writer::rust_native::type_ref::TypeRefExt;
2425
use crate::{
25-
debug, settings, Class, DefaultElement, Element, EntityExt, Field, GeneratedType, GeneratorEnv, IteratorExt, NameDebug,
26-
NameStyle, StrExt, StringExt, TypeRef,
26+
debug, settings, Class, CowMapBorrowedExt, DefaultElement, Element, EntityExt, Field, GeneratedType, GeneratorEnv,
27+
IteratorExt, NameDebug, NameStyle, StrExt, StringExt, TypeRef,
2728
};
2829

2930
mod desc;
@@ -120,16 +121,18 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
120121
FuncKind::GenericInstanceMethod(cls) => FuncKind::InstanceMethod(cls),
121122
kind => kind,
122123
};
123-
let generic = spec
124-
.values()
125-
.map(|s| s().cpp_name(CppNameStyle::Reference).into_owned())
126-
.join(", ");
124+
let spec_values = spec.values();
125+
let mut generic = String::with_capacity(spec_values.len() * 16);
126+
for spec in spec_values {
127+
let spec = spec();
128+
generic.extend_sep(", ", &spec.cpp_name(CppNameStyle::Reference));
129+
}
127130
let mut desc = self.to_desc(InheritConfig::empty().kind().arguments().return_type_ref());
128131
let desc_mut = Rc::make_mut(&mut desc);
129132
desc_mut.kind = kind;
130133
desc_mut.type_hint = FuncTypeHint::Specialized;
131134
desc_mut.arguments = arguments;
132-
desc_mut.return_type_ref = specialized(&return_type_ref).unwrap_or(return_type_ref);
135+
desc_mut.return_type_ref = specialized(&return_type_ref).unwrap_or_else(|| return_type_ref.into_owned());
133136
desc_mut.cpp_body = FuncCppBody::ManualCall(format!("{{{{name}}}}<{generic}>({{{{args}}}})").into());
134137
Self::Desc(desc)
135138
}
@@ -160,7 +163,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
160163
let return_type_ref = if skip_config.return_type_ref {
161164
TypeRefDesc::void()
162165
} else {
163-
self.return_type_ref()
166+
self.return_type_ref().into_owned()
164167
};
165168
let def_loc = if skip_config.definition_location {
166169
DefinitionLocation::Generated
@@ -227,7 +230,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
227230
desc.arguments = ancestor.arguments().into();
228231
}
229232
if config.return_type_ref {
230-
desc.return_type_ref = ancestor.return_type_ref();
233+
desc.return_type_ref = ancestor.return_type_ref().into_owned();
231234
}
232235
if config.definition_location {
233236
desc.def_loc = ancestor.file_line_name().location;
@@ -261,7 +264,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
261264
match self {
262265
&Self::Clang { entity, gen_env, .. } => {
263266
const OPERATOR: &str = "operator";
264-
Cow::Owned(match entity.get_kind() {
267+
Owned(match entity.get_kind() {
265268
EntityKind::FunctionDecl => {
266269
if let Some(operator) = entity.cpp_name(CppNameStyle::Declaration).strip_prefix(OPERATOR) {
267270
let arg_count = entity.get_arguments().map_or(0, |v| v.len());
@@ -299,7 +302,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
299302
_ => unreachable!("Unknown function entity: {:#?}", entity),
300303
})
301304
}
302-
Self::Desc(desc) => Cow::Borrowed(&desc.kind),
305+
Self::Desc(desc) => Borrowed(&desc.kind),
303306
}
304307
}
305308

@@ -341,7 +344,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
341344
const OVERLOAD: &str = "@overload";
342345
if let Some(idx) = out.find(OVERLOAD) {
343346
let rep = if let Some(copy) = gen_env.get_func_comment(line, self.cpp_name(CppNameStyle::Reference).as_ref()) {
344-
Cow::Owned(format!("{copy}\n\n## Overloaded parameters\n"))
347+
Owned(format!("{copy}\n\n## Overloaded parameters\n"))
345348
} else {
346349
"This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.".into()
347350
};
@@ -422,7 +425,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
422425
}
423426
}
424427

425-
pub fn return_type_ref(&self) -> TypeRef<'tu, 'ge> {
428+
pub fn return_type_ref(&self) -> Cow<TypeRef<'tu, 'ge>> {
426429
match self {
427430
&Self::Clang { entity, gen_env, .. } => {
428431
let mut out = match self.kind().as_ref() {
@@ -461,9 +464,9 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
461464
} else if !out.kind().is_char_ptr_string(out.type_hint()) {
462465
out.set_type_hint(TypeRefTypeHint::PrimitivePtrAsRaw);
463466
}
464-
out
467+
Owned(out)
465468
}
466-
Self::Desc(desc) => desc.return_type_ref.clone(),
469+
Self::Desc(desc) => Borrowed(&desc.return_type_ref),
467470
}
468471
}
469472

@@ -497,10 +500,10 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
497500
pub fn arguments(&self) -> Cow<[Field<'tu, 'ge>]> {
498501
match self {
499502
&Self::Clang { entity, gen_env, .. } => {
500-
let mut slice_arg_finder = SliceArgFinder::new();
501503
let arg_overrides = settings::ARGUMENT_OVERRIDE.get(&self.func_id());
502-
let mut out = self
503-
.clang_arguments(entity)
504+
let arguments = self.clang_arguments(entity);
505+
let mut slice_arg_finder = SliceArgFinder::with_capacity(arguments.len());
506+
let mut out = arguments
504507
.into_iter()
505508
.enumerate()
506509
.map(|(idx, a)| {
@@ -527,7 +530,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
527530
} else {
528531
1
529532
};
530-
slice_len_arg.set_type_ref_type_hint(TypeRefTypeHint::LenForSlice(slice_arg_names, divisor));
533+
slice_len_arg.set_type_ref_type_hint(TypeRefTypeHint::LenForSlice(slice_arg_names.into(), divisor));
531534
}
532535
out.into()
533536
}
@@ -567,8 +570,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
567570
// this to all of the functions, but it's too much work to rename all those entries in FUNC_RENAME
568571
if self.is_specialized() {
569572
out.push('_');
570-
let ret = self.return_type_ref();
571-
out.push_str(&ret.cpp_name(CppNameStyle::Reference));
573+
out.push_str(&self.return_type_ref().cpp_name(CppNameStyle::Reference));
572574
}
573575
if self.constness().is_const() {
574576
out.push_str("_const");
@@ -673,16 +675,16 @@ impl Element for Func<'_, '_> {
673675
fn cpp_namespace(&self) -> Cow<str> {
674676
match self {
675677
&Self::Clang { entity, .. } => DefaultElement::cpp_namespace(entity).into(),
676-
Self::Desc(desc) => match self.kind().as_ref() {
678+
Self::Desc(desc) => self.kind().map_borrowed(|kind| match kind {
677679
FuncKind::Function | FuncKind::FunctionOperator(_) | FuncKind::GenericFunction => desc.cpp_name.namespace().into(),
678680
FuncKind::Constructor(cls)
679681
| FuncKind::InstanceMethod(cls)
680682
| FuncKind::StaticMethod(cls)
681683
| FuncKind::FieldAccessor(cls, _)
682684
| FuncKind::ConversionMethod(cls)
683685
| FuncKind::InstanceOperator(cls, _)
684-
| FuncKind::GenericInstanceMethod(cls) => cls.cpp_name(CppNameStyle::Reference).into_owned().into(),
685-
},
686+
| FuncKind::GenericInstanceMethod(cls) => cls.cpp_name(CppNameStyle::Reference),
687+
}),
686688
}
687689
}
688690

binding-generator/src/func/func_id.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ impl<'f> FuncId<'f> {
3939
}
4040

4141
pub fn from_entity(entity: Entity) -> FuncId<'static> {
42-
let name = entity.cpp_name(CppNameStyle::Reference).into_owned().into();
4342
let args = if let EntityKind::FunctionTemplate = entity.get_kind() {
4443
let mut args = Vec::with_capacity(8);
4544
entity.walk_children_while(|child| {
@@ -58,7 +57,7 @@ impl<'f> FuncId<'f> {
5857
.collect()
5958
};
6059
FuncId {
61-
name,
60+
name: entity.cpp_name(CppNameStyle::Reference).into_owned().into(),
6261
constness: Constness::from_is_const(entity.is_const_method()),
6362
args,
6463
}

binding-generator/src/func/slice_arg_finder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ pub struct SliceArgFinder {
66
}
77

88
impl SliceArgFinder {
9-
pub fn new() -> Self {
9+
pub fn with_capacity(capacity: usize) -> Self {
1010
Self {
11-
slice_args: vec![],
11+
slice_args: Vec::with_capacity(capacity),
1212
state: None,
1313
}
1414
}

binding-generator/src/function.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt;
44
use clang::{Entity, EntityKind, EntityVisitResult, Type};
55

66
use crate::type_ref::CppNameStyle;
7-
use crate::{Element, Field, GeneratorEnv, IteratorExt, TypeRef};
7+
use crate::{CowMapBorrowedExt, Element, Field, GeneratorEnv, IteratorExt, TypeRef};
88

99
#[derive(Clone)]
1010
pub struct Function<'tu, 'ge> {
@@ -58,8 +58,11 @@ impl Element for Function<'_, '_> {
5858
fn cpp_name(&self, _style: CppNameStyle) -> Cow<str> {
5959
let args = self
6060
.arguments()
61-
.into_iter()
62-
.map(|a| a.type_ref().cpp_name_ext(CppNameStyle::Reference, "", false).into_owned())
61+
.iter()
62+
.map(|a| {
63+
a.type_ref()
64+
.map_borrowed(|tref| tref.cpp_name_ext(CppNameStyle::Reference, "", false))
65+
})
6366
.join(", ");
6467
let ret = self.return_type();
6568
format!("{ret} (*)({args})", args = args, ret = ret.cpp_name(CppNameStyle::Reference)).into()

binding-generator/src/generator_env.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cmp::Reverse;
12
use std::collections::{HashMap, HashSet};
23
use std::convert::TryFrom;
34
use std::fmt;
@@ -115,7 +116,7 @@ impl<'tu> GeneratorEnvPopulator<'tu, '_> {
115116
let defs = self.gen_env.func_comments.entry(name).or_default();
116117
defs.push((line, raw_comment.into_owned()));
117118
// reverse sort due to how we're querying this; the amount of elements in this Vec doesn't go above 7
118-
defs.sort_unstable_by(|(left_line, _), (right_line, _)| right_line.cmp(left_line));
119+
defs.sort_unstable_by_key(|(line, _)| Reverse(*line));
119120
}
120121
}
121122

binding-generator/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub use iterator_ext::IteratorExt;
3737
use memoize::{MemoizeMap, MemoizeMapExt};
3838
use name_pool::NamePool;
3939
use smart_ptr::SmartPtr;
40-
pub use string_ext::{CompiledInterpolation, StrExt, StringExt};
40+
pub use string_ext::{CompiledInterpolation, CowMapBorrowedExt, StrExt, StringExt};
4141
use tuple::Tuple;
4242
#[allow(unused)]
4343
use type_ref::dbg_clang_type;

binding-generator/src/name_pool.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl NamePool {
2020
name.to_mut().bump_counter();
2121
out = MakeUniqueNameResult::Changed;
2222
}
23-
self.names.insert(name.clone().into_owned());
23+
self.names.insert(name.to_string());
2424
out
2525
}
2626

0 commit comments

Comments
 (0)