Add implementation of From<Vec<T>> for Vector<T> + misc vector improvements
MODIFY
src/manual/core/vector.rs
src/manual/core/vector.rs
@@ -41,7 +41,9 @@ impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
/// Create a Vector from iterator
pub fn from_iter<'a>(s: impl IntoIterator<Item=<T as OpenCVType<'a>>::Arg>) -> Self {
- <Self as FromIterator<_>>::from_iter(s)
+ let mut out = Self::new();
+ out.extend(s);
+ out
}
/// Return Vector length
@@ -145,12 +147,20 @@ impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
VectorRefIterator::new(self)
}
+ /// Return slice to the elements of the array.
+ ///
+ /// This method is only available for OpenCV types that are Copy, with the exception of bool
+ /// because bool is handled in a special way on the C++ side.
pub fn as_slice(&self) -> &[T] where Self: VectorExternCopyNonBool<T> {
unsafe {
slice::from_raw_parts(self.extern_data(), self.len())
}
}
+ /// Return mutable slice to the elements of the array.
+ ///
+ /// This method is only available for OpenCV types that are Copy, with the exception of bool
+ /// because bool is handled in a special way on the C++ side.
pub fn as_mut_slice(&mut self) -> &mut [T] where Self: VectorExternCopyNonBool<T> {
unsafe {
slice::from_raw_parts_mut(self.extern_data_mut(), self.len())
@@ -158,7 +168,7 @@ impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
}
pub fn to_vec(&self) -> Vec<T> {
- T::convert_to_vec(self)
+ T::opencv_vector_to_vec(self)
}
}
@@ -176,11 +186,17 @@ impl<T: VectorElement> From<Vector<T>> for Vec<T> where Vector<T>: VectorExtern<
}
}
+impl<T: VectorElement> From<Vec<<T as OpenCVType<'_>>::Arg>> for Vector<T> where Vector<T>: VectorExtern<T> {
+ #[inline]
+ fn from(from: Vec<<T as OpenCVType<'_>>::Arg>) -> Self {
+ Self::from_iter(from)
+ }
+}
+
impl<'a, T: VectorElement> FromIterator<<T as OpenCVType<'a>>::Arg> for Vector<T> where Self: VectorExtern<T> {
+ #[inline]
fn from_iter<I: IntoIterator<Item=<T as OpenCVType<'a>>::Arg>>(s: I) -> Vector<T> {
- let mut out = Self::new();
- out.extend(s);
- out
+ Self::from_iter(s)
}
}

MODIFY
src/manual/core/vector/vector_extern.rs
src/manual/core/vector/vector_extern.rs
@@ -7,9 +7,12 @@ use crate::{
traits::{OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
};
-#[doc(hidden)]
+/// This trait is implemented by any type that can be stored inside `Vector`.
+///
+/// It is mostly used internally, feasibility of implementing it for your own types hasn't been
+/// considered. Use is mostly for external type constraints.
pub trait VectorElement: for<'a> OpenCVType<'a> where Vector<Self>: VectorExtern<Self> {
- #[doc(hidden)] fn convert_to_vec(v: &Vector<Self>) -> Vec<Self>;
+ #[doc(hidden)] fn opencv_vector_to_vec(v: &Vector<Self>) -> Vec<Self>;
}
#[doc(hidden)]
@@ -172,7 +175,7 @@ macro_rules! vector_copy_non_bool {
impl $crate::manual::core::VectorElement for $type where $crate::manual::core::Vector<$type>: $crate::manual::core::VectorExtern<$type> {
#[inline(always)]
- fn convert_to_vec(v: &$crate::manual::core::Vector<$type>) -> std::vec::Vec<$type> {
+ fn opencv_vector_to_vec(v: &$crate::manual::core::Vector<$type>) -> std::vec::Vec<$type> {
v.as_slice().to_vec()
}
}
@@ -215,7 +218,7 @@ macro_rules! vector_non_copy_or_bool {
($type: ty) => {
impl $crate::manual::core::VectorElement for $type where $crate::manual::core::Vector<$type>: $crate::manual::core::VectorExtern<$type> {
#[inline(always)]
- fn convert_to_vec(v: &$crate::manual::core::Vector<$type>) -> std::vec::Vec<$type> {
+ fn opencv_vector_to_vec(v: &$crate::manual::core::Vector<$type>) -> std::vec::Vec<$type> {
(0..v.len()).map(|x| unsafe { v.get_unchecked(x) }).collect()
}
}

Add implementation of From<Vec<T>> for Vector<T> + misc vector improvements