Improve code generation for Input/OutputArray
MODIFY
binding-generator/src/writer/rust_native/tpl/vector/rust_input_output_array.tpl.rs
binding-generator/src/writer/rust_native/tpl/vector/rust_input_output_array.tpl.rs
@@ -10,13 +10,6 @@ impl core::ToInputArray for {{rust_localalias}} {
}
}
-impl core::ToInputArray for &{{rust_localalias}} {
- #[inline]
- fn input_array(&self) -> Result<core::_InputArray> {
- (*self).input_array()
- }
-}
-
impl core::ToOutputArray for {{rust_localalias}} {
#[inline]
fn output_array(&mut self) -> Result<core::_OutputArray> {
@@ -27,13 +20,6 @@ impl core::ToOutputArray for {{rust_localalias}} {
}
}
-impl core::ToOutputArray for &mut {{rust_localalias}} {
- #[inline]
- fn output_array(&mut self) -> Result<core::_OutputArray> {
- (*self).output_array()
- }
-}
-
impl core::ToInputOutputArray for {{rust_localalias}} {
#[inline]
fn input_output_array(&mut self) -> Result<core::_InputOutputArray> {
@@ -44,9 +30,4 @@ impl core::ToInputOutputArray for {{rust_localalias}} {
}
}
-impl core::ToInputOutputArray for &mut {{rust_localalias}} {
- #[inline]
- fn input_output_array(&mut self) -> Result<core::_InputOutputArray> {
- (*self).input_output_array()
- }
-}
+input_output_array_ref_forward! { {{rust_localalias}} }

MODIFY
src/lib.rs
src/lib.rs
@@ -53,6 +53,7 @@ pub(crate) mod mod_prelude {
ptr_extern_ctor,
vector_copy_non_bool,
vector_extern,
+ input_output_array_ref_forward,
vector_non_copy_or_bool,
};
}

MODIFY
src/manual/core/gpumat.rs
src/manual/core/gpumat.rs
use crate::{
- core::{
- _InputArray,
- _InputOutputArray,
- _OutputArray,
- GpuMat,
- HostMem,
- ToInputArray,
- ToInputOutputArray,
- ToOutputArray,
- },
+ core::{GpuMat, HostMem},
+ input_output_array,
Result,
};
@@ -19,86 +11,5 @@ impl GpuMat {
}
}
-impl ToInputArray for GpuMat {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- _InputArray::from_gpumat(self)
- }
-}
-
-impl ToInputArray for &GpuMat {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- (*self).input_array()
- }
-}
-
-impl ToOutputArray for GpuMat {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- _OutputArray::from_gpumat_mut(self)
- }
-}
-
-impl ToOutputArray for &mut GpuMat {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- (*self).output_array()
- }
-}
-
-impl ToInputOutputArray for GpuMat {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- _InputOutputArray::from_gpumat_mut(self)
- }
-}
-
-impl ToInputOutputArray for &mut GpuMat {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- (*self).input_output_array()
- }
-}
-
-impl ToInputArray for HostMem {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- _InputArray::from_hostmem(self)
- }
-}
-
-impl ToInputArray for &HostMem {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- (*self).input_array()
- }
-}
-
-impl ToOutputArray for HostMem {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- _OutputArray::from_hostmem_mut(self)
- }
-}
-
-impl ToOutputArray for &mut HostMem {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- (*self).output_array()
- }
-}
-
-impl ToInputOutputArray for HostMem {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- _InputOutputArray::from_hostmem_mut(self)
- }
-}
-
-impl ToInputOutputArray for &mut HostMem {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- (*self).input_output_array()
- }
-}
+input_output_array! { GpuMat, from_gpumat, from_gpumat_mut }
+input_output_array! { HostMem, from_hostmem, from_hostmem_mut }

MODIFY
src/manual/core/input_output_array.rs
src/manual/core/input_output_array.rs
@@ -2,6 +2,7 @@ use std::os::raw::c_void;
use crate::{
core::{_InputArray, _InputArrayTrait, _InputOutputArray, _InputOutputArrayTrait, _OutputArray, _OutputArrayTrait},
+ input_output_array,
Result,
sys,
traits::Boxed,
@@ -17,19 +18,7 @@ pub trait ToInputArray {
fn input_array(&self) -> Result<_InputArray>;
}
-impl ToInputArray for f64 {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- _InputArray::from_f64(self)
- }
-}
-
-impl ToInputArray for &f64 {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- (*self).input_array()
- }
-}
+input_output_array! { f64, from_f64 }
/// Trait to serve as a replacement for `OutputArray` in C++ OpenCV
///
@@ -77,3 +66,76 @@ impl<T: _InputOutputArrayTrait> ToInputOutputArray for T {
.map(|ptr| unsafe { _InputOutputArray::from_raw(ptr) })
}
}
+
+#[macro_export]
+macro_rules! input_output_array {
+ ($type: ty, $const_cons: ident) => {
+ impl $crate::manual::core::ToInputArray for $type {
+ #[inline]
+ fn input_array(&self) -> $crate::Result<$crate::core::_InputArray> {
+ $crate::core::_InputArray::$const_cons(self)
+ }
+ }
+
+ $crate::input_array_ref_forward! { $type }
+ };
+
+ ($type: ty, $const_cons: ident, $mut_cons: ident) => {
+ $crate::input_output_array! { $type, $const_cons }
+
+ impl $crate::manual::core::ToOutputArray for $type {
+ #[inline]
+ fn output_array(&mut self) -> $crate::Result<$crate::core::_OutputArray> {
+ $crate::core::_OutputArray::$mut_cons(self)
+ }
+ }
+
+ impl $crate::manual::core::ToInputOutputArray for $type {
+ #[inline]
+ fn input_output_array(&mut self) -> $crate::Result<$crate::core::_InputOutputArray> {
+ $crate::core::_InputOutputArray::$mut_cons(self)
+ }
+ }
+
+ $crate::output_array_ref_forward! { $type }
+ };
+}
+
+#[macro_export]
+macro_rules! input_array_ref_forward {
+ ($type: ty) => {
+ impl $crate::manual::core::ToInputArray for &$type {
+ #[inline]
+ fn input_array(&self) -> $crate::Result<$crate::core::_InputArray> {
+ (*self).input_array()
+ }
+ }
+ };
+}
+
+#[macro_export]
+macro_rules! output_array_ref_forward {
+ ($type: ty) => {
+ impl $crate::manual::core::ToOutputArray for &mut $type {
+ #[inline]
+ fn output_array(&mut self) -> $crate::Result<$crate::core::_OutputArray> {
+ (*self).output_array()
+ }
+ }
+
+ impl $crate::manual::core::ToInputOutputArray for &mut $type {
+ #[inline]
+ fn input_output_array(&mut self) -> $crate::Result<$crate::core::_InputOutputArray> {
+ (*self).input_output_array()
+ }
+ }
+ };
+}
+
+#[macro_export]
+macro_rules! input_output_array_ref_forward {
+ ($type: ty) => {
+ $crate::input_array_ref_forward! { $type }
+ $crate::output_array_ref_forward! { $type }
+ }
+}

MODIFY
src/manual/core/mat.rs
src/manual/core/mat.rs
@@ -9,22 +9,9 @@ use std::{
pub use mat_::*;
use crate::{
- core::{
- self,
- _InputArray,
- _InputOutputArray,
- _OutputArray,
- MatExpr,
- MatSize,
- MatStep,
- Point,
- Scalar,
- ToInputArray,
- ToInputOutputArray,
- ToOutputArray,
- UMat,
- },
+ core::{self, MatExpr, MatSize, MatStep, Point, Scalar, UMat},
Error,
+ input_output_array,
platform_types::size_t,
prelude::*,
Result,
@@ -574,47 +561,7 @@ impl<T: MatTraitConst + ?Sized> MatTraitConstManual for T {}
impl<T: MatTrait + ?Sized> MatTraitManual for T {}
-impl ToInputArray for Mat {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- _InputArray::from_mat(self)
- }
-}
-
-impl ToInputArray for &Mat {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- (*self).input_array()
- }
-}
-
-impl ToOutputArray for Mat {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- _OutputArray::from_mat_mut(self)
- }
-}
-
-impl ToOutputArray for &mut Mat {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- (*self).output_array()
- }
-}
-
-impl ToInputOutputArray for Mat {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- _InputOutputArray::from_mat_mut(self)
- }
-}
-
-impl ToInputOutputArray for &mut Mat {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- (*self).input_output_array()
- }
-}
+input_output_array! { Mat, from_mat, from_mat_mut }
impl fmt::Debug for Mat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -653,47 +600,7 @@ pub trait UMatTraitConstManual: UMatTraitConst {
impl<T: UMatTraitConst> UMatTraitConstManual for T {}
-impl ToInputArray for UMat {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- _InputArray::from_umat(self)
- }
-}
-
-impl ToInputArray for &UMat {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- (*self).input_array()
- }
-}
-
-impl ToOutputArray for UMat {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- _OutputArray::from_umat_mut(self)
- }
-}
-
-impl ToOutputArray for &mut UMat {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- (*self).output_array()
- }
-}
-
-impl ToInputOutputArray for UMat {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- _InputOutputArray::from_umat_mut(self)
- }
-}
-
-impl ToInputOutputArray for &mut UMat {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- (*self).input_output_array()
- }
-}
+input_output_array!{ UMat, from_umat, from_umat_mut }
#[cfg(ocvrs_opencv_branch_32)]
pub trait MatSizeTraitConstManual: MatSizeTraitConst {
@@ -766,19 +673,7 @@ pub trait MatConstIteratorTraitManual: MatConstIteratorTrait {
impl<T: MatConstIteratorTrait> MatConstIteratorTraitManual for T {}
-impl ToInputArray for MatExpr {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- _InputArray::from_matexpr(self)
- }
-}
-
-impl ToInputArray for &MatExpr {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- (*self).input_array()
- }
-}
+input_output_array! { MatExpr, from_matexpr }
mod private {
pub trait Sealed {}

MODIFY
src/manual/core/vec.rs
src/manual/core/vec.rs
@@ -2,6 +2,7 @@ use std::ffi::c_void;
use crate::{
core::{_InputArray, ToInputArray},
+ input_array_ref_forward,
opencv_type_simple_generic,
Result,
sys,
@@ -112,9 +113,4 @@ impl ToInputArray for Scalar_<f64> {
}
}
-impl ToInputArray for &Scalar_<f64> {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- (*self).input_array()
- }
-}
+input_array_ref_forward! { Scalar_<f64> }

MODIFY
src/manual/types.rs
src/manual/types.rs
pub use abstract_ref::*;
use crate::{
- core::{_InputArray, _InputOutputArray, _OutputArray, ToInputArray, ToInputOutputArray, ToOutputArray},
- Result,
+ input_output_array,
types,
};
@@ -17,86 +16,6 @@ impl From<Unit> for () {
fn from(_: Unit) -> Self {}
}
-impl ToInputArray for types::VectorOfMat {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- _InputArray::from_mat_vec(self)
- }
-}
-
-impl ToInputArray for &types::VectorOfMat {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- (*self).input_array()
- }
-}
-
-impl ToOutputArray for types::VectorOfMat {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- _OutputArray::from_mat_vec_mut(self)
- }
-}
-
-impl ToOutputArray for &mut types::VectorOfMat {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- (*self).output_array()
- }
-}
-
-impl ToInputOutputArray for types::VectorOfMat {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- _InputOutputArray::from_mat_vec_mut(self)
- }
-}
-
-impl ToInputOutputArray for &mut types::VectorOfMat {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- (*self).input_output_array()
- }
-}
+input_output_array! { types::VectorOfMat, from_mat_vec, from_mat_vec_mut }
-impl ToInputArray for types::VectorOfUMat {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- _InputArray::from_umat_vec(self)
- }
-}
-
-impl ToInputArray for &types::VectorOfUMat {
- #[inline]
- fn input_array(&self) -> Result<_InputArray> {
- (*self).input_array()
- }
-}
-
-impl ToOutputArray for types::VectorOfUMat {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- _OutputArray::from_umat_vec_mut(self)
- }
-}
-
-impl ToOutputArray for &mut types::VectorOfUMat {
- #[inline]
- fn output_array(&mut self) -> Result<_OutputArray> {
- (*self).output_array()
- }
-}
-
-impl ToInputOutputArray for types::VectorOfUMat {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- _InputOutputArray::from_umat_vec_mut(self)
- }
-}
-
-impl ToInputOutputArray for &mut types::VectorOfUMat {
- #[inline]
- fn input_output_array(&mut self) -> Result<_InputOutputArray> {
- (*self).input_output_array()
- }
-}
+input_output_array! { types::VectorOfUMat, from_umat_vec, from_umat_vec_mut }

Improve code generation for Input/OutputArray