Move opencv_into_extern into a separate trait, remove unused trait lifetimes
MODIFY
src/manual/core/mat/mat_.rs
src/manual/core/mat/mat_.rs
@@ -7,7 +7,7 @@ use crate::core::{
Mat, MatTrait, MatTraitConst, MatTraitConstManual, MatTraitManual, ToInputArray, ToInputOutputArray, ToOutputArray,
_InputArray, _InputOutputArray, _OutputArray,
};
-use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
+use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer, OpenCVTypeExternContainerMove};
use crate::{Error, Result};
use super::{match_dims, match_format, match_is_continuous, match_total, DataType};
@@ -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;
@@ -181,7 +181,9 @@ impl<T> OpenCVTypeExternContainer<'_> for Mat_<T> {
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.as_raw_mut()
}
+}
+impl<T> OpenCVTypeExternContainerMove for Mat_<T> {
#[inline]
fn opencv_into_extern(self) -> Self::ExternSendMut {
self.into_raw()

MODIFY
src/manual/core/matx.rs
src/manual/core/matx.rs
use std::fmt;
-use std::mem::ManuallyDrop;
use num_traits::{One, Zero};
@@ -194,7 +193,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;
@@ -206,10 +205,6 @@ impl<T, A: SizedArray<T>> OpenCVTypeExternContainer<'_> for Matx<T, A> {
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self
}
- #[inline]
- fn opencv_into_extern(self) -> Self::ExternSendMut {
- &mut *ManuallyDrop::new(self) as _
- }
}
impl<T: PartialEq, A: SizedArray<T>> PartialEq for Matx<T, A> {

MODIFY
src/manual/core/ptr.rs
src/manual/core/ptr.rs
@@ -2,7 +2,7 @@ use std::{ffi::c_void, marker::PhantomData, mem::ManuallyDrop};
pub use ptr_extern::{PtrExtern, PtrExternCtor};
-use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
+use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer, OpenCVTypeExternContainerMove};
mod ptr_extern;
mod ptr_f32;
@@ -27,10 +27,9 @@ where
/// Create a new `Ptr` from the object
pub fn new(val: T) -> Self
where
- T: for<'a> OpenCVTypeArg<'a>,
+ T: OpenCVTypeExternContainerMove + Sized,
Self: PtrExternCtor<T>,
{
- let val = val.opencv_into_extern_container_nofail();
unsafe { Self::from_raw(Self::extern_new(val.opencv_into_extern())) }
}
@@ -95,7 +94,7 @@ where
}
}
-impl<T: ?Sized> OpenCVTypeExternContainer<'_> for Ptr<T>
+impl<T: ?Sized> OpenCVTypeExternContainer for Ptr<T>
where
Self: PtrExtern,
{
@@ -111,7 +110,13 @@ where
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.as_raw_mut()
}
+}
+impl<T: ?Sized> OpenCVTypeExternContainerMove for Ptr<T>
+where
+ Self: PtrExtern,
+{
+ #[inline]
fn opencv_into_extern(self) -> Self::ExternSendMut {
self.into_raw()
}

MODIFY
src/manual/core/ptr/ptr_extern.rs
src/manual/core/ptr/ptr_extern.rs
use std::ffi::c_void;
-use crate::traits::{OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
-use crate::{extern_container_send, extern_receive, extern_send};
+use crate::traits::{OpenCVType, OpenCVTypeExternContainerMove};
+use crate::{extern_receive, extern_send};
#[doc(hidden)]
pub trait PtrExtern {
@@ -14,9 +14,9 @@ pub trait PtrExtern {
}
#[doc(hidden)]
-pub trait PtrExternCtor<T: for<'a> OpenCVTypeArg<'a>> {
+pub trait PtrExternCtor<T: OpenCVTypeExternContainerMove> {
#[doc(hidden)]
- unsafe fn extern_new<'a>(val: extern_container_send!(mut T: 'a)) -> extern_receive!(Self: 'a)
+ unsafe fn extern_new<'a>(val: extern_send!(mut T)) -> extern_receive!(Self: 'a)
where
Self: OpenCVType<'a>;
}

MODIFY
src/manual/core/tuple.rs
src/manual/core/tuple.rs
-use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
+use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer, OpenCVTypeExternContainerMove};
use std::ffi::c_void;
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
@@ -61,7 +61,7 @@ where
}
}
-impl<T> OpenCVTypeExternContainer<'_> for Tuple<T>
+impl<T> OpenCVTypeExternContainer for Tuple<T>
where
Tuple<T>: TupleExtern,
{
@@ -77,7 +77,12 @@ where
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.as_raw_mut()
}
+}
+impl<T> OpenCVTypeExternContainerMove for Tuple<T>
+where
+ Tuple<T>: TupleExtern,
+{
#[inline]
fn opencv_into_extern(self) -> Self::ExternSendMut {
self.into_raw()

MODIFY
src/manual/core/vec.rs
src/manual/core/vec.rs
@@ -123,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;
@@ -136,11 +136,6 @@ impl<T, const N: usize> OpenCVTypeExternContainer<'_> for VecN<T, N> {
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self
}
-
- #[inline]
- fn opencv_into_extern(self) -> Self::ExternSendMut {
- &mut *std::mem::ManuallyDrop::new(self) as _
- }
}
impl<T, const N: usize> ToInputArray for VecN<T, N>

MODIFY
src/manual/core/vector.rs
src/manual/core/vector.rs
-use std::{borrow::Borrow, ffi::c_void, fmt, iter::FromIterator, marker::PhantomData, mem::ManuallyDrop, slice};
+use std::borrow::Borrow;
+use std::ffi::c_void;
+use std::iter::FromIterator;
+use std::marker::PhantomData;
+use std::mem::ManuallyDrop;
+use std::{fmt, slice};
pub use iter::{VectorIterator, VectorRefIterator};
pub use vector_extern::{VectorElement, VectorExtern, VectorExternCopyNonBool};
-use crate::{
- platform_types::size_t,
- traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
- Result,
-};
+use crate::platform_types::size_t;
+use crate::traits::OpenCVTypeExternContainerMove;
+use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
+use crate::Result;
mod iter;
mod vector_extern;
@@ -363,7 +367,7 @@ where
}
}
-impl<T: VectorElement> OpenCVTypeExternContainer<'_> for Vector<T>
+impl<T: VectorElement> OpenCVTypeExternContainer for Vector<T>
where
Self: VectorExtern<T>,
{
@@ -379,7 +383,12 @@ where
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.as_raw_mut()
}
+}
+impl<T: VectorElement> OpenCVTypeExternContainerMove for Vector<T>
+where
+ Self: VectorExtern<T>,
+{
#[inline]
fn opencv_into_extern(self) -> Self::ExternSendMut {
self.into_raw()

MODIFY
src/manual/core/vector/vector_extern.rs
src/manual/core/vector/vector_extern.rs
use crate::core::Vector;
use crate::platform_types::size_t;
-use crate::traits::{OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
+use crate::traits::OpenCVType;
use crate::{extern_arg_send, extern_container_send, extern_receive, extern_send};
/// This trait is implemented by any type that can be stored inside `Vector`.
@@ -44,7 +44,7 @@ pub trait VectorExtern<T: for<'a> OpenCVType<'a>> {
#[doc(hidden)]
unsafe fn extern_push<'a>(&mut self, val: extern_arg_send!(T: 'a));
#[doc(hidden)]
- unsafe fn extern_push_owned<'a>(&mut self, val: extern_container_send!(T: 'a));
+ unsafe fn extern_push_owned(&mut self, val: extern_container_send!(T));
#[doc(hidden)]
unsafe fn extern_insert<'a>(&mut self, index: size_t, val: extern_arg_send!(T: 'a));
#[doc(hidden)]
@@ -152,7 +152,7 @@ macro_rules! vector_extern {
}
#[inline]
- unsafe fn extern_push_owned<'a>(&mut self, val: extern_container_send!($type: 'a)) {
+ unsafe fn extern_push_owned(&mut self, val: extern_container_send!($type)) {
$extern_push(self.as_raw_mut(), val)
}

MODIFY
src/manual/types/abstract_ref.rs
src/manual/types/abstract_ref.rs
-use std::{ffi::c_void, marker::PhantomData};
+use std::ffi::c_void;
+use std::marker::PhantomData;
-use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
+use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer, OpenCVTypeExternContainerMove};
pub struct AbstractRefMut<'r, T: ?Sized> {
ptr: *mut c_void,
@@ -48,7 +49,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;
@@ -61,7 +62,9 @@ impl<T: ?Sized> OpenCVTypeExternContainer<'_> for AbstractRefMut<'_, T> {
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.as_raw_mut()
}
+}
+impl<T: ?Sized> OpenCVTypeExternContainerMove for AbstractRefMut<'_, T> {
#[inline]
fn opencv_into_extern(self) -> Self::ExternSendMut {
self.into_raw()

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;
@@ -82,7 +82,9 @@ macro_rules! opencv_type_boxed {
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.as_raw_mut()
}
+ }
+ impl $crate::traits::OpenCVTypeExternContainerMove for $type {
#[inline]
fn opencv_into_extern(self) -> Self::ExternSendMut {
self.into_raw()

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<'a>;
+ type ExternContainer: OpenCVTypeExternContainer;
/// 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<'a> {
+pub trait OpenCVTypeExternContainer {
/// 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)]
@@ -59,6 +59,11 @@ pub trait OpenCVTypeExternContainer<'a> {
fn opencv_as_extern(&self) -> Self::ExternSend;
#[doc(hidden)]
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut;
+}
+
+/// Common trait for those `OpenCVTypeExternContainer`s that can be moved. Currently used for moving a boxed class behind a `Ptr`.
+#[doc(hidden)]
+pub trait OpenCVTypeExternContainerMove: OpenCVTypeExternContainer {
#[doc(hidden)]
fn opencv_into_extern(self) -> Self::ExternSendMut;
}
@@ -77,17 +82,11 @@ macro_rules! extern_receive {
/// 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 as $crate::traits::OpenCVTypeExternContainer>::ExternSendMut
};
($typ: ty) => {
- extern_send!($typ: '_)
+ <$typ as $crate::traits::OpenCVTypeExternContainer>::ExternSend
};
}
@@ -95,10 +94,16 @@ macro_rules! extern_send {
#[macro_export]
macro_rules! extern_container_send {
(mut $typ: ty: $lt: lifetime) => {
- extern_send!(mut <$typ as OpenCVTypeArg<$lt>>::ExternContainer: $lt)
+ extern_send!(mut <$typ as $crate::traits::OpenCVTypeArg<$lt>>::ExternContainer)
};
($typ: ty: $lt: lifetime) => {
- extern_send!(<$typ as OpenCVTypeArg<$lt>>::ExternContainer: $lt)
+ extern_send!(<$typ as $crate::traits::OpenCVTypeArg<$lt>>::ExternContainer)
+ };
+ (mut $typ: ty) => {
+ extern_container_send!(mut $typ: '_)
+ };
+ ($typ: ty) => {
+ extern_container_send!($typ: '_)
};
}
@@ -106,10 +111,10 @@ macro_rules! extern_container_send {
#[macro_export]
macro_rules! extern_arg_send {
(mut $typ: ty: $lt: lifetime) => {
- extern_container_send!(mut <$typ as OpenCVType<$lt>>::Arg: $lt)
+ extern_container_send!(mut <$typ as $crate::traits::OpenCVType<$lt>>::Arg: $lt)
};
($typ: ty: $lt: lifetime) => {
- extern_container_send!(<$typ as OpenCVType<$lt>>::Arg: $lt)
+ extern_container_send!(<$typ as $crate::traits::OpenCVType<$lt>>::Arg: $lt)
};
}
@@ -130,12 +135,15 @@ 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;
#[inline] fn opencv_as_extern(&self) -> Self { *self }
#[inline] fn opencv_as_extern_mut(&mut self) -> Self { *self }
+ }
+
+ impl $crate::traits::OpenCVTypeExternContainerMove for $type {
#[inline] fn opencv_into_extern(self) -> Self { self }
}
)+
@@ -156,22 +164,33 @@ macro_rules! opencv_type_simple {
type Arg = Self;
type ExternReceive = Self;
- #[inline] unsafe fn opencv_from_extern(s: Self) -> Self { s }
+ #[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_nofail(self) -> Self { self }
+ #[inline]
+ fn opencv_into_extern_container_nofail(self) -> Self {
+ self
+ }
}
- impl $crate::traits::OpenCVTypeExternContainer<'_> for $type {
+ impl $crate::traits::OpenCVTypeExternContainer for $type {
type ExternSend = *const Self;
type ExternSendMut = *mut Self;
- #[inline] fn opencv_as_extern(&self) -> Self::ExternSend { self }
- #[inline] fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut { self }
- #[inline] fn opencv_into_extern(self) -> Self::ExternSendMut { unimplemented!("This is intentionally left unimplemented as there seems to be no need for it and it's difficult to implement it without leakage") }
+ #[inline]
+ fn opencv_as_extern(&self) -> Self::ExternSend {
+ self
+ }
+ #[inline]
+ fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
+ self
+ }
}
};
}
@@ -192,13 +211,12 @@ 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;
#[inline] fn opencv_as_extern(&self) -> Self::ExternSend { self }
#[inline] fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut { self }
- #[inline] fn opencv_into_extern(self) -> Self::ExternSendMut { unimplemented!("This is intentionally left unimplemented as there seems to be no need for it and it's difficult to implement it without leakage") }
}
};
}
@@ -253,7 +271,7 @@ impl OpenCVTypeArg<'_> for &str {
}
}
-impl OpenCVTypeExternContainer<'_> for CString {
+impl OpenCVTypeExternContainer for CString {
type ExternSend = *const c_char;
type ExternSendMut = *mut c_char;
@@ -266,11 +284,6 @@ impl OpenCVTypeExternContainer<'_> for CString {
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.as_ptr() as _ // fixme: use as_mut_ptr() when it's stabilized or cast_mut() when MSRV is 1.65
}
-
- #[inline]
- fn opencv_into_extern(self) -> Self::ExternSendMut {
- unimplemented!("This is intentionally left unimplemented as there seems to be no need for it and it's difficult to implement it without leakage")
- }
}
impl OpenCVType<'_> for Vec<u8> {
@@ -292,7 +305,7 @@ impl OpenCVTypeArg<'_> for Vec<u8> {
}
}
-impl OpenCVTypeExternContainer<'_> for Vec<u8> {
+impl OpenCVTypeExternContainer for Vec<u8> {
type ExternSend = *const u8;
type ExternSendMut = *mut u8;
@@ -305,11 +318,6 @@ impl OpenCVTypeExternContainer<'_> for Vec<u8> {
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.as_mut_ptr()
}
-
- #[inline]
- fn opencv_into_extern(self) -> Self::ExternSendMut {
- unimplemented!("This is intentionally left unimplemented as there seems to be no need for it and it's difficult to implement it without leakage")
- }
}
opencv_type_copy! {

Move opencv_into_extern into a separate trait, remove unused trait lifetimes