Move remaining methods from VectorTrait to Vector
MODIFY
binding-generator/src/vector.rs
binding-generator/src/vector.rs
@@ -201,33 +201,13 @@ impl GeneratedElement for Vector<'_, '_> {
let vec_type = self.type_ref();
let element_type = self.element_type();
- let inner_rust_full_arg = if element_type.is_string() {
- "&'i str".into()
- } else {
- element_type.rust_full()
- };
- let mut pre_call = element_type.rust_arg_pre_call("val", false);
- if !pre_call.is_empty() {
- pre_call.push_str(";");
- }
- let mut pre_call_infallible = element_type.rust_arg_pre_call("val", true);
- if !pre_call_infallible.is_empty() {
- pre_call_infallible.push_str(";");
- }
let mut inter_vars = hashmap! {
"rust_localalias" => self.rust_localalias(),
"rust_full" => self.rust_fullname(),
"rust_extern_const" => vec_type.rust_extern_with_const(Constness::Const),
"rust_extern_mut" => vec_type.rust_extern_with_const(Constness::Mut),
- "inner_rust_func_decl" => element_type.rust_arg_func_decl("val").into(),
- "inner_rust_extern_func_decl" => element_type.rust_extern_arg_func_decl("val", Constness::Auto).into(),
- "inner_rust_func_call" => element_type.rust_arg_func_call("val", false).into(),
"inner_rust_full" => element_type.rust_full(),
- "inner_rust_full_arg" => inner_rust_full_arg.into(),
- "pre_call" => pre_call.into(),
- "pre_call_infallible" => pre_call_infallible.into(),
"inner_rust_extern_return" => element_type.rust_extern_return(),
- "inner_rust_extern_return_wrapper" => element_type.rust_extern_return_wrapper_full(),
};
if element_type.as_typedef().is_some()
&& !element_type.is_data_type()

MODIFY
binding-generator/tpl/vector/rust.tpl.rs
binding-generator/tpl/vector/rust.tpl.rs
@@ -5,48 +5,11 @@ vector_extern! { {{inner_rust_full}}, {{rust_extern_const}}, {{rust_extern_mut}}
cv_{{rust_localalias}}_capacity, cv_{{rust_localalias}}_shrink_to_fit,
cv_{{rust_localalias}}_reserve, cv_{{rust_localalias}}_remove,
cv_{{rust_localalias}}_swap, cv_{{rust_localalias}}_clear,
- cv_{{rust_localalias}}_get -> sys::{{inner_rust_extern_return_wrapper}},
+ cv_{{rust_localalias}}_get, cv_{{rust_localalias}}_set,
+ cv_{{rust_localalias}}_push, cv_{{rust_localalias}}_insert,
}
{{additional_methods}}
-impl<'i> VectorTrait<'i> for {{rust_full}} {
- type Arg = {{inner_rust_full_arg}};
-
- fn with_capacity(capacity: size_t) -> Self { Self::with_capacity(capacity) }
-
- #[inline]
- fn push(&mut self, {{inner_rust_func_decl}}) {
- extern "C" { fn cv_{{rust_localalias}}_push(instance: {{rust_extern_mut}}, {{inner_rust_extern_func_decl}}); }
- {{pre_call_infallible}}
- unsafe { cv_{{rust_localalias}}_push(self.as_raw_mut_{{rust_localalias}}(), {{inner_rust_func_call}}) }
- }
-
- #[inline]
- fn insert(&mut self, index: size_t, {{inner_rust_func_decl}}) -> Result<()> {
- extern "C" { fn cv_{{rust_localalias}}_insert(instance: {{rust_extern_mut}}, index: size_t, {{inner_rust_extern_func_decl}}); }
- core::vector_index_check(index, self.len() + 1)?;
- {{pre_call}}
- unsafe { cv_{{rust_localalias}}_insert(self.as_raw_mut_{{rust_localalias}}(), index, {{inner_rust_func_call}}) }
- Ok(())
- }
-
- #[inline]
- fn set(&mut self, index: size_t, {{inner_rust_func_decl}}) -> Result<()> {
- extern "C" { fn cv_{{rust_localalias}}_set(instance: {{rust_extern_mut}}, index: size_t, {{inner_rust_extern_func_decl}}); }
- core::vector_index_check(index, self.len())?;
- {{pre_call}}
- unsafe { cv_{{rust_localalias}}_set(self.as_raw_mut_{{rust_localalias}}(), index, {{inner_rust_func_call}}) }
- Ok(())
- }
-
- #[inline]
- unsafe fn set_unchecked(&mut self, index: size_t, {{inner_rust_func_decl}}) {
- extern "C" { fn cv_{{rust_localalias}}_set(instance: {{rust_extern_mut}}, index: size_t, {{inner_rust_extern_func_decl}}); }
- {{pre_call_infallible}}
- cv_{{rust_localalias}}_set(self.as_raw_mut_{{rust_localalias}}(), index, {{inner_rust_func_call}})
- }
-}
-
unsafe impl Send for {{rust_full}} {}
{{impls}}

MODIFY
src/lib.rs
src/lib.rs
@@ -17,7 +17,7 @@ pub mod prelude {
core::{DataType, Mat},
hub_prelude::*,
manual::prelude::*,
- traits::{Boxed, VectorTrait},
+ traits::Boxed,
};
}
@@ -26,7 +26,7 @@ pub(crate) mod mod_prelude_types {
pub use libc::{clock_t, FILE, ptrdiff_t, size_t};
- pub use crate::traits::{Boxed, OpenCVType, OpenCVTypeExternContainer, VectorTrait};
+ pub use crate::traits::{Boxed, OpenCVType, OpenCVTypeExternContainer};
}
pub(crate) mod mod_prelude {

MODIFY
src/manual/core/vector.rs
src/manual/core/vector.rs
@@ -97,6 +97,15 @@ impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
out
}
+ /// Create a Vector from iterator
+ pub fn from_iter<'a>(s: impl IntoIterator<Item=<T as OpenCVType<'a>>::Arg>) -> Self {
+ let s = s.into_iter();
+ let (lo, hi) = s.size_hint();
+ let mut out = Self::with_capacity(hi.unwrap_or(lo));
+ s.for_each(|x| out.push(x));
+ out
+ }
+
/// Return Vector length
pub fn len(&self) -> size_t {
unsafe { self.extern_len() }
@@ -145,26 +154,34 @@ impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
Ok(())
}
- // pub fn push(&mut self, val: T::ArgFuncDecl) {
- // unsafe { self.extern_push(val.as_extern()) }
- // }
- //
- // pub fn insert(&mut self, index: size_t, val: T::ArgFuncDecl) -> crate::Result<()> {
- // vector_index_check(index, self.len() + 1)?;
- // unsafe { self.extern_insert(index, val.as_extern()) }
- // Ok(())
- // }
- //
- // pub fn set(&mut self, index: size_t, val: T::ArgFuncDecl) -> Result<()> {
- // vector_index_check(index, self.len())?;
- // unsafe { self.extern_set(index, val.as_extern()) }
- // Ok(())
- // }
- //
- // pub unsafe fn set_unchecked(&mut self, index: size_t, val: T::ArgFuncDecl) {
- // self.extern_set(index, val.as_extern())
- // }
- //
+ /// Add new element
+ pub fn push(&mut self, val: <T as OpenCVType<'_>>::Arg) {
+ let val = val.opencv_into_extern_container_nofail();
+ unsafe { self.extern_push(val.opencv_to_extern()) }
+ }
+
+ /// Insert a new element at the specified `index`
+ pub fn insert(&mut self, index: size_t, val: <T as OpenCVType<'_>>::Arg) -> Result<()> {
+ vector_index_check(index, self.len() + 1)?;
+ let val = val.opencv_into_extern_container()?;
+ unsafe { self.extern_insert(index, val.opencv_to_extern()) }
+ Ok(())
+ }
+
+ /// Set element at the specified `index`
+ pub fn set(&mut self, index: size_t, val: <T as OpenCVType<'_>>::Arg) -> Result<()> {
+ vector_index_check(index, self.len())?;
+ let val = val.opencv_into_extern_container()?;
+ unsafe { self.extern_set(index, val.opencv_to_extern()) }
+ Ok(())
+ }
+
+ /// Same as `set()` but without bounds checking
+ pub unsafe fn set_unchecked(&mut self, index: size_t, val: <T as OpenCVType<'_>>::Arg) {
+ let val = val.opencv_into_extern_container_nofail();
+ self.extern_set(index, val.opencv_to_extern())
+ }
+
/// Get element at the specified `index`
pub fn get(&self, index: size_t) -> Result<T> {
vector_index_check(index, self.len())?;

MODIFY
src/manual/core/vector/vector_extern.rs
src/manual/core/vector/vector_extern.rs
@@ -5,15 +5,16 @@ use libc::size_t;
use crate::{
core::Vector,
Result,
+ traits::{OpenCVType, OpenCVTypeExternContainer},
};
#[doc(hidden)]
-pub trait VectorElement: Sized where Vector<Self>: VectorExtern<Self> {
+pub trait VectorElement: Sized + for<'a> OpenCVType<'a> where Vector<Self>: VectorExtern<Self> {
fn convert_to_vec(v: &Vector<Self>) -> Vec<Self>;
}
#[doc(hidden)]
-pub trait VectorExtern<T> {
+pub trait VectorExtern<T: for<'a> OpenCVType<'a>> {
unsafe fn extern_new() -> *mut c_void;
unsafe fn extern_delete(&mut self);
unsafe fn extern_len(&self) -> size_t;
@@ -25,9 +26,9 @@ pub trait VectorExtern<T> {
unsafe fn extern_swap(&mut self, index1: size_t, index2: size_t);
unsafe fn extern_clear(&mut self);
unsafe fn extern_get(&self, index: size_t) -> Result<T>;
- // unsafe fn extern_push(&mut self, val: <T::ArgFuncDecl as RustAsExtern>::Extern);
- // unsafe fn extern_insert(&mut self, index: size_t, val: <T::ArgFuncDecl as RustAsExtern>::Extern);
- // unsafe fn extern_set(&mut self, index: size_t, val: <T::ArgFuncDecl as RustAsExtern>::Extern);
+ unsafe fn extern_push<'a>(&mut self, val: <<<T as OpenCVType<'a>>::Arg as OpenCVType<'a>>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
+ unsafe fn extern_insert<'a>(&mut self, index: size_t, val: <<<T as OpenCVType<'a>>::Arg as OpenCVType<'a>>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
+ unsafe fn extern_set<'a>(&mut self, index: size_t, val: <<<T as OpenCVType<'a>>::Arg as OpenCVType<'a>>::ExternContainer as OpenCVTypeExternContainer>::ExternSend);
}
#[macro_export]
@@ -46,10 +47,10 @@ macro_rules! vector_extern {
$extern_remove: ident,
$extern_swap: ident,
$extern_clear: ident,
- $extern_get: ident -> $extern_return: ty,
- // $extern_push: ident,
- // $extern_insert: ident,
- // $extern_set: ident,
+ $extern_get: ident,
+ $extern_set: ident,
+ $extern_push: ident,
+ $extern_insert: ident,
) => {
impl $crate::manual::core::VectorExtern<$type> for $crate::manual::core::Vector<$type> {
#[inline(always)]
@@ -114,11 +115,29 @@ macro_rules! vector_extern {
#[inline(always)]
unsafe fn extern_get(&self, index: libc::size_t) -> $crate::Result<$type> {
- extern "C" { fn $extern_get(instance: $vector_extern_const, index: libc::size_t) -> $extern_return; }
+ extern "C" { fn $extern_get<'a>(instance: $vector_extern_const, index: libc::size_t) -> $crate::sys::Result<<$type as $crate::traits::OpenCVType<'a>>::ExternReceive>; }
$extern_get(self.as_raw(), index)
.into_result()
.map(|s| <$type>::opencv_from_extern(s))
}
+
+ #[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); }
+ $extern_push(self.as_raw_mut(), val)
+ }
+
+ #[inline(always)]
+ unsafe fn extern_insert<'a>(&mut self, index: libc::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: libc::size_t, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVType<'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: libc::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: libc::size_t, val: <<<$type as $crate::traits::OpenCVType<'a>>::Arg as $crate::traits::OpenCVType<'a>>::ExternContainer as $crate::traits::OpenCVTypeExternContainer>::ExternSend); }
+ $extern_set(self.as_raw_mut(), index, val)
+ }
}
};
}

MODIFY
src/traits/mod.rs
src/traits/mod.rs
pub use boxed::*;
pub use opencv_type::*;
-pub use vector::*;
mod boxed;
mod opencv_type;
-mod vector;

DELETE
src/traits/vector.rs
-use libc::size_t;
-
-/// Common interface for all C++ vector types generated by the crate
-///
-/// You'll need to import this trait to use any of the C++ vector wrappers, usually imported as
-/// part of the prelude.
-pub trait VectorTrait<'i>: Sized {
- type Arg;
-
- fn with_capacity(capacity: size_t) -> Self;
-
- /// Create a Vector from iterator
- fn from_iter(s: impl IntoIterator<Item=Self::Arg>) -> Self {
- let s = s.into_iter();
- let (lo, hi) = s.size_hint();
- let mut out = Self::with_capacity(hi.unwrap_or(lo));
- s.for_each(|x| out.push(x));
- out
- }
-
- /// Add new element
- fn push(&mut self, val: Self::Arg);
-
- /// Insert a new element at the specified `index`
- fn insert(&mut self, index: size_t, val: Self::Arg) -> crate::Result<()>;
-
- /// Set element at the specified `index`
- fn set(&mut self, index: size_t, val: Self::Arg) -> crate::Result<()>;
-
- /// Same as `set()` but without bounds checking
- unsafe fn set_unchecked(&mut self, index: size_t, val: Self::Arg);
-}

Move remaining methods from VectorTrait to Vector