Skip to content

Commit 80123ec

Browse files
committed
Allow creation of Vector<BoxedRef<_>> instances and using them as ToInputArray
1 parent d718187 commit 80123ec

23 files changed

+702
-64
lines changed

binding-generator/src/class.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,6 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
160160
}
161161
}
162162

163-
pub fn is_trait(&self) -> bool {
164-
self.kind().is_boxed()
165-
// self.is_abstract() || self.has_descendants() || settings::FORCE_CLASS_TRAIT.contains(self.cpp_name(CppNameStyle::Reference).as_ref())
166-
}
167-
168163
/// Special case of an empty class with only an anonymous enum inside (e.g. DrawLinesMatchesFlags)
169164
pub fn as_enum(&self) -> Option<Enum<'tu>> {
170165
match self {
@@ -429,7 +424,7 @@ impl<'tu, 'ge> Class<'tu, 'ge> {
429424
&& !fld_type_kind.is_char_ptr_string(fld_type_ref.type_hint())
430425
{
431426
fld_type_ref.set_type_hint(TypeRefTypeHint::PrimitivePtrAsRaw);
432-
} else if fld_type_kind.as_class().map_or(false, |cls| cls.is_trait()) {
427+
} else if fld_type_kind.as_class().map_or(false, |cls| cls.kind().is_trait()) {
433428
fld_type_ref.set_type_hint(TypeRefTypeHint::TraitClassConcrete);
434429
}
435430
let fld_type_kind = fld_type_ref.kind();
@@ -718,7 +713,7 @@ impl fmt::Debug for Class<'_, '_> {
718713
if self.is_polymorphic() {
719714
props.push("polymorphic");
720715
}
721-
if self.is_trait() {
716+
if self.kind().is_trait() {
722717
props.push("trait");
723718
}
724719
if self.as_enum().is_some() {
@@ -800,7 +795,13 @@ impl ClassKind {
800795

801796
pub fn is_boxed(self) -> bool {
802797
match self {
803-
// fixme: Self::System to return true is kind of hack to make sure that system classes are passed by void*, probably better to handle in explicitly in CppPassByVoidPtrRenderLane
798+
Self::Boxed | Self::BoxedForced => true,
799+
Self::Simple | Self::Other | Self::System => false,
800+
}
801+
}
802+
803+
pub fn is_trait(&self) -> bool {
804+
match self {
804805
Self::Boxed | Self::BoxedForced | Self::System => true,
805806
Self::Simple | Self::Other => false,
806807
}

binding-generator/src/class/desc.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ impl<'tu, 'ge> ClassDesc<'tu, 'ge> {
3737
}
3838
}
3939

40+
pub fn system(cpp_refname: impl Into<Rc<str>>, rust_module: impl Into<Rc<str>>) -> Self {
41+
Self {
42+
kind: ClassKind::System,
43+
..Self::boxed(cpp_refname, rust_module)
44+
}
45+
}
46+
4047
/// `cv::Scalar`
4148
pub fn cv_scalar() -> Class<'tu, 'ge> {
4249
Class::new_desc(Self::simple("cv::Scalar", "core"))
@@ -104,7 +111,7 @@ impl<'tu, 'ge> ClassDesc<'tu, 'ge> {
104111

105112
/// `cv::String`
106113
pub fn cv_string() -> Class<'tu, 'ge> {
107-
Class::new_desc(Self::boxed("cv::String", "core"))
114+
Class::new_desc(Self::system("cv::String", "core"))
108115
}
109116

110117
/// `cv::MatConstIterator`

binding-generator/src/type_ref/kind.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'tu, 'ge> TypeRefKind<'tu, 'ge> {
6363
match self {
6464
TypeRefKind::Typedef(tdef) => tdef.underlying_type_ref().kind().extern_pass_kind(),
6565
TypeRefKind::Class(inner) if !inner.string_type().is_some() => {
66-
if inner.kind().is_boxed() {
66+
if inner.kind().is_trait() {
6767
ExternPassKind::ByVoidPtr
6868
} else {
6969
ExternPassKind::ByPtr
@@ -212,10 +212,6 @@ impl<'tu, 'ge> TypeRefKind<'tu, 'ge> {
212212
}
213213
}
214214

215-
pub fn as_simple_class(&self) -> Option<Cow<Class<'tu, 'ge>>> {
216-
self.as_class().filter(|cls| cls.kind().is_simple())
217-
}
218-
219215
pub fn as_vector(&self) -> Option<Cow<Vector<'tu, 'ge>>> {
220216
match self {
221217
TypeRefKind::StdVector(out) => Some(Borrowed(out)),

binding-generator/src/writer/rust_native/class.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ fn gen_rust_class(c: &Class, opencv_version: &str) -> String {
5151
static TRAIT_TPL: Lazy<CompiledInterpolation> = Lazy::new(|| include_str!("tpl/class/trait.tpl.rs").compile_interpolation());
5252

5353
let type_ref = c.type_ref();
54-
let is_trait = c.is_trait();
5554
let class_kind = c.kind();
55+
let is_trait = class_kind.is_trait();
5656
let doc_comment = c.rendered_doc_comment("///", opencv_version);
5757

5858
let mut out = String::new();
@@ -577,7 +577,7 @@ pub trait ClassExt {
577577
impl ClassExt for Class<'_, '_> {
578578
fn rust_trait_name(&self, style: NameStyle, constness: Constness) -> Cow<str> {
579579
let mut out = self.rust_name(style);
580-
if self.is_trait() {
580+
if self.kind().is_trait() {
581581
if constness.is_const() {
582582
out.to_mut().push_str("TraitConst");
583583
} else {

binding-generator/src/writer/rust_native/func.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl RustNativeGeneratedElement for Func<'_, '_> {
293293

294294
let doc_comment = self.rendered_doc_comment("///", _opencv_version);
295295
let visibility = if let Some(cls) = as_instance_method {
296-
if cls.is_trait() {
296+
if cls.kind().is_trait() {
297297
""
298298
} else {
299299
"pub "

binding-generator/src/writer/rust_native/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl GeneratorVisitor for RustNativeBindingWriter<'_> {
165165

166166
fn visit_class(&mut self, class: Class) {
167167
self.emit_debug_log(&class);
168-
if class.is_trait() {
168+
if class.kind().is_trait() {
169169
self.prelude_traits.push(format!(
170170
"super::{}",
171171
class.rust_trait_name(NameStyle::decl(), Constness::Const).into_owned()

binding-generator/src/writer/rust_native/smart_ptr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl RustNativeGeneratedElement for SmartPtr<'_, '_> {
104104
]);
105105

106106
let mut impls = String::new();
107-
if let Some(cls) = pointee_kind.as_class().filter(|cls| cls.is_trait()) {
107+
if let Some(cls) = pointee_kind.as_class().filter(|cls| cls.kind().is_trait()) {
108108
inter_vars.extend([
109109
("base_rust_as_raw_const", cls.rust_as_raw_name(Constness::Const).into()),
110110
("base_rust_as_raw_mut", cls.rust_as_raw_name(Constness::Mut).into()),
@@ -195,7 +195,7 @@ fn extern_functions<'tu, 'ge>(ptr: &SmartPtr<'tu, 'ge>) -> Vec<Func<'tu, 'ge>> {
195195
pointee_type.with_inherent_constness(Constness::Mut),
196196
));
197197
out.push(FuncDesc::method_delete(smartptr_class.clone()));
198-
if let Some(cls) = pointee_kind.as_class().filter(|cls| cls.is_trait()) {
198+
if let Some(cls) = pointee_kind.as_class().filter(|cls| cls.kind().is_trait()) {
199199
for base in all_bases(&cls) {
200200
out.push(method_cast_to_base(
201201
smartptr_class.clone(),
@@ -266,7 +266,7 @@ fn method_new<'tu, 'ge>(
266266
) -> Func<'tu, 'ge> {
267267
let pointee_kind = pointee_type.kind();
268268
let val = if pointee_kind.is_copy(pointee_type.type_hint()) {
269-
if pointee_kind.as_simple_class().is_some() {
269+
if pointee_kind.as_class().map_or(false, |cls| cls.kind().is_simple()) {
270270
panic!("Ptr with simple class is not supported");
271271
} else {
272272
format!("new {typ}(val)", typ = pointee_type.cpp_name(CppNameStyle::Reference)).into()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
vector_boxed_ref! { {{inner_rust_full}} }
3+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
12
vector_copy_non_bool! { {{inner_rust_full}},
23
{{extern_data}}, {{extern_data_mut}}, {{extern_from_slice}},
34
{{extern_clone}},
45
}
56

7+

binding-generator/src/writer/rust_native/tpl/vector/rust_extern.tpl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ vector_extern! { {{inner_rust_full}},
1010
{{extern_push}}, {{extern_insert}},
1111
}
1212

13+

0 commit comments

Comments
 (0)