Implement Default when possible
MODIFY
binding-generator/src/func.rs
binding-generator/src/func.rs
@@ -336,6 +336,10 @@ impl<'tu, 'ge> Func<'tu, 'ge> {
|| settings::FORCE_NOEXCEPT.contains(&(self.cpp_fullname().as_ref(), self.clang_arguments().len()))
}
+ pub fn is_default_constructor(&self) -> bool {
+ self.entity.is_default_constructor() && !self.has_arguments()
+ }
+
pub fn is_clone(&self) -> bool {
if self.rust_leafname(FishStyle::No) == "clone" {
if let Some(c) = self.as_instance_method() {
@@ -721,6 +725,7 @@ impl fmt::Debug for Func<'_, '_> {
self.update_debug_struct(&mut debug_struct)
.field("export_config", &self.gen_env.get_export_config(self.entity))
.field("is_const", &self.constness())
+ .field("is_infallible", &self.is_infallible())
.field("type_hint", &self.type_hint)
.field("kind", &self.kind())
.field("return_type", &self.return_type())

MODIFY
binding-generator/src/writer/rust_native/class.rs
binding-generator/src/writer/rust_native/class.rs
@@ -33,6 +33,10 @@ fn gen_rust_class(c: &Class, opencv_version: &str) -> String {
|| include_str!("tpl/class/impl_clone.tpl.rs").compile_interpolation()
);
+ static IMPL_DEFAULT_TPL: Lazy<CompiledInterpolation> = Lazy::new(
+ || include_str!("tpl/class/impl_default.tpl.rs").compile_interpolation()
+ );
+
static SIMPLE_TPL: Lazy<CompiledInterpolation> = Lazy::new(
|| include_str!("tpl/class/simple.tpl.rs").compile_interpolation()
);
@@ -265,6 +269,14 @@ fn gen_rust_class(c: &Class, opencv_version: &str) -> String {
let mut inherent_methods_pool = NamePool::with_capacity(method_count);
let is_trait = c.is_trait();
+ if let Some(def_cons) = mut_methods.iter().find(|m| m.is_default_constructor() && !m.is_excluded()) {
+ if def_cons.is_infallible() {
+ impls += &IMPL_DEFAULT_TPL.interpolate(&hashmap! {
+ "rust_local" => rust_local.as_ref(),
+ });
+ }
+ }
+
inherent_const_methods.push_str(&if is_trait {
rust_generate_funcs(
const_methods.iter()

ADD
binding-generator/src/writer/rust_native/tpl/class/impl_default.tpl.rs
+impl Default for {{rust_local}} {
+ #[inline]
+ /// Forwards to infallible Self::default()
+ fn default() -> Self {
+ Self::default()
+ }
+}
+
+

Implement Default when possible