opencv_binding_generator/settings/
func_replace.rs1use std::borrow::Cow;
2use std::collections::HashMap;
3use std::rc::Rc;
4
5use crate::class::ClassDesc;
6use crate::func::{FuncCppBody, FuncDesc, FuncKind, FuncMatcher, FuncRustBody, FuncRustExtern, InheritConfig, ReturnKind};
7use crate::type_ref::{Constness, TypeRef};
8use crate::{Func, SupportedModule};
9
10pub type FuncReplace = FuncMatcher<'static, FuncInheritFactory>;
11
12pub type FuncInheritFactory = for<'tu, 'ge> fn(&Func<'tu, 'ge>) -> Func<'tu, 'ge>;
13
14pub fn func_replace_factory(module: SupportedModule) -> FuncReplace {
15 match module {
16 SupportedModule::Core => core_factory(),
17 _ => FuncReplace::empty(),
18 }
19}
20
21fn core_factory() -> FuncReplace {
22 const MAT_FORWARD_INHERIT_CONFIG: InheritConfig = InheritConfig {
23 kind: false,
24 name: false,
25 doc_comment: true,
26 arguments: true,
27 return_type_ref: false,
28 definition_location: true,
29 };
30
31 fn make_at_forward(constness: Constness) -> FuncDesc<'static, 'static> {
32 FuncDesc::new(
33 FuncKind::InstanceMethod(ClassDesc::cv_mat()),
34 constness,
35 ReturnKind::Fallible,
36 "at",
37 SupportedModule::Core,
38 [],
39 TypeRef::new_pointer(TypeRef::new_generic("T").with_inherent_constness(constness)),
40 )
41 .cpp_body(FuncCppBody::Absent)
42 .rust_body(FuncRustBody::ManualCallReturn(Cow::Borrowed(
43 "core::mat_forward::{{name}}(self, {{forward_args}})",
44 )))
45 .rust_extern_definition(FuncRustExtern::Absent)
46 .rust_generic_decls([("T".to_string(), "core::DataType".to_string())])
47 }
48
49 FuncMatcher::create(HashMap::from([
50 (
51 "cv::Mat::at",
52 vec![
53 (
54 pred!(mut, ["i0"]),
55 (|f| {
56 Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_mut"))
57 .inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
58 }) as FuncInheritFactory,
59 ),
60 (
61 pred!(const, ["i0"]),
62 (|f| Func::new_desc(make_at_forward(Constness::Const)).inheriting(f, MAT_FORWARD_INHERIT_CONFIG))
63 as FuncInheritFactory,
64 ),
65 (
66 pred!(mut, ["row", "col"]),
67 (|f| {
68 Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_2d_mut"))
69 .inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
70 }) as FuncInheritFactory,
71 ),
72 (
73 pred!(const, ["row", "col"]),
74 (|f| {
75 Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_2d"))
76 .inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
77 }) as FuncInheritFactory,
78 ),
79 (
80 pred!(mut, ["i0", "i1", "i2"]),
81 (|f| {
82 Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_3d_mut"))
83 .inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
84 }) as FuncInheritFactory,
85 ),
86 (
87 pred!(const, ["i0", "i1", "i2"]),
88 (|f| {
89 Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_3d"))
90 .inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
91 }) as FuncInheritFactory,
92 ),
93 (
94 pred!(mut, ["pt"]),
95 (|f| {
96 Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_pt_mut"))
97 .inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
98 }) as FuncInheritFactory,
99 ),
100 (
101 pred!(const, ["pt"]),
102 (|f| {
103 Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_pt"))
104 .inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
105 }) as FuncInheritFactory,
106 ),
107 (
108 pred!(mut, ["idx"]),
109 (|f| {
110 Func::new_desc(make_at_forward(Constness::Mut).rust_custom_leafname("at_nd_mut"))
111 .inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
112 }) as FuncInheritFactory,
113 ),
114 (
115 pred!(const, ["idx"]),
116 (|f| {
117 Func::new_desc(make_at_forward(Constness::Const).rust_custom_leafname("at_nd"))
118 .inheriting(f, MAT_FORWARD_INHERIT_CONFIG)
119 }) as FuncInheritFactory,
120 ),
121 ],
122 ),
123 (
124 "cv::Mat::step",
125 vec![(
126 &[],
127 (|f| {
128 let replace_f =
129 Rc::unwrap_or_clone(f.to_desc_with_skip_config(InheritConfig::empty())).rust_body(FuncRustBody::Absent);
130 Func::new_desc(replace_f)
131 }) as FuncInheritFactory,
132 )],
133 ),
134 (
135 "cv::UMat::step",
136 vec![(
137 &[],
138 (|f| {
139 let replace_f =
140 Rc::unwrap_or_clone(f.to_desc_with_skip_config(InheritConfig::empty())).rust_body(FuncRustBody::Absent);
141 Func::new_desc(replace_f)
142 }) as FuncInheritFactory,
143 )],
144 ),
145 ]))
146}