Split out OpenCVTypeArg from OpenCVType
MODIFY
src/lib.rs
src/lib.rs
@@ -30,7 +30,7 @@ pub(crate) mod mod_prelude_types {
pub use crate::{
platform_types::*,
- traits::{Boxed, OpenCVType, OpenCVTypeExternContainer},
+ traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
};
}

MODIFY
src/manual/core.rs
src/manual/core.rs
@@ -32,7 +32,6 @@ macro_rules! valid_types {
macro_rules! opencv_type_simple_generic {
($type: ident<$trait: ident>) => {
impl<T: $trait> $crate::traits::OpenCVType<'_> for $type<T> {
- type Owned = Self;
type Arg = Self;
type ExternReceive = Self;
type ExternContainer = Self;
@@ -42,6 +41,13 @@ macro_rules! opencv_type_simple_generic {
#[inline] unsafe fn opencv_from_extern(s: Self) -> Self { s }
}
+ impl<T: $trait> $crate::traits::OpenCVTypeArg<'_> for $type<T> {
+ type ExternContainer = Self;
+
+ #[inline] fn opencv_into_extern_container(self) -> $crate::Result<Self> { Ok(self) }
+ #[inline] fn opencv_into_extern_container_nofail(self) -> Self { self }
+ }
+
impl<T: $trait> $crate::traits::OpenCVTypeExternContainer for $type<T> {
type ExternSend = *const Self;
type ExternSendMut = *mut Self;

MODIFY
src/manual/core/mat/mat_.rs
src/manual/core/mat/mat_.rs
@@ -9,7 +9,7 @@ use crate::{
core::{Mat, MatTrait},
Error,
Result,
- traits::{Boxed, OpenCVType, OpenCVTypeExternContainer},
+ traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
};
use super::{DataType, match_dims, match_format, match_is_continuous, match_total, MatTraitManual};
@@ -115,7 +115,6 @@ impl<T> Boxed for Mat_<T> {
}
impl<T> OpenCVType<'_> for Mat_<T> {
- type Owned = Self;
type Arg = Self;
type ExternReceive = *mut c_void;
type ExternContainer = Self;
@@ -131,11 +130,25 @@ impl<T> OpenCVType<'_> for Mat_<T> {
}
#[inline]
- unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self::Owned {
+ unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
Self::from_raw(s)
}
}
+impl<T> OpenCVTypeArg<'_> for Mat_<T> {
+ type ExternContainer = Self;
+
+ #[inline]
+ fn opencv_into_extern_container(self) -> Result<Self::ExternContainer> {
+ Ok(self)
+ }
+
+ #[inline]
+ fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
+ self
+ }
+}
+
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
@@ -11,7 +11,7 @@ use crate::{
manual::core::sized::*,
Result,
sys::Result as SysResult,
- traits::{Boxed, OpenCVType, OpenCVTypeExternContainer},
+ traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
};
fn index_check(idx: (usize, usize), rows: usize, cols: usize) -> Result<()> {
@@ -144,7 +144,6 @@ impl<T: ValidMatxType, A: SizedArray<T>> std::ops::IndexMut<(usize, usize)> for
}
impl<T: ValidMatxType, A: SizedArray<T>> OpenCVType<'_> for Matx<T, A> {
- type Owned = Self;
type Arg = Self;
type ExternReceive = Self;
type ExternContainer = Self;
@@ -154,6 +153,15 @@ impl<T: ValidMatxType, A: SizedArray<T>> OpenCVType<'_> for Matx<T, A> {
#[inline] unsafe fn opencv_from_extern(s: Self) -> Self { s }
}
+impl<T: ValidMatxType, A: SizedArray<T>> OpenCVTypeArg<'_> for Matx<T, A> {
+ type ExternContainer = Self;
+
+ #[inline]
+ fn opencv_into_extern_container(self) -> Result<Self> { Ok(self) }
+ #[inline]
+ fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer { self }
+}
+
impl<T: ValidMatxType, A: SizedArray<T>> OpenCVTypeExternContainer for Matx<T, A> {
type ExternSend = *const Self;
type ExternSendMut = *mut Self;

MODIFY
src/manual/core/ptr.rs
src/manual/core/ptr.rs
@@ -8,7 +8,7 @@ pub use ptr_extern::PtrExtern;
use crate::{
Result,
- traits::{Boxed, OpenCVType, OpenCVTypeExternContainer},
+ traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
};
#[cfg(not(feature = "opencv-32"))]
@@ -57,7 +57,6 @@ impl<T: ?Sized> Boxed for Ptr<T> where Self: PtrExtern {
}
impl<T: ?Sized> OpenCVType<'_> for Ptr<T> where Self: PtrExtern {
- type Owned = Self;
type Arg = Self;
type ExternReceive = *mut c_void;
type ExternContainer = Self;
@@ -73,11 +72,25 @@ impl<T: ?Sized> OpenCVType<'_> for Ptr<T> where Self: PtrExtern {
}
#[inline]
- unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self::Owned {
+ unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
Self::from_raw(s)
}
}
+impl<T: ?Sized> OpenCVTypeArg<'_> for Ptr<T> where Self: PtrExtern {
+ type ExternContainer = Self;
+
+ #[inline]
+ fn opencv_into_extern_container(self) -> Result<Self::ExternContainer> {
+ Ok(self)
+ }
+
+ #[inline]
+ fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
+ self
+ }
+}
+
impl<T: ?Sized> OpenCVTypeExternContainer for Ptr<T> where Self: PtrExtern {
type ExternSend = *const c_void;
type ExternSendMut = *mut c_void;

MODIFY
src/manual/core/vector.rs
src/manual/core/vector.rs
@@ -13,7 +13,7 @@ pub use vector_extern::{VectorElement, VectorExtern, VectorExternCopyNonBool};
use crate::{
platform_types::size_t,
Result,
- traits::{Boxed, OpenCVType, OpenCVTypeExternContainer},
+ traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
};
mod vector_extern;
@@ -239,7 +239,6 @@ impl<T: VectorElement> Boxed for Vector<T> where Self: VectorExtern<T> {
}
impl<T: VectorElement> OpenCVType<'_> for Vector<T> where Self: VectorExtern<T> {
- type Owned = Self;
type Arg = Self;
type ExternReceive = *mut c_void;
type ExternContainer = Self;
@@ -255,11 +254,25 @@ impl<T: VectorElement> OpenCVType<'_> for Vector<T> where Self: VectorExtern<T>
}
#[inline]
- unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self::Owned {
+ unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
Self::from_raw(s)
}
}
+impl<T: VectorElement> OpenCVTypeArg<'_> for Vector<T> where Self: VectorExtern<T> {
+ type ExternContainer = Self;
+
+ #[inline]
+ fn opencv_into_extern_container(self) -> Result<Self::ExternContainer> {
+ Ok(self)
+ }
+
+ #[inline]
+ fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
+ self
+ }
+}
+
impl<T: VectorElement> OpenCVTypeExternContainer for Vector<T> where Self: VectorExtern<T> {
type ExternSend = *const c_void;
type ExternSendMut = *mut c_void;

MODIFY
src/manual/core/vector/vector_extern.rs
src/manual/core/vector/vector_extern.rs
@@ -4,7 +4,7 @@ use crate::{
core::Vector,
platform_types::size_t,
Result,
- traits::{OpenCVType, OpenCVTypeExternContainer},
+ traits::{OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
};
#[doc(hidden)]
@@ -26,9 +26,9 @@ pub trait VectorExtern<T: for<'a> OpenCVType<'a>> {
#[doc(hidden)] unsafe fn extern_swap(&mut self, index1: size_t, index2: size_t);
#[doc(hidden)] unsafe fn extern_clear(&mut self);
#[doc(hidden)] unsafe fn extern_get(&self, index: size_t) -> Result<T>;
- #[doc(hidden)] unsafe fn extern_push<'a>(&mut self, val: <<<T as OpenCVType<'a>>::Arg as OpenCVType<'a>>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
- #[doc(hidden)] unsafe fn extern_insert<'a>(&mut self, index: size_t, val: <<<T as OpenCVType<'a>>::Arg as OpenCVType<'a>>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
- #[doc(hidden)] unsafe fn extern_set<'a>(&mut self, index: size_t, val: <<<T as OpenCVType<'a>>::Arg as OpenCVType<'a>>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
+ #[doc(hidden)] unsafe fn extern_push<'a>(&mut self, val: <<<T as OpenCVType<'a>>::Arg as OpenCVTypeArg<'a>>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
+ #[doc(hidden)] unsafe fn extern_insert<'a>(&mut self, index: size_t, val: <<<T as OpenCVType<'a>>::Arg as OpenCVTypeArg<'a>>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
+ #[doc(hidden)] unsafe fn extern_set<'a>(&mut self, index: size_t, val: <<<T as OpenCVType<'a>>::Arg as OpenCVTypeArg<'a>>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
}
#[macro_export]
@@ -129,20 +129,20 @@ macro_rules! vector_extern {
}
#[inline(always)]
- unsafe fn extern_push<'a>(&mut self, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVType<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend) {
- extern "C" { fn $extern_push<'a>(instance: $vector_extern_mut, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVType<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend); }
+ unsafe fn extern_push<'a>(&mut self, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVTypeArg<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend) {
+ extern "C" { fn $extern_push<'a>(instance: $vector_extern_mut, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVTypeArg<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend); }
$extern_push(self.as_raw_mut(), val)
}
#[inline(always)]
- unsafe fn extern_insert<'a>(&mut self, index: $crate::platform_types::size_t, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVType<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend) {
- extern "C" { fn $extern_insert<'a>(instance: $vector_extern_mut, index: $crate::platform_types::size_t, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVType<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend); }
+ unsafe fn extern_insert<'a>(&mut self, index: $crate::platform_types::size_t, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVTypeArg<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend) {
+ extern "C" { fn $extern_insert<'a>(instance: $vector_extern_mut, index: $crate::platform_types::size_t, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVTypeArg<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend); }
$extern_insert(self.as_raw_mut(), index, val)
}
#[inline(always)]
- unsafe fn extern_set<'a>(&mut self, index: $crate::platform_types::size_t, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVType<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend) {
- extern "C" { fn $extern_set<'a>(instance: $vector_extern_mut, index: $crate::platform_types::size_t, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVType<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend); }
+ unsafe fn extern_set<'a>(&mut self, index: $crate::platform_types::size_t, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVTypeArg<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend) {
+ extern "C" { fn $extern_set<'a>(instance: $vector_extern_mut, index: $crate::platform_types::size_t, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVTypeArg<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend); }
$extern_set(self.as_raw_mut(), index, val)
}
}

MODIFY
src/manual/types/abstract_ref.rs
src/manual/types/abstract_ref.rs
@@ -5,7 +5,7 @@ use std::{
use crate::{
Result,
- traits::{Boxed, OpenCVType, OpenCVTypeExternContainer},
+ traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
};
pub struct AbstractRefMut<'r, T: ?Sized> {
@@ -36,7 +36,6 @@ impl<T: ?Sized> Boxed for AbstractRefMut<'_, T> {
}
impl<'r, T: ?Sized> OpenCVType<'r> for AbstractRefMut<'r, T> {
- type Owned = Self;
type Arg = Self;
type ExternReceive = *mut c_void;
type ExternContainer = Self;
@@ -52,11 +51,25 @@ impl<'r, T: ?Sized> OpenCVType<'r> for AbstractRefMut<'r, T> {
}
#[inline]
- unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self::Owned {
+ unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
Self::from_raw(s)
}
}
+impl<'r, T: ?Sized> OpenCVTypeArg<'r> for AbstractRefMut<'r, T> {
+ type ExternContainer = Self;
+
+ #[inline]
+ fn opencv_into_extern_container(self) -> Result<Self::ExternContainer> {
+ Ok(self)
+ }
+
+ #[inline]
+ fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
+ self
+ }
+}
+
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
@@ -49,7 +49,6 @@ macro_rules! opencv_type_boxed {
}
impl $crate::traits::OpenCVType<'_> for $type {
- type Owned = Self;
type Arg = Self;
type ExternReceive = *mut std::ffi::c_void;
type ExternContainer = Self;
@@ -65,11 +64,25 @@ macro_rules! opencv_type_boxed {
}
#[inline]
- unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self::Owned {
+ unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
Self::from_raw(s)
}
}
+ impl $crate::traits::OpenCVTypeArg<'_> for $type {
+ type ExternContainer = Self;
+
+ #[inline]
+ fn opencv_into_extern_container(self) -> $crate::Result<Self::ExternContainer> {
+ Ok(self)
+ }
+
+ #[inline]
+ fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
+ self
+ }
+ }
+
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
@@ -12,12 +12,8 @@ use crate::Result;
/// pass &str as argument to functions that await String and do necessary conversion through CString.
pub trait OpenCVType<'a>: Sized {
#[doc(hidden)]
- /// Owned version of the type, e.g. String for &str, for most other types it's Self
- type Owned: OpenCVType<'a>;
- #[doc(hidden)]
- /// Type when passed as argument to function, e.g. &str for String, for most other types it's either Self
- /// or &Self
- type Arg: OpenCVType<'a>;
+ /// Type when passed as argument to function, e.g. &str for String, for most other types it's Self
+ type Arg: OpenCVTypeArg<'a>;
#[doc(hidden)]
/// Return type when this type is returned over the FFI boundary from the C++ function, Self for simple
/// types, *mut c_void for complex ones
@@ -34,13 +30,28 @@ pub trait OpenCVType<'a>: Sized {
/// Convert Self into external container in the nofail context, this can panic
fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer;
#[doc(hidden)]
- /// Construct the new Self::Owned from the data received from C++ function
- unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self::Owned;
+ /// Construct the new Self from the data received from C++ function
+ unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self;
+}
+
+#[doc(hidden)]
+pub trait OpenCVTypeArg<'a>: Sized {
+ #[doc(hidden)]
+ /// Container to help marshall type over FFI boundary, e.g. CString for String or &str, for most other
+ /// types it's Self
+ type ExternContainer: OpenCVTypeExternContainer;
+
+ #[doc(hidden)]
+ /// Convert Self into external container with possible error result, it shouldn't panic
+ fn opencv_into_extern_container(self) -> Result<Self::ExternContainer>;
+ #[doc(hidden)]
+ /// Convert Self into external container in the nofail context, this can panic
+ fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer;
}
#[doc(hidden)]
/// Common trait for the type that is used to help marshall OpenCV related type over the FFI boundary
-pub trait OpenCVTypeExternContainer: Sized {
+pub trait OpenCVTypeExternContainer {
#[doc(hidden)]
/// Type when constant Self is sent to C++ function, usually it's Self for simple types or *const c_void
/// for complex ones
@@ -61,7 +72,6 @@ macro_rules! opencv_type_copy {
($($type: ty),+ $(,)?) => {
$(
impl $crate::traits::OpenCVType<'_> for $type {
- type Owned = Self;
type Arg = Self;
type ExternReceive = Self;
type ExternContainer = Self;
@@ -71,6 +81,13 @@ macro_rules! opencv_type_copy {
#[inline] unsafe fn opencv_from_extern(s: Self) -> Self { s }
}
+ impl $crate::traits::OpenCVTypeArg<'_> for $type {
+ type ExternContainer = Self;
+
+ #[inline] fn opencv_into_extern_container(self) -> $crate::Result<Self> { Ok(self) }
+ #[inline] fn opencv_into_extern_container_nofail(self) -> Self { self }
+ }
+
impl $crate::traits::OpenCVTypeExternContainer for $type {
type ExternSend = Self;
type ExternSendMut = Self;
@@ -93,7 +110,6 @@ macro_rules! opencv_type_enum {
macro_rules! opencv_type_simple {
($type: ty) => {
impl $crate::traits::OpenCVType<'_> for $type {
- type Owned = Self;
type Arg = Self;
type ExternReceive = Self;
type ExternContainer = Self;
@@ -103,6 +119,13 @@ macro_rules! opencv_type_simple {
#[inline] unsafe fn opencv_from_extern(s: Self) -> Self { s }
}
+ impl $crate::traits::OpenCVTypeArg<'_> for $type {
+ type ExternContainer = Self;
+
+ #[inline] fn opencv_into_extern_container(self) -> $crate::Result<Self> { Ok(self) }
+ #[inline] fn opencv_into_extern_container_nofail(self) -> Self { self }
+ }
+
impl $crate::traits::OpenCVTypeExternContainer for $type {
type ExternSend = *const Self;
type ExternSendMut = *mut Self;
@@ -128,7 +151,6 @@ pub fn cstring_new_nofail(bytes: impl Into<Vec<u8>>) -> CString {
}
impl<'a> OpenCVType<'a> for String {
- type Owned = Self;
type Arg = &'a str;
type ExternReceive = *mut c_void;
type ExternContainer = CString;
@@ -144,15 +166,12 @@ impl<'a> OpenCVType<'a> for String {
}
#[inline]
- unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self::Owned {
+ unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
crate::templ::receive_string(s as *mut String)
}
}
-impl OpenCVType<'_> for &str {
- type Owned = String;
- type Arg = Self;
- type ExternReceive = *mut c_void;
+impl OpenCVTypeArg<'_> for &str {
type ExternContainer = CString;
#[inline]
@@ -164,11 +183,6 @@ impl OpenCVType<'_> for &str {
fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
cstring_new_nofail(self)
}
-
- #[inline]
- unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self::Owned {
- crate::templ::receive_string(s as *mut String)
- }
}
impl OpenCVTypeExternContainer for CString {

Split out OpenCVTypeArg from OpenCVType