Rework dependent types
MODIFY
binding-generator/src/class.rs
binding-generator/src/class.rs
@@ -4,11 +4,10 @@ use std::{cmp, fmt, hash, iter};
use clang::Entity;
-use crate::return_type_wrapper::ReturnTypeWrapper;
use crate::type_ref::{Constness, CppNameStyle, FishStyle, NameStyle};
use crate::{
- settings, Const, DefaultElement, DefinitionLocation, DependentType, DependentTypeMode, Element, EntityElement, EntityExt,
- Field, Func, FunctionTypeHint, GeneratorEnv, StrExt, TypeRef,
+ settings, Const, DefaultElement, Element, EntityElement, EntityExt, Field, Func, FunctionTypeHint, GeneratedType,
+ GeneratorEnv, StrExt, TypeRef,
};
#[derive(Clone, Copy, Debug, PartialOrd, PartialEq, Eq)]
@@ -288,41 +287,20 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
}
}
- pub fn dependent_types(&self) -> Vec<DependentType<'tu, 'ge>> {
- let dep_types = self
+ pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
+ self
.fields()
.into_iter()
.filter(|f| !f.is_excluded())
- .flat_map(|f| {
- f.type_ref()
- .dependent_types(DependentTypeMode::ForReturn(DefinitionLocation::Module))
- })
+ .flat_map(|f| f.type_ref().generated_types())
.chain(
self
.methods(None)
.into_iter()
.filter(|m| !m.is_excluded())
- .flat_map(|m| m.dependent_types()),
- );
- if !self.is_simple() {
- dep_types
- .chain(
- self
- .descendants()
- .into_iter()
- .filter(|b| !b.is_excluded() && !b.is_simple() && !b.is_abstract())
- .map(|b| {
- DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
- b.type_ref(),
- DefinitionLocation::Type,
- self.gen_env,
- ))
- }),
+ .flat_map(|m| m.generated_types()),
)
.collect()
- } else {
- dep_types.collect()
- }
}
}

MODIFY
binding-generator/src/func.rs
binding-generator/src/func.rs
@@ -9,8 +9,8 @@ use regex::Regex;
use crate::type_ref::{Constness, CppNameStyle, FishStyle, NameStyle, TypeRefTypeHint};
use crate::{
- comment, reserved_rename, settings, Class, DefaultElement, DefinitionLocation, DependentType, DependentTypeMode, Element,
- EntityElement, EntityExt, Field, FieldTypeHint, GeneratorEnv, IteratorExt, StrExt, StringExt, TypeRef,
+ comment, reserved_rename, settings, Class, DefaultElement, Element, EntityElement, EntityExt, Field, FieldTypeHint,
+ GeneratedType, GeneratorEnv, IteratorExt, StrExt, StringExt, TypeRef,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -531,18 +531,14 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
.collect()
}
- pub fn dependent_types(&self) -> Vec<DependentType<'tu, 'ge>> {
+ pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
self
.arguments()
.into_iter()
.map(|a| a.type_ref())
.filter(|t| !t.is_ignored())
- .flat_map(|t| t.dependent_types(DependentTypeMode::None))
- .chain(
- self
- .return_type()
- .dependent_types(DependentTypeMode::ForReturn(DefinitionLocation::Module)),
- )
+ .flat_map(|t| t.generated_types())
+ .chain(self.return_type().generated_types())
.collect()
}

MODIFY
binding-generator/src/generator.rs
binding-generator/src/generator.rs
@@ -15,37 +15,18 @@ use once_cell::sync::Lazy;
use crate::type_ref::{CppNameStyle, FishStyle, Kind as TypeRefKind};
use crate::{
get_definition_text, line_reader, opencv_module_from_path, settings, AbstractRefWrapper, Class, CompiledInterpolation, Const,
- Element, EntityExt, EntityWalker, EntityWalkerVisitor, Enum, Func, FunctionTypeHint, GeneratorEnv, ReturnTypeWrapper,
- SmartPtr, StrExt, Tuple, Typedef, Vector,
+ Element, EntityExt, EntityWalker, EntityWalkerVisitor, Enum, Func, FunctionTypeHint, GeneratorEnv, SmartPtr, StrExt, Tuple,
+ Typedef, Vector,
};
#[derive(Debug)]
-pub enum DependentType<'tu, 'ge> {
- ReturnTypeWrapper(ReturnTypeWrapper<'tu, 'ge>),
+pub enum GeneratedType<'tu, 'ge> {
AbstractRefWrapper(AbstractRefWrapper<'tu, 'ge>),
Vector(Vector<'tu, 'ge>),
SmartPtr(SmartPtr<'tu, 'ge>),
Tuple(Tuple<'tu, 'ge>),
}
-impl<'tu, 'ge> DependentType<'tu, 'ge> {
- pub fn from_return_type_wrapper(s: ReturnTypeWrapper<'tu, 'ge>) -> Self {
- DependentType::ReturnTypeWrapper(s)
- }
-
- pub fn from_abstract_ref_wrapper(s: AbstractRefWrapper<'tu, 'ge>) -> Self {
- DependentType::AbstractRefWrapper(s)
- }
-
- pub fn from_vector(s: Vector<'tu, 'ge>) -> Self {
- DependentType::Vector(s)
- }
-
- pub fn from_smart_ptr(s: SmartPtr<'tu, 'ge>) -> Self {
- DependentType::SmartPtr(s)
- }
-}
-
#[allow(unused)]
pub trait GeneratorVisitor {
fn wants_file(&mut self, path: &Path) -> bool {
@@ -58,7 +39,7 @@ pub trait GeneratorVisitor {
fn visit_func(&mut self, func: Func) {}
fn visit_typedef(&mut self, typedef: Typedef) {}
fn visit_class(&mut self, class: Class) {}
- fn visit_dependent_type(&mut self, typ: DependentType) {}
+ fn visit_generated_type(&mut self, typ: GeneratedType) {}
fn visit_ephemeral_header(&mut self, contents: &str) {}
}
@@ -248,7 +229,7 @@ impl<'tu, V: GeneratorVisitor> EntityWalkerVisitor<'tu> for OpenCvWalker<'tu, '_
} else if name == "CV_NODISCARD_STD" || name == "CV_NODISCARD" {
self.gen_env.make_export_config(entity).no_discard = true;
} else if name == "OCVRS_ONLY_DEPENDENT_TYPES" {
- self.gen_env.make_export_config(entity).only_dependent_types = true;
+ self.gen_env.make_export_config(entity).only_generated_types = true;
}
}
}
@@ -298,8 +279,8 @@ impl<'tu, 'r, V: GeneratorVisitor> OpenCvWalker<'tu, 'r, V> {
if gen_env.get_export_config(class_decl).is_some() {
let cls = Class::new(class_decl, gen_env);
if !cls.is_excluded() {
- cls.dependent_types().into_iter().for_each(|dep| {
- visitor.visit_dependent_type(dep);
+ cls.generated_types().into_iter().for_each(|dep| {
+ visitor.visit_generated_type(dep);
});
class_decl.walk_enums_while(|enm| {
let enm = Enum::new(enm);
@@ -355,7 +336,7 @@ impl<'tu, 'r, V: GeneratorVisitor> OpenCvWalker<'tu, 'r, V> {
}
fn process_func(visitor: &mut V, gen_env: &mut GeneratorEnv<'tu>, func_decl: Entity<'tu>) {
- if let Some(only_dependent_types) = gen_env.get_export_config(func_decl).map(|e| e.only_dependent_types) {
+ if let Some(e) = gen_env.get_export_config(func_decl) {
let func = Func::new(func_decl, gen_env);
if !func.is_excluded() {
let specs = settings::FUNC_SPECIALIZE.get(func.identifier().as_ref()).map_or_else(
@@ -364,7 +345,7 @@ impl<'tu, 'r, V: GeneratorVisitor> OpenCvWalker<'tu, 'r, V> {
);
for type_hint in specs {
let mut name_hint = None;
- if !only_dependent_types {
+ if !e.only_generated_types {
let mut name = Func::new_ext(func_decl, type_hint, None, gen_env)
.rust_leafname(FishStyle::No)
.into_owned()
@@ -375,10 +356,10 @@ impl<'tu, 'r, V: GeneratorVisitor> OpenCvWalker<'tu, 'r, V> {
}
}
let func = Func::new_ext(func_decl, type_hint, name_hint, gen_env);
- func.dependent_types().into_iter().for_each(|dep| {
- visitor.visit_dependent_type(dep);
+ func.generated_types().into_iter().for_each(|dep| {
+ visitor.visit_generated_type(dep);
});
- if !only_dependent_types {
+ if !e.only_generated_types {
visitor.visit_func(func);
}
}
@@ -390,38 +371,36 @@ impl<'tu, 'r, V: GeneratorVisitor> OpenCvWalker<'tu, 'r, V> {
let typedef = Typedef::new(typedef_decl, gen_env);
let type_ref = typedef.type_ref();
match type_ref.kind() {
- TypeRefKind::Class(..) => {
- return Self::process_class(visitor, gen_env, typedef_decl);
- }
- TypeRefKind::Enum(..) => {
- return Self::process_enum(visitor, typedef_decl);
- }
- _ => {}
- }
- let mut export = gen_env.get_export_config(typedef_decl).is_some()
+ TypeRefKind::Class(..) => Self::process_class(visitor, gen_env, typedef_decl),
+ TypeRefKind::Enum(..) => Self::process_enum(visitor, typedef_decl),
+ _ => {
+ if !typedef.is_excluded() {
+ let export = gen_env.get_export_config(typedef_decl).is_some()
// we need to have a typedef even if it's not exported for e.g. cv::Size
- || type_ref.is_data_type();
- export = export || {
+ || type_ref.is_data_type()
+ || {
let underlying_type = typedef.underlying_type_ref();
underlying_type.as_function().is_some()
|| !underlying_type.is_excluded()
- || if let Some(templ) = underlying_type.as_template() {
+ || underlying_type.as_template().map_or(false, |templ| {
let cpp_refname = templ.cpp_name(CppNameStyle::Reference);
settings::IMPLEMENTED_GENERICS.contains(cpp_refname.as_ref())
|| settings::IMPLEMENTED_CONST_GENERICS.contains(cpp_refname.as_ref())
- } else {
- false
- }
+ })
};
- if export && !typedef.is_excluded() {
+
+ if export {
typedef
- .dependent_types()
+ .generated_types()
.into_iter()
- .for_each(|dep| visitor.visit_dependent_type(dep));
+ .for_each(|dep| visitor.visit_generated_type(dep));
visitor.visit_typedef(typedef)
}
}
}
+ }
+ }
+}
impl<V: GeneratorVisitor> Drop for OpenCvWalker<'_, '_, V> {
fn drop(&mut self) {

MODIFY
binding-generator/src/generator_env.rs
binding-generator/src/generator_env.rs
@@ -20,7 +20,8 @@ pub struct ExportConfig {
pub no_return: bool,
pub no_except: bool,
pub no_discard: bool,
- pub only_dependent_types: bool,
+ // the function is used only to generate helper types using OCVRS_ONLY_DEPENDENT_TYPES
+ pub only_generated_types: bool,
}
pub struct RenameConfig {

MODIFY
binding-generator/src/lib.rs
binding-generator/src/lib.rs
@@ -36,18 +36,17 @@ pub use func::{Func, FuncId, FunctionTypeHint};
use function::Function;
#[allow(unused)]
use generator::{dbg_clang_entity, dbg_clang_type};
-pub use generator::{is_ephemeral_header, DependentType, Generator, GeneratorVisitor};
+pub use generator::{is_ephemeral_header, GeneratedType, Generator, GeneratorVisitor};
pub use generator_env::{ExportConfig, GeneratorEnv};
pub use iterator_ext::IteratorExt;
#[allow(unused)]
use memoize::{memo, memo_map, Memoize, MemoizeMap};
use name_pool::NamePool;
-use return_type_wrapper::{DefinitionLocation, ReturnTypeWrapper};
use smart_ptr::SmartPtr;
pub use string_ext::{CompiledInterpolation, StrExt, StringExt};
use tuple::Tuple;
+use type_ref::TypeRef;
pub use type_ref::{CppNameStyle, NameStyle};
-use type_ref::{DependentTypeMode, TypeRef};
pub use typedef::Typedef;
use vector::Vector;
pub use walker::{EntityWalker, EntityWalkerVisitor};
@@ -67,7 +66,6 @@ mod generator_env;
mod iterator_ext;
mod memoize;
mod name_pool;
-mod return_type_wrapper;
pub mod settings;
mod smart_ptr;
mod string_ext;

DELETE
binding-generator/src/return_type_wrapper.rs
-use std::borrow::Cow;
-
-use crate::type_ref::ConstnessOverride;
-use crate::{GeneratorEnv, TypeRef};
-
-#[derive(Clone, Debug, PartialEq, Eq)]
-pub enum DefinitionLocation {
- Module,
- Type,
- Custom(String),
-}
-
-#[derive(Debug)]
-pub struct ReturnTypeWrapper<'tu, 'ge> {
- type_ref: TypeRef<'tu, 'ge>,
- const_hint: ConstnessOverride,
- definition_location: DefinitionLocation,
- gen_env: &'ge GeneratorEnv<'tu>,
-}
-
-impl<'tu, 'ge> ReturnTypeWrapper<'tu, 'ge> {
- pub fn new(type_ref: TypeRef<'tu, 'ge>, definition_location: DefinitionLocation, gen_env: &'ge GeneratorEnv<'tu>) -> Self {
- Self::new_ext(type_ref, ConstnessOverride::No, definition_location, gen_env)
- }
-
- pub fn new_ext(
- type_ref: TypeRef<'tu, 'ge>,
- const_hint: ConstnessOverride,
- definition_location: DefinitionLocation,
- gen_env: &'ge GeneratorEnv<'tu>,
- ) -> Self {
- Self {
- type_ref,
- const_hint,
- definition_location,
- gen_env,
- }
- }
-
- pub fn type_ref(&self) -> &TypeRef<'tu, 'ge> {
- &self.type_ref
- }
-
- pub fn const_hint(&self) -> ConstnessOverride {
- self.const_hint
- }
-
- pub fn definition_location(&self) -> Cow<str> {
- match &self.definition_location {
- DefinitionLocation::Module => self.gen_env.module().into(),
- DefinitionLocation::Type => self.type_ref.rust_module(),
- DefinitionLocation::Custom(module) => module.into(),
- }
- }
-}

MODIFY
binding-generator/src/smart_ptr.rs
binding-generator/src/smart_ptr.rs
@@ -3,8 +3,8 @@ use std::fmt;
use clang::Entity;
-use crate::type_ref::{CppNameStyle, DependentTypeMode, FishStyle, NameStyle, TemplateArg};
-use crate::{DefaultElement, DependentType, Element, EntityElement, GeneratorEnv, TypeRef};
+use crate::type_ref::{CppNameStyle, FishStyle, NameStyle, TemplateArg};
+use crate::{DefaultElement, Element, EntityElement, GeneratedType, GeneratorEnv, TypeRef};
#[derive(Clone)]
pub struct SmartPtr<'tu, 'ge> {
@@ -30,9 +30,9 @@ impl<'tu, 'ge> SmartPtr<'tu, 'ge> {
.expect("Smart pointer template argument list is empty")
}
- pub fn dependent_types(&self) -> Vec<DependentType<'tu, 'ge>> {
+ pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
if self.pointee().as_typedef().is_some() {
- self.type_ref().canonical_clang().dependent_types(DependentTypeMode::None)
+ self.type_ref().canonical_clang().generated_types()
} else {
vec![]
}

MODIFY
binding-generator/src/type_ref.rs
binding-generator/src/type_ref.rs
@@ -10,8 +10,8 @@ use regex::{Captures, Regex};
pub use crate::writer::rust_native::renderer::{CppExternReturnRenderer, CppRenderer, RustRenderer};
use crate::{
settings::{self, ArgOverride},
- AbstractRefWrapper, Class, DefinitionLocation, DependentType, Element, EntityExt, Enum, Function, GeneratorEnv,
- ReturnTypeWrapper, SmartPtr, StringExt, Tuple, Typedef, Vector,
+ AbstractRefWrapper, Class, Element, EntityExt, Enum, Function, GeneratedType, GeneratorEnv, SmartPtr, StringExt, Tuple,
+ Typedef, Vector,
};
pub trait TypeRefRenderer<'a> {
@@ -21,12 +21,6 @@ pub trait TypeRefRenderer<'a> {
fn recurse(&self) -> Self::Recursed;
}
-#[derive(Clone, Debug, PartialEq, Eq)]
-pub enum DependentTypeMode {
- None,
- ForReturn(DefinitionLocation),
-}
-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StrType {
StdString(StrEnc),
@@ -411,14 +405,6 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
Constness::from_is_const(self.type_ref.is_const_qualified())
}
- fn get_const_hint(&self, type_ref: &TypeRef) -> ConstnessOverride {
- let constness = self.constness();
- match constness {
- Constness::Const if type_ref.clang_constness().is_mut() => ConstnessOverride::Const,
- _ => ConstnessOverride::No,
- }
- }
-
pub fn is_primitive(&self) -> bool {
matches!(self.canonical().kind(), Kind::Primitive(..))
}
@@ -932,12 +918,15 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
}
}
- pub fn dependent_types(&self, mode: DependentTypeMode) -> Vec<DependentType<'tu, 'ge>> {
+ pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
match self.source().kind() {
Kind::StdVector(vec) => {
- let mut out = vec.dependent_types();
+ let mut out = vec.generated_types();
out.reserve(2);
if let Some(Dir::In(str_type)) = vec.element_type().as_string() {
+ // implement workaround for race when type with std::string gets generated first
+ // we only want vector<cv::String> because it's more compatible across OpenCV versions
+ if matches!(str_type, StrType::StdString(_)) {
// We need to generate return wrappers for std::vector<cv::String>, but it has several issues:
// * we can't use get_canonical_type() because it resolves into compiler dependent inner type like
// std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>
@@ -946,100 +935,30 @@ impl<'tu, 'ge> TypeRef<'tu, 'ge> {
// That's why we try to resolve both types and check if they are the same, if they are we only generate
// vector<std::string> if not - both.
let vec_cv_string = self.gen_env.resolve_typeref("std::vector<cv::String>");
- if let DependentTypeMode::ForReturn(def_location) = mode {
- let vec_std_string = self.gen_env.resolve_typeref("std::vector<std::string>");
- let vec_type_ref = if vec_cv_string.canonical() == vec_std_string.canonical() {
- vec_std_string
- } else {
- vec.type_ref()
- };
- let const_hint = self.get_const_hint(&vec_type_ref);
- out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new_ext(
- vec_type_ref,
- const_hint,
- def_location,
- self.gen_env,
- )));
- }
- // implement workaround for race when type with std::string gets generated first
- // we only want vector<cv::String> because it's more compatible across OpenCV versions
- if matches!(str_type, StrType::StdString(_)) {
- let tref = vec_cv_string;
- out.push(DependentType::from_vector(
- tref.as_vector().expect("Not possible unless something is terribly broken"),
+ out.push(GeneratedType::Vector(
+ vec_cv_string
+ .as_vector()
+ .expect("Not possible unless something is terribly broken"),
));
} else {
- out.push(DependentType::from_vector(vec))
+ out.push(GeneratedType::Vector(vec))
}
} else {
- if let DependentTypeMode::ForReturn(def_location) = mode {
- let vec_type_ref = vec.type_ref().canonical_clang();
- let const_hint = self.get_const_hint(&vec_type_ref);
- out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new_ext(
- vec_type_ref,
- const_hint,
- def_location,
- self.gen_env,
- )));
- }
- out.push(DependentType::from_vector(vec));
+ out.push(GeneratedType::Vector(vec));
}
out
}
- Kind::StdTuple(tuple) => vec![DependentType::Tuple(tuple)],
+ Kind::StdTuple(tuple) => vec![GeneratedType::Tuple(tuple)],
Kind::SmartPtr(ptr) => {
- let mut out = ptr.dependent_types();
- if let DependentTypeMode::ForReturn(def_location) = mode {
- let ptr_type_ref = ptr.type_ref().canonical_clang();
- let const_hint = self.get_const_hint(&ptr_type_ref);
- out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new_ext(
- ptr_type_ref,
- const_hint,
- def_location,
- self.gen_env,
- )));
- }
- out.push(DependentType::from_smart_ptr(ptr));
+ let mut out = ptr.generated_types();
+ out.push(GeneratedType::SmartPtr(ptr));
out
}
- Kind::Typedef(typedef) => typedef.dependent_types(),
+ Kind::Typedef(typedef) => typedef.generated_types(),
_ => {
let mut out = vec![];
- if let DependentTypeMode::ForReturn(def_location) = mode {
- if !self.is_generic() && !self.is_void() {
- if self.as_string().is_some() {
- let type_ref = TypeRef::new(
- self
- .gen_env
- .resolve_type(self.cpp_extern_return(ConstnessOverride::No).as_ref())
- .expect("Can't resolve string cpp_extern_return()"),
- self.gen_env,
- );
- let def_location = match def_location {
- DefinitionLocation::Type => DefinitionLocation::Custom(self.rust_module().into_owned()),
- dl => dl,
- };
- out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
- type_ref,
- def_location,
- self.gen_env,
- )));
- } else {
- let type_ref = self.canonical_clang();
- let const_hint = self.get_const_hint(&type_ref);
- out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new_ext(
- type_ref,
- const_hint,
- def_location,
- self.gen_env,
- )));
if self.as_abstract_class_ptr().is_some() {
- out.push(DependentType::from_abstract_ref_wrapper(AbstractRefWrapper::new(
- self.clone(),
- )))
- }
- }
- }
+ out.push(GeneratedType::AbstractRefWrapper(AbstractRefWrapper::new(self.clone())))
}
out
}

MODIFY
binding-generator/src/typedef.rs
binding-generator/src/typedef.rs
@@ -3,8 +3,8 @@ use std::fmt;
use clang::Entity;
-use crate::type_ref::{CppNameStyle, DependentTypeMode, FishStyle, Kind, NameStyle, TypeRefTypeHint};
-use crate::{settings, DefaultElement, DependentType, Element, EntityElement, GeneratorEnv, TypeRef};
+use crate::type_ref::{CppNameStyle, FishStyle, Kind, NameStyle, TypeRefTypeHint};
+use crate::{settings, DefaultElement, Element, EntityElement, GeneratedType, GeneratorEnv, TypeRef};
#[derive(Clone)]
pub struct Typedef<'tu, 'ge> {
@@ -33,8 +33,8 @@ impl<'tu, 'ge> Typedef<'tu, 'ge> {
)
}
- pub fn dependent_types(&self) -> Vec<DependentType<'tu, 'ge>> {
- self.underlying_type_ref().dependent_types(DependentTypeMode::None)
+ pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
+ self.underlying_type_ref().generated_types()
}
}
@@ -47,8 +47,12 @@ impl<'tu> EntityElement<'tu> for Typedef<'tu, '_> {
impl Element for Typedef<'_, '_> {
fn is_excluded(&self) -> bool {
DefaultElement::is_excluded(self)
- || self.rust_name(NameStyle::ref_()) == self.underlying_type_ref().rust_name(NameStyle::ref_()) // fixes recursive typedefs like Cv16suf
|| settings::PRIMITIVE_TYPEDEFS.contains_key(self.cpp_name(CppNameStyle::Reference).as_ref())
+ || {
+ let underlying_type = self.underlying_type_ref();
+ // fixes recursive typedefs like Cv16suf
+ self.rust_name(NameStyle::ref_()) == underlying_type.rust_name(NameStyle::ref_()) || underlying_type.is_ignored()
+ }
}
fn is_ignored(&self) -> bool {

MODIFY
binding-generator/src/vector.rs
binding-generator/src/vector.rs
@@ -3,11 +3,8 @@ use std::fmt;
use clang::{Entity, Type};
-use crate::type_ref::{ConstnessOverride, CppNameStyle, FishStyle, NameStyle, TemplateArg};
-use crate::{
- DefaultElement, DefinitionLocation, DependentType, DependentTypeMode, Element, EntityElement, GeneratorEnv, ReturnTypeWrapper,
- TypeRef,
-};
+use crate::type_ref::{CppNameStyle, FishStyle, NameStyle, TemplateArg};
+use crate::{DefaultElement, Element, EntityElement, GeneratedType, GeneratorEnv, TypeRef};
#[derive(Clone)]
pub struct Vector<'tu, 'ge> {
@@ -41,72 +38,8 @@ impl<'tu, 'ge> Vector<'tu, 'ge> {
.expect("vector template argument list is empty")
}
- pub fn dependent_types(&self) -> Vec<DependentType<'tu, 'ge>> {
- let element_type = self.element_type();
- let is_data_type = self.is_data_type(&element_type);
- let mut out = element_type.dependent_types(DependentTypeMode::ForReturn(DefinitionLocation::Type));
- out.reserve(
- 1 + if is_data_type {
- 3
- } else {
- 0
- },
- );
- if element_type.as_string().is_some() {
- out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
- TypeRef::new(
- self
- .gen_env
- .resolve_type(&element_type.cpp_extern_return(ConstnessOverride::No))
- .expect("Can't resolve string cpp_extern_return()"),
- self.gen_env,
- ),
- DefinitionLocation::Custom(element_type.rust_module().into_owned()),
- self.gen_env,
- )));
- } else {
- out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
- element_type.canonical_clang(),
- DefinitionLocation::Module,
- self.gen_env,
- )));
- }
- if is_data_type {
- out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
- TypeRef::new(
- self
- .gen_env
- .resolve_type("cv::_InputArray")
- .expect("Can't resolve _InputArray"),
- self.gen_env,
- ),
- DefinitionLocation::Custom(element_type.rust_module().into_owned()),
- self.gen_env,
- )));
- out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
- TypeRef::new(
- self
- .gen_env
- .resolve_type("cv::_OutputArray")
- .expect("Can't resolve _OutputArray"),
- self.gen_env,
- ),
- DefinitionLocation::Custom(element_type.rust_module().into_owned()),
- self.gen_env,
- )));
- out.push(DependentType::from_return_type_wrapper(ReturnTypeWrapper::new(
- TypeRef::new(
- self
- .gen_env
- .resolve_type("cv::_InputOutputArray")
- .expect("Can't resolve _InputOutputArray"),
- self.gen_env,
- ),
- DefinitionLocation::Custom(element_type.rust_module().into_owned()),
- self.gen_env,
- )));
- }
- out
+ pub fn generated_types(&self) -> Vec<GeneratedType<'tu, 'ge>> {
+ self.element_type().generated_types()
}
pub fn rust_localalias(&self) -> Cow<str> {

MODIFY
binding-generator/src/writer/rust_native/element.rs
binding-generator/src/writer/rust_native/element.rs
-use crate::DependentType;
+use crate::GeneratedType;
pub trait RustNativeGeneratedElement {
/// Element order in the output file, lower means earlier
@@ -21,54 +21,13 @@ pub trait RustNativeGeneratedElement {
}
}
-impl RustNativeGeneratedElement for DependentType<'_, '_> {
- fn element_order(&self) -> u8 {
- match self {
- DependentType::ReturnTypeWrapper(ret) => ret.element_order(),
- DependentType::AbstractRefWrapper(r) => r.element_order(),
- DependentType::Vector(vec) => vec.element_order(),
- DependentType::SmartPtr(ptr) => ptr.element_order(),
- DependentType::Tuple(tuple) => tuple.element_order(),
- }
- }
-
- fn element_safe_id(&self) -> String {
- match self {
- DependentType::ReturnTypeWrapper(ret) => ret.element_safe_id(),
- DependentType::AbstractRefWrapper(r) => r.element_safe_id(),
- DependentType::Vector(vec) => vec.element_safe_id(),
- DependentType::SmartPtr(ptr) => ptr.element_safe_id(),
- DependentType::Tuple(tuple) => tuple.element_safe_id(),
- }
- }
-
- fn gen_rust(&self, opencv_version: &str) -> String {
- match self {
- DependentType::ReturnTypeWrapper(ret) => ret.gen_rust(opencv_version),
- DependentType::AbstractRefWrapper(r) => r.gen_rust(opencv_version),
- DependentType::Vector(vec) => vec.gen_rust(opencv_version),
- DependentType::SmartPtr(ptr) => ptr.gen_rust(opencv_version),
- DependentType::Tuple(tuple) => tuple.gen_rust(opencv_version),
- }
- }
-
- fn gen_rust_exports(&self) -> String {
- match self {
- DependentType::ReturnTypeWrapper(ret) => ret.gen_rust_exports(),
- DependentType::AbstractRefWrapper(r) => r.gen_rust_exports(),
- DependentType::Vector(vec) => vec.gen_rust_exports(),
- DependentType::SmartPtr(ptr) => ptr.gen_rust_exports(),
- DependentType::Tuple(tuple) => tuple.gen_rust_exports(),
- }
- }
-
- fn gen_cpp(&self) -> String {
+impl<'ne, 'tu: 'ne, 'ge: 'ne> AsRef<dyn RustNativeGeneratedElement + 'ne> for GeneratedType<'tu, 'ge> {
+ fn as_ref(&self) -> &(dyn RustNativeGeneratedElement + 'ne) {
match self {
- DependentType::ReturnTypeWrapper(ret) => ret.gen_cpp(),
- DependentType::AbstractRefWrapper(r) => r.gen_cpp(),
- DependentType::Vector(vec) => vec.gen_cpp(),
- DependentType::SmartPtr(ptr) => ptr.gen_cpp(),
- DependentType::Tuple(tuple) => tuple.gen_cpp(),
+ GeneratedType::AbstractRefWrapper(r) => r,
+ GeneratedType::Vector(vec) => vec,
+ GeneratedType::SmartPtr(ptr) => ptr,
+ GeneratedType::Tuple(tuple) => tuple,
}
}
}

MODIFY
binding-generator/src/writer/rust_native/mod.rs
binding-generator/src/writer/rust_native/mod.rs
@@ -12,8 +12,8 @@ use element::RustNativeGeneratedElement;
use crate::type_ref::{Constness, CppNameStyle, NameStyle};
use crate::{
- comment, is_ephemeral_header, opencv_module_from_path, settings, Class, CompiledInterpolation, Const, DependentType, Element,
- Enum, Func, GeneratorVisitor, IteratorExt, StrExt, Typedef,
+ comment, is_ephemeral_header, opencv_module_from_path, settings, Class, CompiledInterpolation, Const, Element, Enum, Func,
+ GeneratedType, GeneratorVisitor, IteratorExt, StrExt, Typedef,
};
mod abstract_ref_wrapper;
@@ -24,7 +24,6 @@ mod enumeration;
mod func;
mod func_desc;
pub mod renderer;
-mod return_type_wrapper;
mod smart_ptr;
mod tuple;
mod type_ref;
@@ -164,7 +163,8 @@ impl GeneratorVisitor for RustNativeBindingWriter<'_> {
self.cpp_classes.push((name, class.gen_cpp()));
}
- fn visit_dependent_type(&mut self, typ: DependentType) {
+ fn visit_generated_type(&mut self, typ: GeneratedType) {
+ let typ = typ.as_ref();
let prio = typ.element_order();
let safe_id = typ.element_safe_id();

DELETE
binding-generator/src/writer/rust_native/return_type_wrapper.rs
-use maplit::hashmap;
-use once_cell::sync::Lazy;
-
-use crate::{CompiledInterpolation, ReturnTypeWrapper, StrExt, StringExt};
-
-use super::RustNativeGeneratedElement;
-
-impl RustNativeGeneratedElement for ReturnTypeWrapper<'_, '_> {
- fn element_order(&self) -> u8 {
- 10
- }
-
- fn element_safe_id(&self) -> String {
- let mut name = self.type_ref().cpp_extern_return(self.const_hint()).into_owned();
- name.cleanup_name();
- format!("{}-{}", self.definition_location(), name)
- }
-
- fn gen_cpp(&self) -> String {
- // this explicit instantiation is needed for visual studio compiler, without it it produces error:
- // C linkage function cannot return C++ class
- static CPP_TPL: Lazy<CompiledInterpolation> =
- Lazy::new(|| include_str!("tpl/return_type_wrapper/cpp.tpl.cpp").compile_interpolation());
-
- CPP_TPL.interpolate(&hashmap! {
- "cpp_full" => self.type_ref().cpp_extern_return(self.const_hint()),
- })
- }
-}

DELETE
binding-generator/src/writer/rust_native/tpl/return_type_wrapper/cpp.tpl.cpp
-template struct Result<{{cpp_full}}>;
-

Rework dependent types