Improve auxiliary types generation
MODIFY
src/lib.rs
src/lib.rs
@@ -39,6 +39,7 @@ pub(crate) mod mod_prelude {
pub use crate::{
boxed_cast_base, boxed_cast_descendant,
core::{CV_MAKETYPE, CV_MAKE_TYPE},
+ extern_arg_send, extern_container_send, extern_receive, extern_send,
hub_prelude::*,
input_output_array_ref_forward,
mod_prelude_sys::*,

MODIFY
src/manual/core/input_output_array.rs
src/manual/core/input_output_array.rs
@@ -81,7 +81,7 @@ impl<T: _InputOutputArrayTrait> ToInputOutputArray for T {
#[macro_export]
macro_rules! input_output_array {
($type: ty, $const_cons: ident) => {
- impl $crate::manual::core::ToInputArray for $type {
+ impl $crate::core::ToInputArray for $type {
#[inline]
fn input_array(&self) -> $crate::Result<$crate::core::_InputArray> {
$crate::core::_InputArray::$const_cons(self)
@@ -94,14 +94,14 @@ macro_rules! input_output_array {
($type: ty, $const_cons: ident, $mut_cons: ident) => {
$crate::input_output_array! { $type, $const_cons }
- impl $crate::manual::core::ToOutputArray for $type {
+ impl $crate::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 {
+ impl $crate::core::ToInputOutputArray for $type {
#[inline]
fn input_output_array(&mut self) -> $crate::Result<$crate::core::_InputOutputArray> {
$crate::core::_InputOutputArray::$mut_cons(self)
@@ -115,7 +115,7 @@ macro_rules! input_output_array {
#[macro_export]
macro_rules! input_array_ref_forward {
($type: ty) => {
- impl $crate::manual::core::ToInputArray for &$type {
+ impl $crate::core::ToInputArray for &$type {
#[inline]
fn input_array(&self) -> $crate::Result<$crate::core::_InputArray> {
(*self).input_array()
@@ -127,14 +127,14 @@ macro_rules! input_array_ref_forward {
#[macro_export]
macro_rules! output_array_ref_forward {
($type: ty) => {
- impl $crate::manual::core::ToOutputArray for &mut $type {
+ impl $crate::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 {
+ impl $crate::core::ToInputOutputArray for &mut $type {
#[inline]
fn input_output_array(&mut self) -> $crate::Result<$crate::core::_InputOutputArray> {
(*self).input_output_array()

MODIFY
src/manual/core/mat/mat_.rs
src/manual/core/mat/mat_.rs
@@ -168,7 +168,7 @@ impl<T> OpenCVTypeArg<'_> for Mat_<T> {
}
}
-impl<T> OpenCVTypeExternContainer for Mat_<T> {
+impl<T> OpenCVTypeExternContainer<'_> for Mat_<T> {
type ExternSend = *const c_void;
type ExternSendMut = *mut c_void;

MODIFY
src/manual/core/matx.rs
src/manual/core/matx.rs
-use std::{ffi::c_void, fmt, mem::ManuallyDrop};
+use std::fmt;
+use std::mem::ManuallyDrop;
use num_traits::{One, Zero};
-use crate::{
- core::{self, ToInputArray, ToInputOutputArray, ToOutputArray, _InputArray, _InputOutputArray, _OutputArray},
- manual::core::sized::*,
- sys,
- traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
- Error, Result,
-};
+use crate::core::{ToInputArray, ToInputOutputArray, ToOutputArray, _InputArray, _InputOutputArray, _OutputArray};
+use crate::manual::core::sized::*;
+use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
+use crate::{core, extern_receive, extern_send, sys, Error, Result};
fn index_check(idx: (usize, usize), rows: usize, cols: usize) -> Result<()> {
if idx.0 >= rows {
@@ -196,7 +194,7 @@ impl<T, A: SizedArray<T>> OpenCVTypeArg<'_> for Matx<T, A> {
}
}
-impl<T, A: SizedArray<T>> OpenCVTypeExternContainer for Matx<T, A> {
+impl<T, A: SizedArray<T>> OpenCVTypeExternContainer<'_> for Matx<T, A> {
type ExternSend = *const Self;
type ExternSendMut = *mut Self;
@@ -301,19 +299,24 @@ where
#[doc(hidden)]
pub trait MatxExtern<T, A: SizedArray<T>> {
#[doc(hidden)]
- unsafe fn extern_input_array(&self) -> sys::Result<*mut c_void>;
+ unsafe fn extern_input_array(&self) -> sys::Result<extern_receive!(_InputArray)>;
#[doc(hidden)]
- unsafe fn extern_output_array(&mut self) -> sys::Result<*mut c_void>;
+ unsafe fn extern_output_array(&mut self) -> sys::Result<extern_receive!(_OutputArray)>;
#[doc(hidden)]
- unsafe fn extern_input_output_array(&mut self) -> sys::Result<*mut c_void>;
+ unsafe fn extern_input_output_array(&mut self) -> sys::Result<extern_receive!(_InputOutputArray)>;
}
macro_rules! matx_extern {
($type: ty, $array: ty, $extern_input_array: ident, $extern_ouput_array: ident, $extern_input_array_output: ident) => {
- impl $crate::manual::core::MatxExtern<$type, $array> for $crate::manual::core::Matx<$type, $array> {
+ extern "C" {
+ fn $extern_input_array(instance: extern_send!($crate::core::Matx<$type, $array>), ocvrs_return: *mut $crate::sys::Result<extern_receive!($crate::core::_InputArray)>);
+ fn $extern_ouput_array(instance: extern_send!(mut $crate::core::Matx<$type, $array>), ocvrs_return: *mut $crate::sys::Result<extern_receive!($crate::core::_OutputArray)>);
+ fn $extern_input_array_output(instance: extern_send!(mut $crate::core::Matx<$type, $array>), ocvrs_return: *mut $crate::sys::Result<extern_receive!($crate::core::_InputOutputArray)>);
+ }
+
+ impl $crate::core::MatxExtern<$type, $array> for $crate::core::Matx<$type, $array> {
#[inline]
- unsafe fn extern_input_array(&self) -> $crate::sys::Result<*mut ::std::ffi::c_void> {
- extern "C" { fn $extern_input_array(instance: *const $crate::manual::core::Matx<$type, $array>, ocvrs_return: *mut $crate::sys::Result<*mut ::std::ffi::c_void>); }
+ unsafe fn extern_input_array(&self) -> $crate::sys::Result<extern_receive!($crate::core::_InputArray)> {
return_send!(via ocvrs_return);
$extern_input_array(self, ocvrs_return.as_mut_ptr());
return_receive!(ocvrs_return => ret);
@@ -321,8 +324,7 @@ macro_rules! matx_extern {
}
#[inline]
- unsafe fn extern_output_array(&mut self) -> $crate::sys::Result<*mut ::std::ffi::c_void> {
- extern "C" { fn $extern_ouput_array(instance: *mut $crate::manual::core::Matx<$type, $array>, ocvrs_return: *mut $crate::sys::Result<*mut ::std::ffi::c_void>); }
+ unsafe fn extern_output_array(&mut self) -> $crate::sys::Result<extern_receive!($crate::core::_OutputArray)> {
return_send!(via ocvrs_return);
$extern_ouput_array(self, ocvrs_return.as_mut_ptr());
return_receive!(ocvrs_return => ret);
@@ -330,8 +332,7 @@ macro_rules! matx_extern {
}
#[inline]
- unsafe fn extern_input_output_array(&mut self) -> $crate::sys::Result<*mut ::std::ffi::c_void> {
- extern "C" { fn $extern_input_array_output(instance: *mut $crate::manual::core::Matx<$type, $array>, ocvrs_return: *mut $crate::sys::Result<*mut ::std::ffi::c_void>); }
+ unsafe fn extern_input_output_array(&mut self) -> $crate::sys::Result<extern_receive!($crate::core::_InputOutputArray)> {
return_send!(via ocvrs_return);
$extern_input_array_output(self, ocvrs_return.as_mut_ptr());
return_receive!(ocvrs_return => ret);

MODIFY
src/manual/core/vec.rs
src/manual/core/vec.rs
-use std::{
- array,
- ffi::c_void,
- ops::{Deref, DerefMut, MulAssign},
-};
+use std::array;
+use std::ops::{Deref, DerefMut, MulAssign};
use num_traits::Float;
-use crate::{
- core::{ToInputArray, ToInputOutputArray, ToOutputArray, _InputArray, _InputOutputArray, _OutputArray},
- sys,
- traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
- Result,
-};
+use crate::core::{ToInputArray, ToInputOutputArray, ToOutputArray, _InputArray, _InputOutputArray, _OutputArray};
+use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
+use crate::{extern_receive, extern_send, sys, Result};
mod operations;
@@ -129,7 +123,7 @@ impl<T, const N: usize> OpenCVTypeArg<'_> for VecN<T, N> {
}
}
-impl<T, const N: usize> OpenCVTypeExternContainer for VecN<T, N> {
+impl<T, const N: usize> OpenCVTypeExternContainer<'_> for VecN<T, N> {
type ExternSend = *const Self;
type ExternSendMut = *mut Self;
@@ -218,19 +212,24 @@ where
#[doc(hidden)]
pub trait VecExtern<T, const N: usize> {
#[doc(hidden)]
- unsafe fn extern_input_array(&self) -> sys::Result<*mut c_void>;
+ unsafe fn extern_input_array(&self) -> sys::Result<extern_receive!(_InputArray)>;
#[doc(hidden)]
- unsafe fn extern_output_array(&mut self) -> sys::Result<*mut c_void>;
+ unsafe fn extern_output_array(&mut self) -> sys::Result<extern_receive!(_OutputArray)>;
#[doc(hidden)]
- unsafe fn extern_input_output_array(&mut self) -> sys::Result<*mut c_void>;
+ unsafe fn extern_input_output_array(&mut self) -> sys::Result<extern_receive!(_InputOutputArray)>;
}
macro_rules! vecn_extern {
($type: ty, $len: expr, $extern_input_array: ident, $extern_ouput_array: ident, $extern_input_array_output: ident) => {
- impl $crate::manual::core::VecExtern<$type, $len> for $crate::manual::core::VecN<$type, $len> {
+ extern "C" {
+ fn $extern_input_array(instance: extern_send!($crate::core::VecN<$type, $len>), ocvrs_return: *mut $crate::sys::Result<extern_receive!($crate::core::_InputArray)>);
+ fn $extern_ouput_array(instance: extern_send!(mut $crate::core::VecN<$type, $len>), ocvrs_return: *mut $crate::sys::Result<extern_receive!($crate::core::_OutputArray)>);
+ fn $extern_input_array_output(instance: extern_send!(mut $crate::core::VecN<$type, $len>), ocvrs_return: *mut $crate::sys::Result<extern_receive!($crate::core::_InputOutputArray)>);
+ }
+
+ impl $crate::core::VecExtern<$type, $len> for $crate::core::VecN<$type, $len> {
#[inline]
- unsafe fn extern_input_array(&self) -> $crate::sys::Result<*mut ::std::ffi::c_void> {
- extern "C" { fn $extern_input_array(instance: *const $crate::manual::core::VecN<$type, $len>, ocvrs_return: *mut $crate::sys::Result<*mut ::std::ffi::c_void>); }
+ unsafe fn extern_input_array(&self) -> $crate::sys::Result<extern_receive!($crate::core::_InputArray)> {
return_send!(via ocvrs_return);
$extern_input_array(self, ocvrs_return.as_mut_ptr());
return_receive!(ocvrs_return => ret);
@@ -238,8 +237,7 @@ macro_rules! vecn_extern {
}
#[inline]
- unsafe fn extern_output_array(&mut self) -> $crate::sys::Result<*mut ::std::ffi::c_void> {
- extern "C" { fn $extern_ouput_array(instance: *mut $crate::manual::core::VecN<$type, $len>, ocvrs_return: *mut $crate::sys::Result<*mut ::std::ffi::c_void>); }
+ unsafe fn extern_output_array(&mut self) -> $crate::sys::Result<extern_receive!($crate::core::_OutputArray)> {
return_send!(via ocvrs_return);
$extern_ouput_array(self, ocvrs_return.as_mut_ptr());
return_receive!(ocvrs_return => ret);
@@ -247,8 +245,7 @@ macro_rules! vecn_extern {
}
#[inline]
- unsafe fn extern_input_output_array(&mut self) -> $crate::sys::Result<*mut ::std::ffi::c_void> {
- extern "C" { fn $extern_input_array_output(instance: *mut $crate::manual::core::VecN<$type, $len>, ocvrs_return: *mut $crate::sys::Result<*mut ::std::ffi::c_void>); }
+ unsafe fn extern_input_output_array(&mut self) -> $crate::sys::Result<extern_receive!($crate::core::_InputOutputArray)> {
return_send!(via ocvrs_return);
$extern_input_array_output(self, ocvrs_return.as_mut_ptr());
return_receive!(ocvrs_return => ret);

MODIFY
src/manual/types/abstract_ref.rs
src/manual/types/abstract_ref.rs
@@ -48,7 +48,7 @@ impl<'r, T: ?Sized> OpenCVTypeArg<'r> for AbstractRefMut<'r, T> {
}
}
-impl<T: ?Sized> OpenCVTypeExternContainer for AbstractRefMut<'_, T> {
+impl<T: ?Sized> OpenCVTypeExternContainer<'_> for AbstractRefMut<'_, T> {
type ExternSend = *const c_void;
type ExternSendMut = *mut c_void;

MODIFY
src/traits/boxed.rs
src/traits/boxed.rs
@@ -69,7 +69,7 @@ macro_rules! opencv_type_boxed {
}
}
- impl $crate::traits::OpenCVTypeExternContainer for $type {
+ impl $crate::traits::OpenCVTypeExternContainer<'_> for $type {
type ExternSend = *const ::std::ffi::c_void;
type ExternSendMut = *mut ::std::ffi::c_void;

MODIFY
src/traits/opencv_type.rs
src/traits/opencv_type.rs
@@ -30,7 +30,7 @@ pub trait OpenCVTypeArg<'a>: Sized {
/// Container to help marshall type over FFI boundary, e.g. CString for String or &str, for most other
/// types it's Self
#[doc(hidden)]
- type ExternContainer: OpenCVTypeExternContainer;
+ type ExternContainer: OpenCVTypeExternContainer<'a>;
/// Convert Self into external container with possible error result, it shouldn't panic
#[doc(hidden)]
@@ -45,7 +45,7 @@ pub trait OpenCVTypeArg<'a>: Sized {
/// Common trait for the type that is used to help marshall OpenCV related type over the FFI boundary
#[doc(hidden)]
-pub trait OpenCVTypeExternContainer {
+pub trait OpenCVTypeExternContainer<'a> {
/// Type when constant Self is sent to C++ function, usually it's Self for simple types or *const c_void
/// for complex ones
#[doc(hidden)]
@@ -63,6 +63,56 @@ pub trait OpenCVTypeExternContainer {
fn opencv_into_extern(self) -> Self::ExternSendMut;
}
+/// Extern type to receive the OpenCVType over FFI boundary, used to improve readability
+#[macro_export]
+macro_rules! extern_receive {
+ ($typ: ty) => {
+ extern_receive!($typ: '_)
+ };
+ ($typ: ty: $lt: lifetime) => {
+ <$typ as $crate::traits::OpenCVType<$lt>>::ExternReceive
+ };
+}
+
+/// Extern type to send the OpenCVTypeExternContainer over FFI boundary, used to improve readability
+#[macro_export]
+macro_rules! extern_send {
+ (mut $typ: ty: $lt: lifetime) => {
+ <$typ as OpenCVTypeExternContainer<$lt>>::ExternSendMut
+ };
+ ($typ: ty: $lt: lifetime) => {
+ <$typ as OpenCVTypeExternContainer<$lt>>::ExternSend
+ };
+ (mut $typ: ty) => {
+ extern_send!(mut $typ: '_)
+ };
+ ($typ: ty) => {
+ extern_send!($typ: '_)
+ };
+}
+
+/// Extern type to send the owned OpenCVType over FFI boundary, used to improve readability
+#[macro_export]
+macro_rules! extern_container_send {
+ (mut $typ: ty: $lt: lifetime) => {
+ extern_send!(mut <$typ as OpenCVTypeArg<$lt>>::ExternContainer: $lt)
+ };
+ ($typ: ty: $lt: lifetime) => {
+ extern_send!(<$typ as OpenCVTypeArg<$lt>>::ExternContainer: $lt)
+ };
+}
+
+/// Extern type to send the ::Arg of a OpenCVType over FFI boundary, used to improve readability
+#[macro_export]
+macro_rules! extern_arg_send {
+ (mut $typ: ty: $lt: lifetime) => {
+ extern_container_send!(mut <$typ as OpenCVType<$lt>>::Arg: $lt)
+ };
+ ($typ: ty: $lt: lifetime) => {
+ extern_container_send!(<$typ as OpenCVType<$lt>>::Arg: $lt)
+ };
+}
+
#[macro_export]
macro_rules! opencv_type_copy {
($($type: ty),+ $(,)?) => {
@@ -80,7 +130,7 @@ macro_rules! opencv_type_copy {
#[inline] fn opencv_into_extern_container_nofail(self) -> Self { self }
}
- impl $crate::traits::OpenCVTypeExternContainer for $type {
+ impl $crate::traits::OpenCVTypeExternContainer<'_> for $type {
type ExternSend = Self;
type ExternSendMut = Self;
@@ -121,7 +171,7 @@ macro_rules! opencv_type_simple {
}
}
- impl $crate::traits::OpenCVTypeExternContainer for $type {
+ impl $crate::traits::OpenCVTypeExternContainer<'_> for $type {
type ExternSend = *const Self;
type ExternSendMut = *mut Self;
@@ -157,7 +207,7 @@ macro_rules! opencv_type_simple_generic {
#[inline] fn opencv_into_extern_container_nofail(self) -> Self { self }
}
- impl<T: $trait$( + $more)*> $crate::traits::OpenCVTypeExternContainer for $type<T> {
+ impl<T: $trait$( + $more)*> $crate::traits::OpenCVTypeExternContainer<'_> for $type<T> {
type ExternSend = *const Self;
type ExternSendMut = *mut Self;
@@ -218,7 +268,7 @@ impl OpenCVTypeArg<'_> for &str {
}
}
-impl OpenCVTypeExternContainer for CString {
+impl OpenCVTypeExternContainer<'_> for CString {
type ExternSend = *const c_char;
type ExternSendMut = *mut c_char;
@@ -257,7 +307,7 @@ impl OpenCVTypeArg<'_> for Vec<u8> {
}
}
-impl OpenCVTypeExternContainer for Vec<u8> {
+impl OpenCVTypeExternContainer<'_> for Vec<u8> {
type ExternSend = *const u8;
type ExternSendMut = *mut u8;

Improve auxiliary types generation