Simplify OpenCVType impls by removing duplications between it and OpenCVTypeArg
MODIFY
src/manual/core/mat/mat_.rs
src/manual/core/mat/mat_.rs
@@ -152,12 +152,6 @@ impl<T> ToInputOutputArray for Mat_<T> {
impl<T> OpenCVType<'_> for Mat_<T> {
type Arg = Self;
type ExternReceive = *mut c_void;
- type ExternContainer = Self;
-
- #[inline]
- fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
- self
- }
#[inline]
unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {

MODIFY
src/manual/core/matx.rs
src/manual/core/matx.rs
@@ -180,12 +180,7 @@ impl<T, A: SizedArray<T>> std::ops::IndexMut<(usize, usize)> for Matx<T, A> {
impl<T, A: SizedArray<T>> OpenCVType<'_> for Matx<T, A> {
type Arg = Self;
type ExternReceive = Self;
- type ExternContainer = Self;
- #[inline]
- fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
- self
- }
#[inline]
unsafe fn opencv_from_extern(s: Self) -> Self {
s

MODIFY
src/manual/core/ptr.rs
src/manual/core/ptr.rs
@@ -72,12 +72,6 @@ where
{
type Arg = Self;
type ExternReceive = *mut c_void;
- type ExternContainer = Self;
-
- #[inline]
- fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
- self
- }
#[inline]
unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {

MODIFY
src/manual/core/ptr/ptr_extern.rs
src/manual/core/ptr/ptr_extern.rs
use std::ffi::c_void;
-use crate::traits::{OpenCVType, OpenCVTypeExternContainer};
+use crate::traits::{OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
#[doc(hidden)]
pub trait PtrExtern {
@@ -15,7 +15,7 @@ pub trait PtrExtern {
#[doc(hidden)]
pub trait PtrExternCtor<T: for<'a> OpenCVType<'a>>: Sized {
#[doc(hidden)]
- unsafe fn extern_new(val: <<T as OpenCVType>::ExternContainer as OpenCVTypeExternContainer>::ExternSendMut) -> *mut c_void;
+ unsafe fn extern_new(val: <<T as OpenCVTypeArg>::ExternContainer as OpenCVTypeExternContainer>::ExternSendMut) -> *mut c_void;
}
#[macro_export]
@@ -51,14 +51,14 @@ macro_rules! ptr_extern_ctor {
($type: ty, $extern_new: ident $(,)?) => {
extern "C" {
fn $extern_new(
- val: <<$type as $crate::traits::OpenCVType>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSendMut,
+ val: <<$type as $crate::traits::OpenCVTypeArg>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSendMut,
) -> *mut std::ffi::c_void;
}
impl $crate::manual::core::PtrExternCtor<$type> for $crate::manual::core::Ptr<$type> {
#[inline]
unsafe fn extern_new(
- val: <<$type as $crate::traits::OpenCVType>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSendMut,
+ val: <<$type as $crate::traits::OpenCVTypeArg>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSendMut,
) -> *mut std::ffi::c_void {
$extern_new(val)
}

MODIFY
src/manual/core/vec.rs
src/manual/core/vec.rs
@@ -113,12 +113,6 @@ impl<F: Float> VecN<F, 4> {
impl<T, const N: usize> OpenCVType<'_> for VecN<T, N> {
type Arg = Self;
type ExternReceive = Self;
- type ExternContainer = Self;
-
- #[inline]
- fn opencv_into_extern_container_nofail(self) -> Self {
- self
- }
#[inline]
unsafe fn opencv_from_extern(s: Self) -> Self {

MODIFY
src/manual/core/vector.rs
src/manual/core/vector.rs
@@ -344,12 +344,6 @@ where
{
type Arg = Self;
type ExternReceive = *mut c_void;
- type ExternContainer = Self;
-
- #[inline]
- fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
- self
- }
#[inline]
unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {

MODIFY
src/manual/core/vector/vector_extern.rs
src/manual/core/vector/vector_extern.rs
@@ -48,7 +48,7 @@ pub trait VectorExtern<T: for<'a> OpenCVType<'a>> {
val: <<<T as OpenCVType<'a>>::Arg as OpenCVTypeArg<'a>>::ExternContainer as OpenCVTypeExternContainer>::ExternSend,
);
#[doc(hidden)]
- unsafe fn extern_push_owned(&mut self, val: <<T as OpenCVType>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
+ unsafe fn extern_push_owned(&mut self, val: <<T as OpenCVTypeArg>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
#[doc(hidden)]
unsafe fn extern_insert<'a>(
&mut self,
@@ -166,7 +166,7 @@ macro_rules! vector_extern {
}
#[inline]
- unsafe fn extern_push_owned(&mut self, val: <<$type as $crate::traits::OpenCVType>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend) {
+ unsafe fn extern_push_owned(&mut self, val: <<$type as $crate::traits::OpenCVTypeArg>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend) {
$extern_push(self.as_raw_mut(), val)
}

MODIFY
src/manual/types/abstract_ref.rs
src/manual/types/abstract_ref.rs
@@ -32,12 +32,6 @@ impl<T: ?Sized> Boxed for AbstractRefMut<'_, T> {
impl<'r, T: ?Sized> OpenCVType<'r> for AbstractRefMut<'r, T> {
type Arg = Self;
type ExternReceive = *mut c_void;
- type ExternContainer = Self;
-
- #[inline]
- fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
- self
- }
#[inline]
unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {

MODIFY
src/traits/boxed.rs
src/traits/boxed.rs
@@ -53,17 +53,6 @@ macro_rules! opencv_type_boxed {
impl $crate::traits::OpenCVType<'_> for $type {
type Arg = Self;
type ExternReceive = *mut ::std::ffi::c_void;
- 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
- }
#[inline]
unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
@@ -74,11 +63,6 @@ macro_rules! opencv_type_boxed {
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

MODIFY
src/traits/opencv_type.rs
src/traits/opencv_type.rs
-use std::{
- ffi::{c_void, CString},
- os::raw::c_char,
-};
+use std::ffi::{c_void, CString};
+use std::os::raw::c_char;
use crate::Result;
@@ -10,7 +8,7 @@ use crate::Result;
/// This trait is somewhat unnecessary complex because of the need of handling String, we need to be able to
/// pass &str as argument to functions that await String and do necessary conversion through CString.
#[doc(hidden)]
-pub trait OpenCVType<'a>: Sized {
+pub trait OpenCVType<'a>: Sized + OpenCVTypeArg<'a> {
/// Type when passed as argument to function, e.g. &str for String, for most other types it's Self
#[doc(hidden)]
type Arg: OpenCVTypeArg<'a>;
@@ -18,25 +16,15 @@ pub trait OpenCVType<'a>: Sized {
/// types, *mut c_void for complex ones
#[doc(hidden)]
type ExternReceive;
- /// 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;
- /// Convert Self into external container with possible error result, it shouldn't panic
- #[doc(hidden)]
- #[inline]
- fn opencv_into_extern_container(self) -> Result<Self::ExternContainer> {
- Ok(self.opencv_into_extern_container_nofail())
- }
- /// Convert Self into external container in the nofail context, this can panic
- #[doc(hidden)]
- fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer;
/// Construct the new Self from the data received from C++ function
#[doc(hidden)]
unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self;
}
+/// Common trait for types that can be used as argument that will be converted into a OpenCV type.
+///
+/// Mostly necessary to be able to pass `&str` argument for types that otherwise have `String` Rust representation.
#[doc(hidden)]
pub trait OpenCVTypeArg<'a>: Sized {
/// Container to help marshall type over FFI boundary, e.g. CString for String or &str, for most other
@@ -82,17 +70,13 @@ macro_rules! opencv_type_copy {
impl $crate::traits::OpenCVType<'_> for $type {
type Arg = Self;
type ExternReceive = Self;
- 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 }
#[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 }
}
@@ -121,16 +105,7 @@ macro_rules! opencv_type_simple {
impl $crate::traits::OpenCVType<'_> for $type {
type Arg = Self;
type ExternReceive = Self;
- 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
- }
#[inline]
unsafe fn opencv_from_extern(s: Self) -> Self {
s
@@ -140,10 +115,6 @@ macro_rules! opencv_type_simple {
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
@@ -176,17 +147,13 @@ macro_rules! opencv_type_simple_generic {
impl<T: $trait$( + $more)*> $crate::traits::OpenCVType<'_> for $type<T> {
type Arg = Self;
type ExternReceive = Self;
- 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 }
#[inline] unsafe fn opencv_from_extern(s: Self) -> Self { s }
}
impl<T: $trait$( + $more)*> $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 }
}
@@ -216,6 +183,14 @@ pub fn cstring_new_nofail(bytes: impl Into<Vec<u8>>) -> CString {
impl<'a> OpenCVType<'a> for String {
type Arg = &'a str;
type ExternReceive = *mut c_void;
+
+ #[inline]
+ unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
+ crate::templ::receive_string(s as *mut String)
+ }
+}
+
+impl OpenCVTypeArg<'_> for String {
type ExternContainer = CString;
#[inline]
@@ -227,11 +202,6 @@ impl<'a> OpenCVType<'a> for String {
fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
cstring_new_nofail(self)
}
-
- #[inline]
- unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
- crate::templ::receive_string(s as *mut String)
- }
}
impl OpenCVTypeArg<'_> for &str {
@@ -268,20 +238,9 @@ impl OpenCVTypeExternContainer for CString {
}
}
-impl<'a> OpenCVType<'a> for Vec<u8> {
+impl OpenCVType<'_> for Vec<u8> {
type Arg = Self;
type ExternReceive = *mut c_void;
- 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
- }
#[inline]
unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {

Simplify OpenCVType impls by removing duplications between it and OpenCVTypeArg