Skip to content

Commit fe1991d

Browse files
committed
Cleanup
1 parent 720cd33 commit fe1991d

25 files changed

+150
-128
lines changed

binding-generator/src/class.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
334334
pub fn methods(&self) -> Vec<Func<'tu, 'ge>> {
335335
let mut out = Vec::with_capacity(32);
336336
self.for_each_method(|func| {
337-
let func = if let Some(func_fact) = settings::FUNC_REPLACE.get(&func.func_id()) {
337+
let func: Func = if let Some(func_fact) = settings::FUNC_REPLACE.get(&func.func_id()) {
338338
func_fact(&func)
339339
} else {
340340
func

binding-generator/src/func.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,21 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
321321
}
322322

323323
pub fn is_generic(&self) -> bool {
324-
match self.kind().as_ref() {
325-
FuncKind::GenericFunction | FuncKind::GenericInstanceMethod(..) => true,
326-
FuncKind::Function
327-
| FuncKind::Constructor(..)
328-
| FuncKind::InstanceMethod(..)
329-
| FuncKind::StaticMethod(..)
330-
| FuncKind::FieldAccessor(..)
331-
| FuncKind::ConversionMethod(..)
332-
| FuncKind::FunctionOperator(..)
333-
| FuncKind::InstanceOperator(..) => false,
324+
match self {
325+
Func::Clang { entity, .. } => {
326+
matches!(entity.get_kind(), EntityKind::FunctionTemplate)
327+
}
328+
Func::Desc(desc) => match desc.kind {
329+
FuncKind::GenericFunction | FuncKind::GenericInstanceMethod(..) => true,
330+
FuncKind::Function
331+
| FuncKind::Constructor(..)
332+
| FuncKind::InstanceMethod(..)
333+
| FuncKind::StaticMethod(..)
334+
| FuncKind::FieldAccessor(..)
335+
| FuncKind::ConversionMethod(..)
336+
| FuncKind::FunctionOperator(..)
337+
| FuncKind::InstanceOperator(..) => false,
338+
},
334339
}
335340
}
336341

@@ -461,7 +466,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
461466
.iter()
462467
.find(|arg| arg.cpp_name(CppNameStyle::Declaration) == *borrow_arg_name)
463468
.map(|arg| arg.type_ref().constness())
464-
.unwrap_or_else(|| panic!("BoxedAsRef refers to the non-existent argument name: {}", borrow_arg_name))
469+
.unwrap_or_else(|| panic!("BoxedAsRef refers to the non-existent argument name: {borrow_arg_name}"))
465470
};
466471
out.set_inherent_constness(borrow_arg_constness);
467472
}
@@ -536,9 +541,9 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
536541
};
537542
slice_len_arg.set_type_ref_type_hint(TypeRefTypeHint::LenForSlice(slice_arg_names.into(), divisor));
538543
}
539-
out.into()
544+
Owned(out)
540545
}
541-
Self::Desc(desc) => desc.arguments.as_ref().into(),
546+
Self::Desc(desc) => Borrowed(desc.arguments.as_ref()),
542547
}
543548
}
544549

@@ -572,6 +577,7 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
572577
};
573578
// add return type to function id for cases when we specialize on the return type, in theory we should be able to apply
574579
// this to all of the functions, but it's too much work to rename all those entries in FUNC_RENAME
580+
// fixme: introduce FuncMatcher and use this logic for every function and not only specialized ones
575581
if self.is_specialized() {
576582
out.push('_');
577583
out.push_str(&self.return_type_ref().cpp_name(CppNameStyle::Reference));

binding-generator/src/func/kind.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ pub enum FuncKind<'tu, 'ge> {
129129
}
130130

131131
impl<'tu, 'ge> FuncKind<'tu, 'ge> {
132+
#[inline]
132133
pub fn as_instance_method(&self) -> Option<&Class<'tu, 'ge>> {
133134
match self {
134135
Self::InstanceMethod(out)
@@ -140,6 +141,7 @@ impl<'tu, 'ge> FuncKind<'tu, 'ge> {
140141
}
141142
}
142143

144+
#[inline]
143145
pub fn as_constructor(&self) -> Option<&Class<'tu, 'ge>> {
144146
if let Self::Constructor(out) = self {
145147
Some(out)
@@ -148,6 +150,7 @@ impl<'tu, 'ge> FuncKind<'tu, 'ge> {
148150
}
149151
}
150152

153+
#[inline]
151154
pub fn as_static_method(&self) -> Option<&Class<'tu, 'ge>> {
152155
if let Self::StaticMethod(out) = self {
153156
Some(out)
@@ -156,6 +159,7 @@ impl<'tu, 'ge> FuncKind<'tu, 'ge> {
156159
}
157160
}
158161

162+
#[inline]
159163
pub fn as_conversion_method(&self) -> Option<&Class<'tu, 'ge>> {
160164
if let Self::ConversionMethod(out) = self {
161165
Some(out)
@@ -164,6 +168,7 @@ impl<'tu, 'ge> FuncKind<'tu, 'ge> {
164168
}
165169
}
166170

171+
#[inline]
167172
/// Any function with a connection to a class: instance method, static method or a constructor
168173
pub fn as_class_method(&self) -> Option<&Class<'tu, 'ge>> {
169174
self
@@ -172,6 +177,7 @@ impl<'tu, 'ge> FuncKind<'tu, 'ge> {
172177
.or_else(|| self.as_static_method())
173178
}
174179

180+
#[inline]
175181
pub fn as_operator(&self) -> Option<(Option<&Class<'tu, 'ge>>, OperatorKind)> {
176182
match self {
177183
Self::FunctionOperator(kind) => Some((None, *kind)),
@@ -180,6 +186,7 @@ impl<'tu, 'ge> FuncKind<'tu, 'ge> {
180186
}
181187
}
182188

189+
#[inline]
183190
pub fn as_field_accessor(&self) -> Option<(&Class<'tu, 'ge>, &Field<'tu, 'ge>)> {
184191
if let FuncKind::FieldAccessor(cls, fld) = self {
185192
Some((cls, fld))

binding-generator/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
#![allow(clippy::nonminimal_bool)] // pattern `!type_ref.as_vector().is_some()` used for more clarity
1414

15-
extern crate core;
16-
1715
use std::borrow::Cow;
1816
use std::fs::File;
1917
use std::io::{BufRead, Read, Seek, SeekFrom};
@@ -33,10 +31,11 @@ pub use func::{Func, FuncId, FuncTypeHint};
3331
pub use generator::{GeneratedType, Generator, GeneratorVisitor, OpenCvWalker};
3432
pub use generator_env::{ClassKindOverride, ExportConfig, GeneratorEnv};
3533
pub use iterator_ext::IteratorExt;
34+
pub use map_borrowed::CowMapBorrowedExt;
3635
use memoize::{MemoizeMap, MemoizeMapExt};
3736
use name_pool::NamePool;
3837
use smart_ptr::SmartPtr;
39-
pub use string_ext::{CompiledInterpolation, CowMapBorrowedExt, StrExt, StringExt};
38+
pub use string_ext::{CompiledInterpolation, StrExt, StringExt};
4039
use tuple::Tuple;
4140
#[allow(unused)]
4241
use type_ref::dbg_clang_type;
@@ -62,6 +61,7 @@ mod function;
6261
mod generator;
6362
mod generator_env;
6463
mod iterator_ext;
64+
mod map_borrowed;
6565
mod memoize;
6666
mod name_pool;
6767
mod renderer;

binding-generator/src/map_borrowed.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use std::borrow::{Borrow, Cow};
2+
3+
pub trait CowMapBorrowedExt<'c, 'b, IN, OUT>
4+
where
5+
IN: ToOwned + ?Sized + 'b,
6+
OUT: ToOwned + ?Sized + 'b,
7+
{
8+
fn map_borrowed<F>(self, f: F) -> Cow<'c, OUT>
9+
where
10+
F: for<'f> FnOnce(&'f IN) -> Cow<'f, OUT>;
11+
}
12+
13+
impl<'c, 'b, IN, OUT> CowMapBorrowedExt<'c, 'b, IN, OUT> for Cow<'c, IN>
14+
where
15+
IN: ToOwned + ?Sized + 'b,
16+
OUT: ToOwned + ?Sized + 'b,
17+
{
18+
#[inline(always)]
19+
fn map_borrowed<F>(self, f: F) -> Cow<'c, OUT>
20+
where
21+
F: for<'f> FnOnce(&'f IN) -> Cow<'f, OUT>,
22+
{
23+
match self {
24+
Cow::Borrowed(v) => f(v),
25+
Cow::Owned(v) => Cow::Owned(f(v.borrow()).into_owned()),
26+
}
27+
}
28+
}

binding-generator/src/renderer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'a> TypeRefRenderer<'a> for CppExternReturnRenderer {
171171
let type_ref = if kind.as_string(type_ref.type_hint()).is_some() {
172172
Cow::Owned(TypeRef::new_pointer(TypeRefDesc::void()))
173173
} else if kind.extern_pass_kind().is_by_void_ptr() && !kind.as_abstract_class_ptr().is_some() {
174-
Cow::Owned(TypeRef::new_pointer(type_ref.source()))
174+
Cow::Owned(TypeRef::new_pointer(type_ref.source().into_owned()))
175175
} else {
176176
Cow::Borrowed(type_ref)
177177
};

binding-generator/src/string_ext.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::borrow::{Borrow, Cow};
1+
use std::borrow::Cow;
22
use std::collections::HashMap;
33
use std::iter;
44

@@ -537,30 +537,3 @@ impl StrExt for str {
537537
}
538538
}
539539
}
540-
541-
pub trait CowMapBorrowedExt<'b, IN, OUT>
542-
where
543-
IN: 'b + ToOwned + ?Sized,
544-
OUT: 'b + ToOwned + ?Sized,
545-
{
546-
fn map_borrowed<F>(self, f: F) -> Cow<'b, OUT>
547-
where
548-
F: for<'f> FnOnce(&'f IN) -> Cow<'f, OUT>;
549-
}
550-
551-
impl<'b, IN, OUT> CowMapBorrowedExt<'b, IN, OUT> for Cow<'b, IN>
552-
where
553-
IN: 'b + ToOwned + ?Sized,
554-
OUT: 'b + ToOwned + ?Sized,
555-
{
556-
#[inline(always)]
557-
fn map_borrowed<F>(self, f: F) -> Cow<'b, OUT>
558-
where
559-
F: for<'f> FnOnce(&'f IN) -> Cow<'f, OUT>,
560-
{
561-
match self {
562-
Cow::Borrowed(v) => f(v),
563-
Cow::Owned(v) => Cow::Owned(f(v.borrow()).into_owned()),
564-
}
565-
}
566-
}

binding-generator/src/type_ref.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,29 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
164164
}
165165

166166
/// TypeRef with all the typedef's traversed
167-
pub fn canonical(&self) -> Self {
167+
pub fn canonical(&self) -> Cow<Self> {
168168
match self.kind().as_ref() {
169-
TypeRefKind::Typedef(tdef) => tdef.underlying_type_ref().canonical(),
170-
_ => self.clone(),
169+
TypeRefKind::Typedef(tdef) => Owned(tdef.underlying_type_ref().canonical().into_owned()),
170+
_ => Borrowed(self),
171171
}
172172
}
173173

174174
/// Removes indirection by pointer and reference, this will also remove typedef if it references a pointer or reference
175-
pub fn source(&self) -> Self {
175+
pub fn source(&self) -> Cow<Self> {
176176
match self.kind().as_ref() {
177-
TypeRefKind::Pointer(inner) | TypeRefKind::Reference(inner) | TypeRefKind::RValueReference(inner) => inner.source(),
177+
TypeRefKind::Pointer(inner) | TypeRefKind::Reference(inner) | TypeRefKind::RValueReference(inner) => {
178+
Owned(inner.source().into_owned())
179+
}
178180
TypeRefKind::Typedef(tdef) => {
179181
let underlying_type = tdef.underlying_type_ref();
180182
match underlying_type.kind().as_ref() {
181183
TypeRefKind::Pointer(inner) | TypeRefKind::Reference(inner) | TypeRefKind::RValueReference(inner) => {
182-
inner.source()
184+
Owned(inner.source().into_owned())
183185
}
184-
_ => self.clone(),
186+
_ => Borrowed(self),
185187
}
186188
}
187-
_ => self.clone(),
189+
_ => Borrowed(self),
188190
}
189191
}
190192

binding-generator/src/type_ref/desc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ impl<'tu> ClangTypeExt<'tu> for Type<'tu> {
379379
match kind {
380380
TypeKind::Pointer => {
381381
let pointee = self.get_pointee_type().expect("No pointee type for pointer");
382-
let pointee_typeref = TypeRef::new_ext(pointee, type_hint.recurse(), parent_entity, gen_env);
382+
let pointee_typeref = TypeRef::new_ext(pointee, type_hint.recurse_inner(), parent_entity, gen_env);
383383
let pointee_kind = pointee_typeref.kind();
384384
if pointee_kind.is_function() {
385385
pointee_kind.into_owned()
@@ -392,7 +392,7 @@ impl<'tu> ClangTypeExt<'tu> for Type<'tu> {
392392

393393
TypeKind::LValueReference => TypeRefKind::Reference(TypeRef::new_ext(
394394
self.get_pointee_type().expect("No pointee type for reference"),
395-
type_hint.recurse(),
395+
type_hint.recurse_inner(),
396396
parent_entity,
397397
gen_env,
398398
)),

binding-generator/src/type_ref/kind.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ pub enum InputOutputArrayKind {
488488
}
489489

490490
#[cfg(test)]
491-
mod tests {
491+
mod test {
492492
use crate::type_ref::{Dir, StrEnc, StrType, TypeRef, TypeRefDesc, TypeRefTypeHint};
493493

494494
fn as_string(type_ref: TypeRef) -> Option<(Dir, StrType)> {

0 commit comments

Comments
 (0)