Skip to main content

opencv_binding_generator/writer/rust_native/
abstract_ref_wrapper.rs

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