opencv_binding_generator/writer/rust_native/
abstract_ref_wrapper.rs

1use std::collections::HashMap;
2use std::sync::LazyLock;
3
4use super::type_ref::TypeRefExt;
5use super::RustNativeGeneratedElement;
6use crate::type_ref::Constness;
7use crate::writer::rust_native::class::ClassExt;
8use crate::writer::rust_native::element::RustElement;
9use crate::{AbstractRefWrapper, CompiledInterpolation, NameStyle, StrExt};
10
11impl RustNativeGeneratedElement for AbstractRefWrapper<'_, '_> {
12	fn element_order(&self) -> u8 {
13		10
14	}
15
16	fn element_safe_id(&self) -> String {
17		let type_ref = self.type_ref();
18		format!("{}-{}", type_ref.rust_module().opencv_name(), type_ref.rust_safe_id(true))
19	}
20
21	fn gen_rust(&self, _opencv_version: &str) -> String {
22		static RUST: LazyLock<CompiledInterpolation> =
23			LazyLock::new(|| include_str!("tpl/abstract_ref_wrapper/rust.tpl.rs").compile_interpolation());
24
25		let type_ref = self.type_ref().source();
26		let type_ref_kind = type_ref.kind();
27		let cls = type_ref_kind.as_class().expect("Can only make an abstract ref to a class");
28		RUST.interpolate(&HashMap::from([
29			("rust_full", cls.rust_name(NameStyle::ref_())),
30			("rust_trait_const", cls.rust_trait_name(NameStyle::ref_(), Constness::Const)),
31			("rust_trait_mut", cls.rust_trait_name(NameStyle::ref_(), Constness::Mut)),
32			("rust_as_raw_const", cls.rust_as_raw_name(Constness::Const).into()),
33			("rust_as_raw_mut", cls.rust_as_raw_name(Constness::Mut).into()),
34		]))
35	}
36}