opencv_binding_generator/settings/
func_replace.rs

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