Skip to content

Commit db40000

Browse files
committed
Stop relying on func_rename for manual functions
1 parent d725213 commit db40000

File tree

5 files changed

+119
-81
lines changed

5 files changed

+119
-81
lines changed

binding-generator/src/func.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
254254
}
255255
}
256256

257+
pub fn inheriting(mut self, ancestor: &Func<'tu, 'ge>, inherit_config: InheritConfig) -> Self {
258+
self.inherit(ancestor, inherit_config);
259+
self
260+
}
261+
257262
/// Returns true if function was specialized
258263
///
259264
/// It's used to add the return type to function identifier because otherwise it will generate identical function names when

binding-generator/src/func/desc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ impl<'tu, 'ge> FuncDesc<'tu, 'ge> {
5757
}
5858
}
5959

60+
#[inline]
61+
pub fn rust_custom_leafname(mut self, rust_custom_leafname: impl Into<Rc<str>>) -> Self {
62+
self.rust_custom_leafname = Some(rust_custom_leafname.into());
63+
self
64+
}
65+
6066
#[inline]
6167
pub fn doc_comment(mut self, doc_comment: impl Into<Rc<str>>) -> Self {
6268
self.doc_comment = doc_comment.into();

binding-generator/src/settings/func_inject.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -69,27 +69,30 @@ pub fn func_inject_factory(module: &str) -> FuncInject {
6969
))
7070
},
7171
|| {
72-
Func::new_desc(FuncDesc::new(
73-
Constructor(ClassDesc::cv_input_array()),
74-
Mut,
75-
Fallible,
76-
"_InputArray",
77-
"core",
78-
[
79-
Field::new_desc(FieldDesc::new(
80-
"vec",
81-
TypeRef::new_array(TypeRefDesc::uchar().with_inherent_constness(Const), None),
82-
)),
83-
Field::new_desc(FieldDesc::new(
84-
"n",
85-
// todo, MSRV: remove `as_slice()` when MSRV allows
86-
TypeRefDesc::int().with_type_hint(TypeRefTypeHint::LenForSlice(["vec".to_string()].as_slice().into(), 1)),
87-
)),
88-
],
89-
TypeRefDesc::cv_input_array()
90-
.with_inherent_constness(Const)
91-
.with_type_hint(TypeRefTypeHint::BoxedAsRef(Const, "vec", Lifetime::Elided)),
92-
))
72+
Func::new_desc(
73+
FuncDesc::new(
74+
Constructor(ClassDesc::cv_input_array()),
75+
Mut,
76+
Fallible,
77+
"_InputArray",
78+
"core",
79+
[
80+
Field::new_desc(FieldDesc::new(
81+
"vec",
82+
TypeRef::new_array(TypeRefDesc::uchar().with_inherent_constness(Const), None),
83+
)),
84+
Field::new_desc(FieldDesc::new(
85+
"n",
86+
// todo, MSRV: remove `as_slice()` when MSRV allows
87+
TypeRefDesc::int().with_type_hint(TypeRefTypeHint::LenForSlice(["vec".to_string()].as_slice().into(), 1)),
88+
)),
89+
],
90+
TypeRefDesc::cv_input_array()
91+
.with_inherent_constness(Const)
92+
.with_type_hint(TypeRefTypeHint::BoxedAsRef(Const, "vec", Lifetime::Elided)),
93+
)
94+
.rust_custom_leafname("from_byte_slice"),
95+
)
9396
},
9497
],
9598
_ => vec![],

binding-generator/src/settings/func_rename.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ pub static FUNC_RENAME: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
8686
("cv_Mat_Mat_int_int_int", "+_rows_cols"),
8787
("cv_Mat_Mat_int_int_int_const_ScalarR", "+_rows_cols_with_default"),
8888
("cv_Mat_Mat_int_int_int_voidX_size_t", "+_rows_cols_with_data_unsafe"),
89-
("cv_Mat_at_Point", "+_pt_mut"),
90-
("cv_Mat_at_const_Point", "+_pt"),
91-
("cv_Mat_at_const_const_intX", "+_nd"),
92-
("cv_Mat_at_const_intX", "+_nd_mut"),
93-
("cv_Mat_at_const_int_int", "+_2d"),
94-
("cv_Mat_at_const_int_int_int", "+_3d"),
95-
("cv_Mat_at_int", "+_mut"),
96-
("cv_Mat_at_int_int", "+_2d_mut"),
97-
("cv_Mat_at_int_int_int", "+_3d_mut"),
9889
("cv_Mat_colRange_const_int_int", "col_bounds"),
9990
("cv_Mat_copyTo_const_const__OutputArrayR_const__InputArrayR", "+_masked"),
10091
("cv_Mat_create_Size_int", "+_size"),
@@ -189,7 +180,6 @@ pub static FUNC_RENAME: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
189180
("cv__InputArray__InputArray_const_MatR", "from_mat"),
190181
("cv__InputArray__InputArray_const_UMatR", "from_umat"),
191182
("cv__InputArray__InputArray_const_doubleR", "from_f64"),
192-
("cv__InputArray__InputArray_const_unsigned_charX_int", "from_byte_slice"),
193183
("cv__InputArray__InputArray_const_vectorLGpuMatGR", "from_gpumat_vec"),
194184
("cv__InputArray__InputArray_const_vectorLMatGR", "from_mat_vec"),
195185
("cv__InputArray__InputArray_const_vectorLUMatGR", "from_umat_vec"),

binding-generator/src/settings/func_replace.rs

Lines changed: 84 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use crate::{Func, FuncId};
1111
pub type FuncInheritFactory = for<'tu, 'ge> fn(&Func<'tu, 'ge>) -> Func<'tu, 'ge>;
1212

1313
pub static FUNC_REPLACE: Lazy<HashMap<FuncId, FuncInheritFactory>> = Lazy::new(|| {
14-
const MAT_FORWARD: Cow<str> = Cow::Borrowed("core::mat_forward::{{name}}(self, {{forward_args}})");
1514
const MAT_FORWARD_INHERIT_CONFIG: InheritConfig = InheritConfig {
1615
kind: false,
1716
name: false,
@@ -21,56 +20,91 @@ pub static FUNC_REPLACE: Lazy<HashMap<FuncId, FuncInheritFactory>> = Lazy::new(|
2120
definition_location: true,
2221
};
2322

24-
let forward_const = (|f| {
25-
let mut out = Func::new_desc(
26-
FuncDesc::new(
27-
FuncKind::InstanceMethod(ClassDesc::cv_mat()),
28-
Constness::Const,
29-
ReturnKind::Fallible,
30-
"at",
31-
"core",
32-
[],
33-
TypeRef::new_pointer(TypeRef::new_generic("T").with_inherent_constness(Constness::Const)),
34-
)
35-
.cpp_body(FuncCppBody::Absent)
36-
.rust_body(FuncRustBody::ManualCallReturn(MAT_FORWARD))
37-
.rust_extern_definition(FuncRustExtern::Absent)
38-
.rust_generic_decls([("T".to_string(), "core::DataType".to_string())]),
39-
);
40-
out.inherit(f, MAT_FORWARD_INHERIT_CONFIG);
41-
out
42-
}) as FuncInheritFactory;
43-
44-
let forward_mut = (|f| {
45-
let mut out = Func::new_desc(
46-
FuncDesc::new(
47-
FuncKind::InstanceMethod(ClassDesc::cv_mat()),
48-
Constness::Mut,
49-
ReturnKind::Fallible,
50-
"at",
51-
"core",
52-
[],
53-
TypeRef::new_pointer(TypeRef::new_generic("T")),
54-
)
55-
.cpp_body(FuncCppBody::Absent)
56-
.rust_body(FuncRustBody::ManualCallReturn(MAT_FORWARD))
57-
.rust_extern_definition(FuncRustExtern::Absent)
58-
.rust_generic_decls([("T".to_string(), "core::DataType".to_string())]),
59-
);
60-
out.inherit(f, MAT_FORWARD_INHERIT_CONFIG);
61-
out
62-
}) as FuncInheritFactory;
23+
fn make_at_forward(constness: Constness) -> FuncDesc<'static, 'static> {
24+
FuncDesc::new(
25+
FuncKind::InstanceMethod(ClassDesc::cv_mat()),
26+
constness,
27+
ReturnKind::Fallible,
28+
"at",
29+
"core",
30+
[],
31+
TypeRef::new_pointer(TypeRef::new_generic("T").with_inherent_constness(constness)),
32+
)
33+
.cpp_body(FuncCppBody::Absent)
34+
.rust_body(FuncRustBody::ManualCallReturn(Cow::Borrowed(
35+
"core::mat_forward::{{name}}(self, {{forward_args}})",
36+
)))
37+
.rust_extern_definition(FuncRustExtern::Absent)
38+
.rust_generic_decls([("T".to_string(), "core::DataType".to_string())])
39+
}
6340

6441
HashMap::from([
65-
(FuncId::new_mut("cv::Mat::at", ["i0"]), forward_mut),
66-
(FuncId::new_const("cv::Mat::at", ["i0"]), forward_const),
67-
(FuncId::new_mut("cv::Mat::at", ["row", "col"]), forward_mut),
68-
(FuncId::new_const("cv::Mat::at", ["row", "col"]), forward_const),
69-
(FuncId::new_mut("cv::Mat::at", ["i0", "i1", "i2"]), forward_mut),
70-
(FuncId::new_const("cv::Mat::at", ["i0", "i1", "i2"]), forward_const),
71-
(FuncId::new_mut("cv::Mat::at", ["pt"]), forward_mut),
72-
(FuncId::new_const("cv::Mat::at", ["pt"]), forward_const),
73-
(FuncId::new_mut("cv::Mat::at", ["idx"]), forward_mut),
74-
(FuncId::new_const("cv::Mat::at", ["idx"]), forward_const),
42+
(
43+
FuncId::new_mut("cv::Mat::at", ["i0"]),
44+
(|f| {
45+
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_mut"))
46+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
47+
}) as FuncInheritFactory,
48+
),
49+
(
50+
FuncId::new_const("cv::Mat::at", ["i0"]),
51+
(|f| Func::new_desc(make_at_forward(Constness::Const)).inheriting(f, MAT_FORWARD_INHERIT_CONFIG)) as FuncInheritFactory,
52+
),
53+
(
54+
FuncId::new_mut("cv::Mat::at", ["row", "col"]),
55+
(|f| {
56+
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_2d_mut"))
57+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
58+
}) as FuncInheritFactory,
59+
),
60+
(
61+
FuncId::new_const("cv::Mat::at", ["row", "col"]),
62+
(|f| {
63+
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_2d"))
64+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
65+
}) as FuncInheritFactory,
66+
),
67+
(
68+
FuncId::new_mut("cv::Mat::at", ["i0", "i1", "i2"]),
69+
(|f| {
70+
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_3d_mut"))
71+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
72+
}) as FuncInheritFactory,
73+
),
74+
(
75+
FuncId::new_const("cv::Mat::at", ["i0", "i1", "i2"]),
76+
(|f| {
77+
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_3d"))
78+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
79+
}) as FuncInheritFactory,
80+
),
81+
(
82+
FuncId::new_mut("cv::Mat::at", ["pt"]),
83+
(|f| {
84+
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_pt_mut"))
85+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
86+
}) as FuncInheritFactory,
87+
),
88+
(
89+
FuncId::new_const("cv::Mat::at", ["pt"]),
90+
(|f| {
91+
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_pt"))
92+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
93+
}) as FuncInheritFactory,
94+
),
95+
(
96+
FuncId::new_mut("cv::Mat::at", ["idx"]),
97+
(|f| {
98+
Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_nd_mut"))
99+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
100+
}) as FuncInheritFactory,
101+
),
102+
(
103+
FuncId::new_const("cv::Mat::at", ["idx"]),
104+
(|f| {
105+
Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_nd"))
106+
.inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
107+
}) as FuncInheritFactory,
108+
),
75109
])
76110
});

0 commit comments

Comments
 (0)