Skip to content

Commit 2d1e79b

Browse files
committed
Extend property renaming to property tweaks
1 parent b1d30d2 commit 2d1e79b

File tree

5 files changed

+153
-85
lines changed

5 files changed

+153
-85
lines changed

binding-generator/src/class.rs

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::element::ExcludeKind;
1414
use crate::entity::{ControlFlowExt, ToEntity};
1515
use crate::field::FieldDesc;
1616
use crate::func::{FuncCppBody, FuncDesc, FuncKind, ReturnKind};
17+
use crate::settings::PropertyReadWrite;
1718
use crate::type_ref::{Constness, CppNameStyle, StrEnc, StrType, TypeRef, TypeRefDesc, TypeRefTypeHint};
1819
use crate::writer::rust_native::element::RustElement;
1920
use crate::{
@@ -438,71 +439,80 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
438439
let return_kind = ReturnKind::infallible(fld_type_kind.return_as_naked(fld_type_ref.type_hint()));
439440
let fld_const = fld.constness();
440441
let passed_by_ref = fld_type_kind.can_return_as_direct_reference();
441-
let rust_custom_leafname = gen_env.settings.property_rename.get(fld_refname.as_ref()).copied();
442+
let prop_tweak = gen_env.settings.property_tweaks.get(fld_refname.as_ref());
443+
let rust_custom_leafname = prop_tweak.and_then(|tweak| tweak.rename);
444+
let read_write = prop_tweak
445+
.and_then(|tweak| tweak.read_write)
446+
.unwrap_or(PropertyReadWrite::ReadWrite);
442447
let fld_declname = fld_refname.localname();
443-
let (mut read_const_yield, mut read_mut_yield) = if fld_const.is_mut() && passed_by_ref {
444-
let read_const_func = if constness_filter.map_or(true, |c| c.is_const()) {
445-
Some(Func::new_desc(
446-
FuncDesc::new(
447-
FuncKind::FieldAccessor(self.clone(), fld.clone()),
448-
Constness::Const,
449-
return_kind,
450-
fld_declname,
451-
Rc::clone(&rust_module),
452-
[],
453-
fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
454-
)
455-
.def_loc(def_loc.clone())
456-
.doc_comment(Rc::clone(&doc_comment))
457-
.cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
458-
.maybe_rust_custom_leafname(rust_custom_leafname),
459-
))
448+
let (mut read_const_yield, mut read_mut_yield) = if read_write.is_read() {
449+
if fld_const.is_mut() && passed_by_ref {
450+
let read_const_func = if constness_filter.map_or(true, |c| c.is_const()) {
451+
Some(Func::new_desc(
452+
FuncDesc::new(
453+
FuncKind::FieldAccessor(self.clone(), fld.clone()),
454+
Constness::Const,
455+
return_kind,
456+
fld_declname,
457+
Rc::clone(&rust_module),
458+
[],
459+
fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Const),
460+
)
461+
.def_loc(def_loc.clone())
462+
.doc_comment(Rc::clone(&doc_comment))
463+
.cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
464+
.maybe_rust_custom_leafname(rust_custom_leafname),
465+
))
466+
} else {
467+
None
468+
};
469+
let read_mut_func = if constness_filter.map_or(true, |c| c.is_mut()) {
470+
Some(Func::new_desc(
471+
FuncDesc::new(
472+
FuncKind::FieldAccessor(self.clone(), fld.clone()),
473+
Constness::Mut,
474+
return_kind,
475+
format!("{fld_declname}Mut"),
476+
Rc::clone(&rust_module),
477+
[],
478+
fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Mut),
479+
)
480+
.def_loc(def_loc.clone())
481+
.doc_comment(Rc::clone(&doc_comment))
482+
.cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
483+
.maybe_rust_custom_leafname(rust_custom_leafname.map(|name| format!("{name}_mut"))),
484+
))
485+
} else {
486+
None
487+
};
488+
(read_const_func, read_mut_func)
460489
} else {
461-
None
462-
};
463-
let read_mut_func = if constness_filter.map_or(true, |c| c.is_mut()) {
464-
Some(Func::new_desc(
465-
FuncDesc::new(
466-
FuncKind::FieldAccessor(self.clone(), fld.clone()),
467-
Constness::Mut,
468-
return_kind,
469-
format!("{fld_declname}Mut"),
470-
Rc::clone(&rust_module),
471-
[],
472-
fld_type_ref.as_ref().clone().with_inherent_constness(Constness::Mut),
473-
)
474-
.def_loc(def_loc.clone())
475-
.doc_comment(Rc::clone(&doc_comment))
476-
.cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
477-
.maybe_rust_custom_leafname(rust_custom_leafname.map(|name| format!("{name}_mut"))),
478-
))
479-
} else {
480-
None
481-
};
482-
(read_const_func, read_mut_func)
490+
let single_read_func = if constness_filter.map_or(true, |c| c == fld_const) {
491+
Some(Func::new_desc(
492+
FuncDesc::new(
493+
FuncKind::FieldAccessor(self.clone(), fld.clone()),
494+
fld_const,
495+
return_kind,
496+
fld_declname,
497+
Rc::clone(&rust_module),
498+
[],
499+
fld_type_ref.as_ref().clone(),
500+
)
501+
.def_loc(def_loc.clone())
502+
.doc_comment(Rc::clone(&doc_comment))
503+
.cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
504+
.maybe_rust_custom_leafname(rust_custom_leafname),
505+
))
506+
} else {
507+
None
508+
};
509+
(single_read_func, None)
510+
}
483511
} else {
484-
let single_read_func = if constness_filter.map_or(true, |c| c == fld_const) {
485-
Some(Func::new_desc(
486-
FuncDesc::new(
487-
FuncKind::FieldAccessor(self.clone(), fld.clone()),
488-
fld_const,
489-
return_kind,
490-
fld_declname,
491-
Rc::clone(&rust_module),
492-
[],
493-
fld_type_ref.as_ref().clone(),
494-
)
495-
.def_loc(def_loc.clone())
496-
.doc_comment(Rc::clone(&doc_comment))
497-
.cpp_body(FuncCppBody::ManualCall("{{name}}".into()))
498-
.maybe_rust_custom_leafname(rust_custom_leafname),
499-
))
500-
} else {
501-
None
502-
};
503-
(single_read_func, None)
512+
(None, None)
504513
};
505-
let mut write_yield = if constness_filter.map_or(true, |c| c.is_mut())
514+
let mut write_yield = if read_write.is_write()
515+
&& constness_filter.map_or(true, |c| c.is_mut())
506516
&& !fld_type_ref.constness().is_const()
507517
&& !fld_type_kind.as_fixed_array().is_some()
508518
{

binding-generator/src/settings.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub use implemented::{
6262
IMPLEMENTED_SYSTEM_CLASSES,
6363
};
6464
use once_cell::sync::Lazy;
65-
pub use property_rename::{property_rename_factory, PropertyRename};
65+
pub use property_tweaks::{property_tweaks_factory, PropertyReadWrite, PropertyTweak, PropertyTweaks};
6666

6767
use crate::func::FuncMatcher;
6868
use crate::type_ref::TypeRef;
@@ -81,7 +81,7 @@ mod func_specialize;
8181
mod func_unsafe;
8282
mod generator_module_tweaks;
8383
mod implemented;
84-
mod property_rename;
84+
mod property_tweaks;
8585

8686
pub type TypeRefFactory = fn() -> TypeRef<'static, 'static>;
8787

@@ -96,9 +96,9 @@ pub struct Settings {
9696
pub func_replace: FuncReplace,
9797
pub func_specialize: FuncSpecialize,
9898
pub func_unsafe: FuncUnsafe,
99-
pub generator_module_tweaks: ModuleTweak,
99+
pub generator_module_tweaks: ModuleTweak<'static>,
100100
pub property_override: PropertyOverride,
101-
pub property_rename: PropertyRename,
101+
pub property_tweaks: PropertyTweaks,
102102
}
103103

104104
impl Settings {
@@ -114,7 +114,7 @@ impl Settings {
114114
func_unsafe: FuncUnsafe::empty(),
115115
generator_module_tweaks: ModuleTweak::empty(),
116116
property_override: PropertyOverride::default(),
117-
property_rename: PropertyRename::default(),
117+
property_tweaks: PropertyTweaks::default(),
118118
}
119119
}
120120

@@ -130,7 +130,7 @@ impl Settings {
130130
func_unsafe: func_unsafe_factory(module),
131131
generator_module_tweaks: generator_module_tweaks_factory(module),
132132
property_override: property_override_factory(module),
133-
property_rename: property_rename_factory(module),
133+
property_tweaks: property_tweaks_factory(module),
134134
}
135135
}
136136
}

binding-generator/src/settings/func_exclude.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ pub static FUNC_EXCLUDE: Lazy<HashSet<&str>> = Lazy::new(|| {
1010
"cv_Mat_Mat_const_MatR_const_RangeX", // duplicate of cv_Mat_Mat_Mat_VectorOfRange, but with pointers
1111
"cv_Mat_copySize_const_MatR", // internal function
1212
"cv_Mat_operator___const_const_RangeX", // duplicate of cv_Mat_operator___const_const_vectorLRangeGR, but with pointers
13-
"cv_Mat_propStep_const_MatStep", // MatStep type prevents assignment
1413
"cv_Mat_push_back__const_voidX", // internal method
1514
"cv_Mat_operatorST_const_MatR", // unsafe with the safe version that moves
1615
"cv_UMat_UMat_const_UMatR_const_RangeX", // duplicate of cv_UMat_UMat_UMat_VectorOfRange, but with pointers
1716
"cv_UMat_copySize_const_UMatR", // internal function
1817
"cv_UMat_operator___const_const_RangeX", // duplicate of cv_UMat_operator___const_const_vectorLRangeGR, but with pointers
19-
"cv_UMat_propStep_const_MatStep", // MatStep type prevents assignment
2018
"cv_RNG_MT19937_operator__", // the same as calling to_u32() or next()
2119
"cv_addImpl_int_const_charX",
2220
"cv_calcCovarMatrix_const_MatX_int_MatR_MatR_int_int", // duplicate of cv_calcCovarMatrix_const__InputArrayR_const__OutputArrayR_const__InputOutputArrayR_int_int, but with pointers

binding-generator/src/settings/property_rename.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
use std::collections::HashMap;
2+
3+
pub type PropertyTweaks = HashMap<&'static str, PropertyTweak<'static>>;
4+
5+
#[derive(Debug)]
6+
pub struct PropertyTweak<'l> {
7+
pub rename: Option<&'l str>,
8+
pub read_write: Option<PropertyReadWrite>,
9+
}
10+
11+
/// Rename property, format: (cpp_refname -> rust_custom_leafname)
12+
pub fn property_tweaks_factory(module: &str) -> PropertyTweaks {
13+
match module {
14+
"core" => HashMap::from([
15+
(
16+
"cv::Mat::size",
17+
PropertyTweak {
18+
rename: Some("mat_size"),
19+
..PropertyTweak::none()
20+
},
21+
),
22+
(
23+
"cv::Mat::step",
24+
PropertyTweak {
25+
rename: Some("mat_step"),
26+
read_write: Some(PropertyReadWrite::ReadOnly), // MatStep type prevents assignment
27+
},
28+
),
29+
(
30+
"cv::UMat::size",
31+
PropertyTweak {
32+
rename: Some("mat_size"),
33+
..PropertyTweak::none()
34+
},
35+
),
36+
(
37+
"cv::UMat::step",
38+
PropertyTweak {
39+
rename: Some("mat_step"),
40+
read_write: Some(PropertyReadWrite::ReadOnly), // MatStep type prevents assignment
41+
},
42+
),
43+
]),
44+
_ => HashMap::new(),
45+
}
46+
}
47+
48+
impl PropertyTweak<'_> {
49+
pub fn none() -> PropertyTweak<'static> {
50+
PropertyTweak {
51+
rename: None,
52+
read_write: None,
53+
}
54+
}
55+
}
56+
57+
#[derive(Clone, Copy, Debug)]
58+
pub enum PropertyReadWrite {
59+
ReadOnly,
60+
ReadWrite,
61+
}
62+
63+
impl PropertyReadWrite {
64+
pub fn is_read(self) -> bool {
65+
match self {
66+
PropertyReadWrite::ReadOnly | PropertyReadWrite::ReadWrite => true,
67+
}
68+
}
69+
70+
pub fn is_write(self) -> bool {
71+
match self {
72+
PropertyReadWrite::ReadWrite => true,
73+
PropertyReadWrite::ReadOnly => false,
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)