opencv_binding_generator/
vector.rs

1use std::borrow::Cow;
2use std::fmt;
3use std::rc::Rc;
4
5use clang::Type;
6pub use desc::VectorDesc;
7
8use crate::element::ExcludeKind;
9use crate::type_ref::{Constness, CppNameStyle, TemplateArg, TypeRefDesc, TypeRefKind};
10use crate::{DefaultElement, Element, GeneratedType, GeneratorEnv, StrExt, TypeRef};
11
12mod desc;
13
14#[derive(Clone)]
15pub enum Vector<'tu, 'ge> {
16	Clang {
17		type_ref: Type<'tu>,
18		gen_env: &'ge GeneratorEnv<'tu>,
19	},
20	Desc(Rc<VectorDesc<'tu, 'ge>>),
21}
22
23impl<'tu, 'ge> Vector<'tu, 'ge> {
24	pub fn new(type_ref: Type<'tu>, gen_env: &'ge GeneratorEnv<'tu>) -> Self {
25		Self::Clang { type_ref, gen_env }
26	}
27
28	pub fn new_desc(desc: VectorDesc<'tu, 'ge>) -> Self {
29		Self::Desc(Rc::new(desc))
30	}
31
32	pub fn type_ref(&self) -> TypeRef<'tu, 'ge> {
33		match self {
34			&Vector::Clang { type_ref, gen_env } => TypeRef::new(type_ref, gen_env),
35			Vector::Desc(desc) => TypeRef::new_desc(TypeRefDesc::new(
36				TypeRefKind::StdVector(Vector::Desc(Rc::clone(desc))),
37				Constness::Mut,
38			)),
39		}
40	}
41
42	pub fn element_type(&self) -> TypeRef<'tu, 'ge> {
43		match self {
44			Vector::Clang { .. } =>
45			{
46				#[allow(clippy::unnecessary_to_owned)]
47				self
48					.type_ref()
49					.template_specialization_args()
50					.into_owned()
51					.into_iter()
52					.find_map(TemplateArg::into_typename)
53					.expect("vector template argument list is empty")
54			}
55			Vector::Desc(desc) => desc.element_type.clone(),
56		}
57	}
58
59	pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
60		self.element_type().generated_types()
61	}
62}
63
64impl Element for Vector<'_, '_> {
65	fn exclude_kind(&self) -> ExcludeKind {
66		DefaultElement::exclude_kind(self).with_exclude_kind(|| self.element_type().exclude_kind())
67	}
68
69	fn is_system(&self) -> bool {
70		true
71	}
72
73	fn is_public(&self) -> bool {
74		true
75	}
76
77	fn doc_comment(&self) -> Cow<'_, str> {
78		"".into()
79	}
80
81	fn cpp_namespace(&self) -> Cow<'_, str> {
82		// force this to be std because on some systems the actual namespace for vector is something like "std::__1"
83		"std".into()
84	}
85
86	fn cpp_name(&self, style: CppNameStyle) -> Cow<'_, str> {
87		"std::vector".cpp_name_from_fullname(style).into()
88	}
89}
90
91impl PartialEq for Vector<'_, '_> {
92	fn eq(&self, other: &Self) -> bool {
93		self.element_type() == other.element_type()
94	}
95}
96
97impl fmt::Debug for Vector<'_, '_> {
98	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99		let mut debug_struct = f.debug_struct(match self {
100			Self::Clang { .. } => "Vector::Clang",
101			Self::Desc(_) => "Vector::Desc",
102		});
103		self
104			.update_debug_struct(&mut debug_struct)
105			.field("element_type", &self.element_type())
106			.finish()
107	}
108}