Add support for a fast Vector construction from slices
MODIFY
binding-generator/src/writer/rust_native/tpl/vector/cpp_methods_copy_non_bool.tpl.cpp
binding-generator/src/writer/rust_native/tpl/vector/cpp_methods_copy_non_bool.tpl.cpp
@@ -10,4 +10,7 @@ const {{inner_cpp_extern_return}}* cv_{{rust_localalias}}_data(const {{cpp_full}
return new {{cpp_full}}(*instance);
}
+{{cpp_extern_return}} cv_{{rust_localalias}}_from_slice(const {{inner_cpp_full}}* data, size_t len) {
+ return new {{cpp_full}}(data, data + len);
+}

MODIFY
binding-generator/src/writer/rust_native/tpl/vector/rust_copy_non_bool.tpl.rs
binding-generator/src/writer/rust_native/tpl/vector/rust_copy_non_bool.tpl.rs
vector_copy_non_bool! { {{inner_rust_full}}, {{rust_extern_const}}, {{rust_extern_mut}},
- cv_{{rust_localalias}}_data, cv_{{rust_localalias}}_data_mut,
+ cv_{{rust_localalias}}_data, cv_{{rust_localalias}}_data_mut, cv_{{rust_localalias}}_from_slice,
cv_{{rust_localalias}}_clone,
}

MODIFY
src/manual/core/vector.rs
src/manual/core/vector.rs
@@ -46,6 +46,11 @@ impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
out
}
+ #[inline]
+ pub fn from_slice(s: &[T]) -> Self where Self: VectorExternCopyNonBool<T> {
+ unsafe { Self::from_raw(Self::extern_from_slice(s.as_ptr(), s.len())) }
+ }
+
/// Return Vector length
pub fn len(&self) -> size_t {
unsafe { self.extern_len() }

MODIFY
src/manual/core/vector/vector_extern.rs
src/manual/core/vector/vector_extern.rs
@@ -155,6 +155,7 @@ macro_rules! vector_extern {
pub trait VectorExternCopyNonBool<T> {
#[doc(hidden)] unsafe fn extern_data(&self) -> *const T;
#[doc(hidden)] unsafe fn extern_data_mut(&mut self) -> *mut T;
+ #[doc(hidden)] unsafe fn extern_from_slice(data: *const T, len: size_t) -> *mut c_void;
}
#[macro_export]
@@ -165,6 +166,7 @@ macro_rules! vector_copy_non_bool {
$vector_extern_mut: ty,
$extern_data_const: ident,
$extern_data_mut: ident,
+ $extern_from_slice: ident,
$extern_clone: ident $(,)?
) => {
impl $crate::manual::core::Vector<$type> where $crate::manual::core::Vector<$type>: $crate::manual::core::VectorExtern<$type> {
@@ -201,6 +203,12 @@ macro_rules! vector_copy_non_bool {
extern "C" { fn $extern_data_mut(instance: $vector_extern_mut) -> *mut $type; }
$extern_data_mut(self.as_raw_mut())
}
+
+ #[inline]
+ unsafe fn extern_from_slice(data: *const $type, len: $crate::platform_types::size_t) -> $vector_extern_mut {
+ extern "C" { fn $extern_from_slice(data: *const $type, len: $crate::platform_types::size_t) -> $vector_extern_mut; }
+ $extern_from_slice(data, len)
+ }
}
};
}

MODIFY
tests/vector.rs
tests/vector.rs
@@ -29,7 +29,6 @@ use opencv::{
}
};
-
#[test]
fn boxed() -> Result<()> {
{
@@ -633,3 +632,29 @@ fn clone() -> Result<()> {
Ok(())
}
+
+#[test]
+fn from_slice() -> Result<()> {
+ {
+ let bytes: &[u8] = &[1, 2, 3, 4, 5];
+ let v = VectorOfu8::from_slice(bytes);
+ assert_eq!(bytes.len(), v.len());
+ assert_eq!(bytes, v.as_slice());
+ }
+
+ {
+ let ints: &[i32] = &[5, 10, 15, 20];
+ let v = VectorOfi32::from_slice(ints);
+ assert_eq!(ints.len(), v.len());
+ assert_eq!(ints, v.as_slice());
+ }
+
+ {
+ let points: &[Point2d] = &[Point2d::new(10., 40.), Point2d::new(0., -55.), Point2d::new(100., -1.)];
+ let v = VectorOfPoint2d::from_slice(points);
+ assert_eq!(points.len(), v.len());
+ assert_eq!(points, v.as_slice());
+ }
+
+ Ok(())
+}

Add support for a fast Vector construction from slices