1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Write;

use once_cell::sync::Lazy;
use regex::Regex;

use crate::field::Field;
use crate::func::{cpp_disambiguate_names, FuncKind, OperatorKind, ReturnKind, Safety};
use crate::type_ref::{Constness, CppNameStyle, Dir, ExternDir, FishStyle, NameStyle, StrEnc, StrType, TypeRef};
use crate::writer::rust_native::disambiguate_single_name;
use crate::{
	reserved_rename, settings, CompiledInterpolation, Element, Func, GeneratorEnv, IteratorExt, NameDebug, StrExt, StringExt,
};

use super::comment;
use super::element::{DefaultRustNativeElement, RustElement};
use super::type_ref::TypeRefExt;
use super::{rust_disambiguate_names, RustNativeGeneratedElement};

impl RustElement for Func<'_, '_> {
	fn rust_module(&self) -> Cow<str> {
		match self {
			&Self::Clang { entity, .. } => DefaultRustNativeElement::rust_module(entity),
			Self::Desc(desc) => desc.rust_module.as_ref().into(),
		}
	}

	fn rust_name(&self, style: NameStyle) -> Cow<str> {
		match self {
			&Self::Clang { entity, .. } => DefaultRustNativeElement::rust_name(self, entity, style).into(),
			Self::Desc(_) => match style {
				NameStyle::Declaration => self.rust_leafname(FishStyle::No),
				NameStyle::Reference(fish_style) => format!(
					"{}::{}",
					DefaultRustNativeElement::rust_module_reference(self),
					self.rust_leafname(fish_style)
				)
				.into(),
			},
		}
	}

	fn rust_leafname(&self, _fish_style: FishStyle) -> Cow<str> {
		let cpp_name = match self {
			&Self::Clang { entity, gen_env, .. } => {
				if let Some(name) = gen_env.get_rename_config(entity).map(|c| &c.rename) {
					name.into()
				} else {
					self.cpp_name(CppNameStyle::Declaration)
				}
			}
			Self::Desc(_) => self.cpp_name(CppNameStyle::Declaration),
		};
		let kind = self.kind();
		let rust_name = if let Some(cls) = kind.as_constructor() {
			let args = self.arguments();
			#[allow(clippy::never_loop)] // fixme use named block when MSRV is 1.65
			'ctor_name: loop {
				if args.is_empty() {
					break 'ctor_name "default";
				} else if args.len() == 1 {
					let arg_typeref = args[0].type_ref();
					let class_arg = arg_typeref.as_reference().and_then(|typ| {
						if let Some(ptr) = typ.as_smart_ptr() {
							ptr.pointee()
						} else {
							typ
						}
						.as_class()
					});
					if let Some(other) = class_arg {
						if *cls == other {
							break 'ctor_name if arg_typeref.constness().is_const() {
								"copy"
							} else {
								"copy_mut"
							};
						}
					}
				}
				break 'ctor_name "new";
			}
			.into()
		} else if kind.as_conversion_method().is_some() {
			let mut name = self.return_type_ref().rust_name(NameStyle::decl()).into_owned();
			name.cleanup_name();
			format!("to_{name}").into()
		} else if let Some((cls, kind)) = kind.as_operator() {
			if cpp_name.starts_with("operator") {
				let name = match kind {
					OperatorKind::Unsupported => cpp_name.as_ref(),
					OperatorKind::Index => {
						if self.constness().is_const() {
							"get"
						} else {
							"get_mut"
						}
					}
					OperatorKind::Add => "add",
					OperatorKind::Sub => "sub",
					OperatorKind::Mul => "mul",
					OperatorKind::Div => "div",
					OperatorKind::Apply => "apply",
					OperatorKind::Deref => {
						if self.constness().is_const() {
							"try_deref"
						} else {
							"try_deref_mut"
						}
					}
					OperatorKind::Equals => "equals",
					OperatorKind::NotEquals => "not_equals",
					OperatorKind::GreaterThan => "greater_than",
					OperatorKind::GreaterThanOrEqual => "greater_than_or_equal",
					OperatorKind::LessThan => "less_than",
					OperatorKind::LessThanOrEqual => "less_than_or_equal",
					OperatorKind::Incr => "incr",
					OperatorKind::Decr => "decr",
					OperatorKind::And => "and",
					OperatorKind::Or => "or",
					OperatorKind::Xor => "xor",
					OperatorKind::BitwiseNot => "negate",
				};
				if kind.add_args_to_name() {
					let args = self.arguments();
					let args = args.as_ref();
					let is_single_arg_same_as_class = if let (Some(cls), [single_arg]) = (cls, args) {
						single_arg
							.type_ref()
							.source()
							.as_class()
							.map_or(false, |single_class| &single_class == cls)
					} else {
						false
					};
					if args.is_empty() || is_single_arg_same_as_class {
						name.into()
					} else {
						let args = args.iter().map(|arg| arg.type_ref().rust_simple_name()).join("_");
						format!("{name}_{args}").into()
					}
				} else {
					name.into()
				}
			} else {
				cpp_name
			}
		} else {
			cpp_name
		};
		if let Some(&name) = settings::FUNC_RENAME.get(self.identifier().as_str()) {
			if name.contains('+') {
				reserved_rename(name.replace('+', rust_name.as_ref()).to_snake_case().into())
			} else {
				name.into()
			}
		} else {
			reserved_rename(rust_name.to_snake_case().into())
		}
	}

	fn rendered_doc_comment_with_prefix(&self, prefix: &str, opencv_version: &str) -> String {
		let comment = match self {
			&Self::Clang { entity, gen_env, .. } => {
				let mut comment = entity.doc_comment().into_owned();
				let line = self.file_line_name().location.as_file().map_or(0, |(_, line)| line);
				const OVERLOAD: &str = "@overload";
				if let Some(idx) = comment.find(OVERLOAD) {
					let rep = if let Some(copy) = gen_env.get_func_comment(line, entity.cpp_name(CppNameStyle::Reference).as_ref()) {
						format!("{copy}\n\n## Overloaded parameters\n")
					} else {
						"This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.".to_string()
					};
					comment.replace_range(idx..idx + OVERLOAD.len(), &rep);
				}
				static COPY_BRIEF: Lazy<Regex> = Lazy::new(|| Regex::new(r#"@copybrief\s+(\w+)"#).unwrap());
				comment.replace_in_place_regex_cb(&COPY_BRIEF, |comment, caps| {
					let copy_name = caps.get(1).map(|(s, e)| &comment[s..e]).expect("Impossible");
					let mut copy_full_name = self.cpp_namespace().into_owned();
					copy_full_name += "::";
					copy_full_name += copy_name;
					if let Some(copy) = gen_env.get_func_comment(line, &copy_full_name) {
						Some(copy.into())
					} else {
						Some("".into())
					}
				});
				Cow::Owned(comment)
			}
			Self::Desc(desc) => desc.doc_comment.as_ref().into(),
		};
		comment::render_doc_comment_with_processor(&comment, prefix, opencv_version, |out| {
			let mut default_args_comment = String::with_capacity(1024);
			for arg in self.arguments().as_ref() {
				if let Some(def_val) = arg.default_value() {
					if default_args_comment.is_empty() {
						default_args_comment += "## C++ default parameters";
					}
					write!(
						&mut default_args_comment,
						"\n* {name}: {val}",
						name = arg.rust_leafname(FishStyle::No),
						val = def_val
					)
					.expect("write! to String shouldn't fail");
				}
			}
			if !default_args_comment.is_empty() {
				if !out.is_empty() {
					out.push_str("\n\n");
				}
				out.push_str(&default_args_comment);
			}
		})
	}
}

impl RustNativeGeneratedElement for Func<'_, '_> {
	fn element_safe_id(&self) -> String {
		format!("{}-{}", self.rust_module(), self.rust_name(NameStyle::decl()))
	}

	fn gen_rust(&self, _opencv_version: &str, _gen_env: &GeneratorEnv) -> String {
		static TPL: Lazy<CompiledInterpolation> = Lazy::new(|| include_str!("tpl/func/rust.tpl.rs").compile_interpolation());

		let name = if self.is_clone() {
			Cow::Borrowed("try_clone")
		} else if let Some(name_hint) = self.custom_rust_leafname() {
			name_hint.into()
		} else {
			self.rust_leafname(FishStyle::No)
		};

		let kind = self.kind();
		let constness = self.constness();
		let return_kind = self.return_kind();
		let return_type_ref = self.return_type_ref();
		let safety = self.safety();
		let identifier = self.identifier();

		let args: Vec<(String, Field)> = rust_disambiguate_names(self.arguments().into_owned()).collect::<Vec<_>>();
		let as_instance_method = kind.as_instance_method();
		let mut decl_args = Vec::with_capacity(args.len());
		let mut call_args = Vec::with_capacity(args.len());
		let mut forward_args = Vec::with_capacity(args.len());
		let mut pre_call_args = Vec::with_capacity(args.len());
		let mut post_call_args = Vec::with_capacity(args.len());
		if let Some(cls) = as_instance_method {
			let cls_type_ref = cls.type_ref();
			decl_args.push(cls_type_ref.rust_self_func_decl(constness));
			call_args.push(cls_type_ref.rust_self_func_call(constness));
		}
		let mut callback_arg_name: Option<String> = None;
		for (name, arg) in args {
			let arg_type_ref = arg.type_ref();
			let arg_as_slice_len = arg.as_slice_len();
			if arg.is_user_data() {
				pre_post_arg_handle(
					arg.type_ref().rust_userdata_pre_call(
						&name,
						callback_arg_name.as_deref().expect("Can't get name of the callback arg"),
					),
					&mut pre_call_args,
				);
			} else {
				if arg_type_ref.as_function().is_some() {
					callback_arg_name = Some(name.clone());
				}
				if !arg_as_slice_len.is_some() {
					decl_args.push(arg_type_ref.rust_arg_func_decl(&name));
				}
				pre_post_arg_handle(
					arg_type_ref.rust_arg_pre_call(&name, return_kind.is_infallible()),
					&mut pre_call_args,
				);
			}
			if let Some((slice_arg, len_div)) = arg_as_slice_len {
				let slice_call = if len_div > 1 {
					format!("({slice_arg}.len() / {len_div}) as _")
				} else {
					format!("{slice_arg}.len() as _")
				};
				call_args.push(slice_call);
			} else {
				call_args.push(arg_type_ref.rust_arg_func_call(&name));
			}
			forward_args.push(arg_type_ref.rust_arg_forward(&name));
			pre_post_arg_handle(
				arg_type_ref.rust_arg_post_call(&name, return_kind.is_infallible()),
				&mut post_call_args,
			);
		}
		if !return_kind.is_naked() {
			pre_call_args.push("return_send!(via ocvrs_return);".to_string());
			call_args.push("ocvrs_return.as_mut_ptr()".to_string());
		}

		let doc_comment = self.rendered_doc_comment_with_prefix("///", _opencv_version);
		let visibility = if let Some(cls) = as_instance_method {
			if cls.is_trait() {
				""
			} else {
				"pub "
			}
		} else {
			"pub "
		};
		let is_static_func = matches!(kind.as_ref(), FuncKind::Function | FuncKind::StaticMethod(_));
		let return_type_func_decl = if return_kind.is_infallible() {
			return_type_ref.rust_return(FishStyle::No, is_static_func)
		} else {
			format!(
				"Result<{}>",
				self.return_type_ref().rust_return(FishStyle::No, is_static_func)
			)
			.into()
		};
		let return_type_func_decl = if return_type_func_decl == "()" {
			Cow::Borrowed("")
		} else {
			format!(" -> {return_type_func_decl}").into()
		};
		if return_kind.is_infallible() {
			post_call_args.push("ret".to_string());
		} else {
			post_call_args.push("Ok(ret)".to_string());
		}
		let ret_receive = if return_kind.is_naked() {
			"let ret = "
		} else {
			""
		};
		let mut ret_convert = Vec::with_capacity(3);
		if !return_kind.is_naked() {
			let spec = if safety.is_safe() {
				"return_receive!(unsafe ocvrs_return => ret);"
			} else {
				"return_receive!(ocvrs_return => ret);"
			};
			ret_convert.push(Cow::Borrowed(spec));
		}
		if !return_kind.is_infallible() {
			ret_convert.push("let ret = ret.into_result()?;".into())
		}
		let ret_map = rust_return_map(&return_type_ref, "ret", safety, return_kind, is_static_func);
		if !ret_map.is_empty() {
			ret_convert.push(format!("let ret = {ret_map};").into());
		}
		let mut attributes = String::new();
		if let Some((rust_attr, _)) = settings::FUNC_CFG_ATTR.get(identifier.as_str()) {
			attributes = format!("#[cfg({rust_attr})]");
		}
		if self.is_no_discard() {
			attributes.push_str("#[must_use]");
		}

		let decl_args = decl_args.join(", ");
		let pre_call_args = pre_call_args.join("\n");
		let call_args = call_args.join(", ");
		let forward_args = forward_args.join(", ");
		let post_call_args = post_call_args.join("\n");
		let ret_convert = ret_convert.join("\n");
		let tpl = if let Some(tpl) = settings::FUNC_MANUAL.get(identifier.as_str()) {
			tpl
		} else {
			&TPL
		};
		tpl.interpolate(&HashMap::from([
			("doc_comment", doc_comment.as_str()),
			("debug", &self.get_debug()),
			("attributes", &attributes),
			("visibility", visibility),
			(
				"unsafety_decl",
				if safety.is_safe() {
					""
				} else {
					"unsafe "
				},
			),
			("name", name.as_ref()),
			("generic_decl", ""),
			("decl_args", &decl_args),
			("rv_rust_full", return_type_func_decl.as_ref()),
			("pre_call_args", &pre_call_args),
			(
				"unsafety_call",
				if safety.is_safe() {
					"unsafe "
				} else {
					""
				},
			),
			("identifier", identifier.as_ref()),
			("call_args", &call_args),
			("forward_args", &forward_args),
			("ret_receive", ret_receive),
			("ret_convert", ret_convert.as_ref()),
			("post_call_args", &post_call_args),
		]))
	}

	fn gen_rust_exports(&self, _gen_env: &GeneratorEnv) -> String {
		static TPL: Lazy<CompiledInterpolation> = Lazy::new(|| include_str!("tpl/func/rust_extern.tpl.rs").compile_interpolation());

		let identifier = self.identifier();

		if settings::FUNC_MANUAL.contains_key(identifier.as_str()) {
			return "".to_string();
		}

		let mut attributes = String::new();
		if let Some((rust_attr, _)) = settings::FUNC_CFG_ATTR.get(identifier.as_str()) {
			attributes = format!("#[cfg({rust_attr})]");
		}
		let mut args = vec![];
		if let Some(cls) = self.kind().as_instance_method() {
			args.push(cls.type_ref().rust_extern_self_func_decl(self.constness()));
		}
		for (name, arg) in rust_disambiguate_names(self.arguments().into_owned()) {
			args.push(arg.type_ref().rust_extern_arg_func_decl(&name))
		}

		let return_kind = self.return_kind();
		let naked_return = return_kind.is_naked();
		let is_infallible = return_kind.is_infallible();
		let return_type = self.return_type_ref();
		let return_wrapper_type = if is_infallible {
			return_type.rust_extern(ExternDir::FromCpp)
		} else {
			return_type.rust_extern_return_fallible()
		};
		if !naked_return {
			let ret_name = "ocvrs_return";
			args.push(format!("{ret_name}: *mut {return_wrapper_type}"));
		}
		let return_wrapper_type = if return_type.is_void() || !naked_return {
			"".to_string()
		} else {
			format!(" -> {return_wrapper_type}")
		};
		TPL.interpolate(&HashMap::from([
			("attributes", attributes),
			("debug", self.get_debug()),
			("identifier", identifier),
			("args", args.join(", ")),
			("return_type", return_wrapper_type),
		]))
	}

	fn gen_cpp(&self, _gen_env: &GeneratorEnv) -> String {
		static TPL: Lazy<CompiledInterpolation> = Lazy::new(|| include_str!("tpl/func/cpp.tpl.cpp").compile_interpolation());

		let identifier = self.identifier();

		if settings::FUNC_MANUAL.contains_key(identifier.as_str()) {
			return "".to_string();
		}

		let kind = self.kind();
		let constness = self.constness();
		let return_kind = self.return_kind();
		let return_type_ref = self.return_type_ref();

		// attributes
		let mut attributes_begin = String::new();
		let mut attributes_end = String::new();
		if let Some((_, cpp_attr)) = settings::FUNC_CFG_ATTR.get(identifier.as_str()) {
			attributes_begin = format!("#if {cpp_attr}");
			attributes_end = "#endif".to_string();
		}

		// arguments
		let args = cpp_disambiguate_names(self.arguments().into_owned()).collect::<Vec<_>>();
		let mut decl_args = Vec::with_capacity(args.len());
		let mut pre_call_args = Vec::with_capacity(args.len());
		let mut post_call_args = Vec::with_capacity(args.len());
		let mut cleanup_args = Vec::with_capacity(args.len());
		if let Some(cls) = kind.as_instance_method() {
			decl_args.push(cls.type_ref().cpp_self_func_decl(constness));
		}
		for (name, arg) in args {
			let arg_type_ref = arg.type_ref();
			decl_args.push(arg_type_ref.cpp_arg_func_decl(&name));
			pre_post_arg_handle(arg_type_ref.cpp_arg_pre_call(&name), &mut pre_call_args);
			pre_post_arg_handle(arg_type_ref.cpp_arg_post_call(&name), &mut post_call_args);
			pre_post_arg_handle(arg_type_ref.cpp_arg_cleanup(&name), &mut cleanup_args);
		}

		// return
		let ocv_ret_name = "ocvrs_return";
		let cpp_extern_return = return_type_ref.cpp_extern_return();
		let ret_full = if return_kind.is_infallible() {
			cpp_extern_return.clone()
		} else {
			return_type_ref.cpp_extern_return_fallible()
		};
		let mut_ret_wrapper_full = if return_kind.is_infallible() {
			return_type_ref
				.with_constness(Constness::Mut)
				.cpp_extern_return()
				.into_owned()
		} else {
			return_type_ref
				.with_constness(Constness::Mut)
				.cpp_extern_return_fallible()
				.into_owned()
		};
		if !return_kind.is_naked() {
			decl_args.push(format!("{mut_ret_wrapper_full}* {ocv_ret_name}"));
		}
		let return_spec = if return_kind.is_naked() {
			Cow::Borrowed(ret_full.as_ref())
		} else {
			"void".into()
		};
		let mut rets = disambiguate_single_name("ret");
		let ret_name = rets.next().expect("Endless iterator returned nothing");
		let (ret, ret_cast) = cpp_return_map(&return_type_ref, &ret_name, kind.as_constructor().is_some());
		let ret = if cleanup_args.is_empty() {
			ret
		} else {
			let ret_name = rets.next().expect("Endless iterator returned nothing");
			pre_post_arg_handle(format!("{cpp_extern_return} {ret_name} = {ret}"), &mut post_call_args);
			ret_name.into()
		};

		// exception handling
		let func_try = if return_kind.is_infallible() {
			""
		} else {
			"try {"
		};
		let catch = if return_kind.is_infallible() {
			"".into()
		} else {
			let typ = if mut_ret_wrapper_full.contains(',') {
				format!("OCVRS_TYPE({mut_ret_wrapper_full})")
			} else {
				mut_ret_wrapper_full
			};
			format!("}} OCVRS_CATCH({typ}, {ocv_ret_name});").into()
		};

		TPL.interpolate(&HashMap::from([
			("attributes_begin", attributes_begin.into()),
			("debug", self.get_debug().into()),
			("return_spec", return_spec),
			("identifier", identifier.into()),
			("decl_args", decl_args.join(", ").into()),
			("try", func_try.into()),
			("pre_call_args", pre_call_args.join("\n").into()),
			("call", self.cpp_call_invoke().into()),
			("post_call_args", post_call_args.join("\n").into()),
			("cleanup_args", cleanup_args.join("\n").into()),
			(
				"return",
				self.cpp_return(&ret, ret_cast.then(|| ret_full.as_ref().into()), ocv_ret_name),
			),
			("catch", catch),
			("attributes_end", attributes_end.into()),
		]))
	}
}

fn pre_post_arg_handle(mut arg: String, args: &mut Vec<String>) {
	if !arg.is_empty() {
		arg.push(';');
		args.push(arg);
	}
}

fn rust_return_map(
	return_type: &TypeRef,
	ret_name: &str,
	context_safety: Safety,
	return_kind: ReturnKind,
	is_static_func: bool,
) -> Cow<'static, str> {
	let unsafety_call = if context_safety.is_safe() {
		"unsafe "
	} else {
		""
	};
	if return_type.as_string().is_some() || return_type.extern_pass_kind().is_by_void_ptr() {
		format!(
			"{unsafety_call}{{ {typ}::opencv_from_extern({ret_name}) }}",
			unsafety_call = unsafety_call,
			typ = return_type.rust_return(FishStyle::Turbo, is_static_func),
			ret_name = ret_name,
		)
		.into()
	} else if return_type.as_pointer().map_or(false, |i| !i.is_void()) && !return_type.is_rust_by_ptr()
		|| return_type.as_fixed_array().is_some()
	{
		let ptr_call = if return_type.constness().is_const() {
			"as_ref"
		} else {
			"as_mut"
		};
		let error_handling = if return_kind.is_infallible() {
			".expect(\"Function returned null pointer\")"
		} else {
			".ok_or_else(|| Error::new(core::StsNullPtr, \"Function returned null pointer\"))?"
		};
		format!("{unsafety_call}{{ {ret_name}.{ptr_call}() }}{error_handling}").into()
	} else {
		"".into()
	}
}

pub fn cpp_return_map<'f>(return_type: &TypeRef, name: &'f str, is_constructor: bool) -> (Cow<'f, str>, bool) {
	if return_type.is_void() {
		("".into(), false)
	} else if let Some(Dir::In(string_type)) | Some(Dir::Out(string_type)) = return_type.as_string() {
		let str_mk = match string_type {
			StrType::StdString(StrEnc::Text) | StrType::CvString(StrEnc::Text) => {
				format!("ocvrs_create_string({name}.c_str())").into()
			}
			StrType::CharPtr => format!("ocvrs_create_string({name})").into(),
			StrType::StdString(StrEnc::Binary) => format!("ocvrs_create_byte_string({name}.data(), {name}.size())").into(),
			StrType::CvString(StrEnc::Binary) => format!("ocvrs_create_byte_string({name}.begin(), {name}.size())").into(),
		};
		(str_mk, false)
	} else if return_type.extern_pass_kind().is_by_void_ptr() && !is_constructor {
		let out = return_type.source().as_class().filter(|cls| cls.is_abstract()).map_or_else(
			|| {
				format!(
					"new {typ}({name})",
					typ = return_type.cpp_name(CppNameStyle::Reference),
					name = name
				)
				.into()
			},
			|_| name.into(),
		);
		(out, false)
	} else {
		(name.into(), return_type.as_fixed_array().is_some())
	}
}