Skip to content

Commit acd1922

Browse files
committed
Improve typedef generation, preserve typedef names more
1 parent 83f5fa8 commit acd1922

File tree

10 files changed

+36
-46
lines changed

10 files changed

+36
-46
lines changed

binding-generator/src/string_ext.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl CompiledInterpolation<'_> {
369369
}
370370

371371
pub trait StrExt {
372-
fn cpp_name_to_rust_fn_case(&self) -> String;
372+
fn cpp_name_to_rust_fn_case(&self) -> Cow<str>;
373373
fn lines_with_nl(&self) -> LinesWithNl;
374374
fn detect_indent(&self) -> Indent;
375375
fn compile_interpolation(&self) -> CompiledInterpolation;
@@ -385,7 +385,7 @@ pub trait StrExt {
385385
}
386386

387387
impl StrExt for str {
388-
fn cpp_name_to_rust_fn_case(&self) -> String {
388+
fn cpp_name_to_rust_fn_case(&self) -> Cow<str> {
389389
let mut out = String::with_capacity(self.len() + 8);
390390
#[derive(Copy, Clone)]
391391
enum State {
@@ -394,26 +394,26 @@ impl StrExt for str {
394394
LastUppercase,
395395
}
396396
let mut state = State::StartOrLastUnderscore;
397-
let mut chars = self.chars().peekable();
398-
while let Some(cur_c) = chars.next() {
397+
let mut chars = self.as_bytes().iter().peekable();
398+
while let Some(&cur_c) = chars.next() {
399399
let (add_c, new_state) = match cur_c {
400400
_ if cur_c.is_ascii_uppercase() => {
401401
match state {
402402
State::StartOrLastUnderscore => {}
403403
State::LastLowercase => out.push('_'),
404404
State::LastUppercase => {
405405
// SVDValue => svd_value
406-
if chars.peek().map_or(false, |next_c| next_c.is_lowercase()) {
406+
if chars.peek().map_or(false, |next_c| next_c.is_ascii_lowercase()) {
407407
out.push('_');
408408
}
409409
}
410410
}
411411
(cur_c.to_ascii_lowercase(), State::LastUppercase)
412412
}
413-
'_' => (cur_c, State::StartOrLastUnderscore),
413+
b'_' => (b'_', State::StartOrLastUnderscore),
414414
_ => (cur_c, State::LastLowercase),
415415
};
416-
out.push(add_c);
416+
out.push(char::from(add_c));
417417
state = new_state;
418418
}
419419
out.replacen_in_place("pn_p", 1, "pnp");
@@ -427,7 +427,7 @@ impl StrExt for str {
427427
out.replacen_in_place("open_cl", 1, "opencl");
428428
out.replacen_in_place("open_vx", 1, "openvx");
429429
out.replacen_in_place("aruco_3detect", 1, "aruco3_detect");
430-
out
430+
out.into()
431431
}
432432

433433
fn lines_with_nl(&self) -> LinesWithNl {

binding-generator/src/type_ref.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,15 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
176176
pub fn source(&self) -> Self {
177177
match self.kind().as_ref() {
178178
TypeRefKind::Pointer(inner) | TypeRefKind::Reference(inner) | TypeRefKind::RValueReference(inner) => inner.source(),
179-
TypeRefKind::Typedef(tdef) => tdef.underlying_type_ref().source(),
179+
TypeRefKind::Typedef(tdef) => {
180+
let underlying_type = tdef.underlying_type_ref();
181+
match underlying_type.kind().as_ref() {
182+
TypeRefKind::Pointer(inner) | TypeRefKind::Reference(inner) | TypeRefKind::RValueReference(inner) => {
183+
inner.source()
184+
}
185+
_ => self.clone(),
186+
}
187+
}
180188
_ => self.clone(),
181189
}
182190
}

binding-generator/src/writer/rust_native/comment.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::func::FuncKind;
1010
use crate::type_ref::FishStyle;
1111
use crate::writer::rust_native::class::ClassExt;
1212
use crate::writer::rust_native::element::RustElement;
13-
use crate::{CppNameStyle, Element, Func, NameStyle, StrExt, StringExt};
13+
use crate::{CowMapBorrowedExt, CppNameStyle, Element, Func, NameStyle, StrExt, StringExt};
1414

1515
#[derive(Debug, Clone, PartialEq)]
1616
pub struct RenderComment {
@@ -92,7 +92,7 @@ impl RenderComment {
9292
let name = caps.get(1).map(|(s, e)| &comment[s..e]).expect("Impossible");
9393
let space = caps.get(2).map(|(s, e)| &comment[s..e]).expect("Impossible");
9494
let name = if name.contains(char::is_lowercase) {
95-
Cow::Owned(name.cpp_name_to_rust_fn_case())
95+
name.cpp_name_to_rust_fn_case()
9696
} else {
9797
Cow::Borrowed(name)
9898
};
@@ -253,12 +253,11 @@ pub fn render_cpp_default_args(args: &[Field]) -> String {
253253
out
254254
}
255255

256-
pub fn render_ref<'r>(referenced: &'r Func, force_cpp_name: Option<&str>) -> Cow<'r, str> {
256+
pub fn render_ref<'r>(referenced: &'r Func, force_cpp_name: Option<&'r str>) -> Cow<'r, str> {
257257
match referenced.kind().as_ref() {
258258
FuncKind::Function | FuncKind::GenericFunction => force_cpp_name
259259
.map_or_else(|| referenced.cpp_name(CppNameStyle::Declaration), Cow::Borrowed)
260-
.cpp_name_to_rust_fn_case()
261-
.into(),
260+
.map_borrowed(|s| s.cpp_name_to_rust_fn_case()),
262261
FuncKind::FunctionOperator(_) | FuncKind::Constructor(_) => referenced.rust_leafname(FishStyle::No),
263262
FuncKind::InstanceMethod(cls) | FuncKind::GenericInstanceMethod(cls) => format!(
264263
"{}::{}",

binding-generator/src/writer/rust_native/constant.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use once_cell::sync::Lazy;
77
use crate::constant::ValueKind;
88
use crate::debug::NameDebug;
99
use crate::type_ref::{FishStyle, NameStyle};
10-
use crate::{settings, CompiledInterpolation, Const, CppNameStyle, Element, EntityElement, StrExt};
10+
use crate::{settings, CompiledInterpolation, Const, EntityElement, StrExt};
1111

1212
use super::element::{DefaultRustNativeElement, RustElement};
1313
use super::RustNativeGeneratedElement;
@@ -26,10 +26,6 @@ impl RustElement for Const<'_> {
2626
out.into()
2727
}
2828

29-
fn rust_leafname(&self, _fish_style: FishStyle) -> Cow<str> {
30-
self.cpp_name(CppNameStyle::Declaration)
31-
}
32-
3329
fn rendered_doc_comment(&self, comment_marker: &str, opencv_version: &str) -> String {
3430
DefaultRustNativeElement::rendered_doc_comment(self.entity(), comment_marker, opencv_version)
3531
}

binding-generator/src/writer/rust_native/element.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use clang::{Entity, EntityKind};
55

66
use crate::type_ref::FishStyle;
77
use crate::{
8-
opencv_module_from_path, reserved_rename, settings, CppNameStyle, Element, GeneratedType, IteratorExt, NameStyle, StrExt,
9-
StringExt,
8+
opencv_module_from_path, reserved_rename, settings, CppNameStyle, Element, GeneratedType, IteratorExt, NameStyle, StringExt,
109
};
1110

1211
use super::comment::RenderComment;
@@ -35,7 +34,7 @@ impl DefaultRustNativeElement {
3534
}
3635

3736
pub fn rust_leafname(this: &(impl Element + ?Sized)) -> Cow<str> {
38-
reserved_rename(this.cpp_name(CppNameStyle::Declaration).cpp_name_to_rust_fn_case().into())
37+
reserved_rename(this.cpp_name(CppNameStyle::Declaration))
3938
}
4039

4140
pub fn rust_name(this: &(impl RustElement + ?Sized), entity: Entity, name_style: NameStyle) -> String {

binding-generator/src/writer/rust_native/enumeration.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use once_cell::sync::Lazy;
55

66
use crate::debug::NameDebug;
77
use crate::type_ref::{FishStyle, NameStyle};
8-
use crate::{CompiledInterpolation, CppNameStyle, Element, EntityElement, Enum, StrExt};
8+
use crate::{CompiledInterpolation, EntityElement, Enum, StrExt};
99

1010
use super::element::{DefaultRustNativeElement, RustElement};
1111
use super::RustNativeGeneratedElement;
@@ -19,10 +19,6 @@ impl RustElement for Enum<'_> {
1919
DefaultRustNativeElement::rust_name(self, self.entity(), style).into()
2020
}
2121

22-
fn rust_leafname(&self, _fish_style: FishStyle) -> Cow<str> {
23-
self.cpp_name(CppNameStyle::Declaration)
24-
}
25-
2622
fn rendered_doc_comment(&self, comment_marker: &str, opencv_version: &str) -> String {
2723
DefaultRustNativeElement::rendered_doc_comment(self.entity(), comment_marker, opencv_version)
2824
}

binding-generator/src/writer/rust_native/field.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::borrow::Cow;
33
use crate::field::Field;
44
use crate::type_ref::FishStyle;
55
use crate::writer::rust_native::element::DebugRust;
6-
use crate::{reserved_rename, NameStyle, StrExt};
6+
use crate::{reserved_rename, CowMapBorrowedExt, NameStyle, StrExt};
77

88
use super::element::{DefaultRustNativeElement, RustElement};
99

@@ -18,8 +18,8 @@ impl RustElement for Field<'_, '_> {
1818

1919
fn rust_leafname(&self, _fish_style: FishStyle) -> Cow<str> {
2020
match self {
21-
Self::Clang { .. } => DefaultRustNativeElement::rust_leafname(self),
22-
Self::Desc(desc) => reserved_rename(desc.cpp_fullname.localname().cpp_name_to_rust_fn_case().into()),
21+
Self::Clang { .. } => DefaultRustNativeElement::rust_leafname(self).map_borrowed(|s| s.cpp_name_to_rust_fn_case()),
22+
Self::Desc(desc) => reserved_rename(desc.cpp_fullname.localname().cpp_name_to_rust_fn_case()),
2323
}
2424
}
2525

binding-generator/src/writer/rust_native/func.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use crate::func::{FuncCppBody, FuncKind, FuncRustBody, FuncRustExtern, InheritCo
1111
use crate::name_pool::NamePool;
1212
use crate::settings::ARG_OVERRIDE_SELF;
1313
use crate::type_ref::{Constness, CppNameStyle, ExternDir, FishStyle, NameStyle, StrEnc, StrType, TypeRef, TypeRefTypeHint};
14-
use crate::{reserved_rename, settings, CompiledInterpolation, Element, Func, IteratorExt, NameDebug, StrExt, StringExt};
14+
use crate::{
15+
reserved_rename, settings, CompiledInterpolation, CowMapBorrowedExt, Element, Func, IteratorExt, NameDebug, StrExt, StringExt,
16+
};
1517

1618
use super::comment::{render_ref, RenderComment};
1719
use super::element::{DefaultRustNativeElement, RustElement};
@@ -173,12 +175,12 @@ impl RustElement for Func<'_, '_> {
173175
};
174176
if let Some(&name) = settings::FUNC_RENAME.get(self.identifier().as_str()) {
175177
if name.contains('+') {
176-
reserved_rename(name.replace('+', rust_name.as_ref()).cpp_name_to_rust_fn_case().into())
178+
reserved_rename(Owned::<str>(name.replace('+', rust_name.as_ref())).map_borrowed(|s| s.cpp_name_to_rust_fn_case()))
177179
} else {
178180
name.into()
179181
}
180182
} else {
181-
reserved_rename(rust_name.cpp_name_to_rust_fn_case().into())
183+
reserved_rename(Owned::<str>(rust_name.into_owned()).map_borrowed(|s| s.cpp_name_to_rust_fn_case()))
182184
}
183185
}
184186

binding-generator/src/writer/rust_native/type_ref.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ impl TypeRefExt for TypeRef<'_, '_> {
138138
fn rust_as_raw_name(&self, constness: Constness) -> String {
139139
match self.kind().as_ref() {
140140
TypeRefKind::Class(cls) => cls.rust_as_raw_name(constness),
141+
TypeRefKind::Typedef(inner) => inner.underlying_type_ref().rust_as_raw_name(constness),
141142
_ => format!(
142143
"as_raw{const_qual}_{rust_safe_id}",
143144
const_qual = constness.rust_function_name_qual(),

binding-generator/src/writer/rust_native/typedef.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::collections::HashMap;
44
use once_cell::sync::Lazy;
55

66
use crate::debug::NameDebug;
7-
use crate::type_ref::{FishStyle, NameStyle, TypeRefKind};
7+
use crate::type_ref::NameStyle;
88
use crate::writer::rust_native::type_ref::Lifetime;
9-
use crate::{CompiledInterpolation, CppNameStyle, DefaultElement, EntityElement, IteratorExt, StrExt, Typedef};
9+
use crate::{CompiledInterpolation, EntityElement, IteratorExt, StrExt, Typedef};
1010

1111
use super::element::{DefaultRustNativeElement, RustElement};
1212
use super::type_ref::TypeRefExt;
@@ -21,17 +21,6 @@ impl RustElement for Typedef<'_, '_> {
2121
DefaultRustNativeElement::rust_name(self, self.entity(), style).into()
2222
}
2323

24-
fn rust_leafname(&self, _fish_style: FishStyle) -> Cow<str> {
25-
match self.underlying_type_ref().source().kind().as_ref() {
26-
TypeRefKind::Class(..)
27-
| TypeRefKind::Function(..)
28-
| TypeRefKind::StdVector(..)
29-
| TypeRefKind::SmartPtr(..)
30-
| TypeRefKind::StdTuple(..) => DefaultElement::cpp_name(self, self.entity(), CppNameStyle::Declaration),
31-
_ => DefaultRustNativeElement::rust_leafname(self),
32-
}
33-
}
34-
3524
fn rendered_doc_comment(&self, comment_marker: &str, opencv_version: &str) -> String {
3625
DefaultRustNativeElement::rendered_doc_comment(self.entity(), comment_marker, opencv_version)
3726
}

0 commit comments

Comments
 (0)