Skip to content

Commit 2e8257b

Browse files
committed
Add implementation of From<Vec<T>> for Vector<T> + misc vector improvements
1 parent 34c9451 commit 2e8257b

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/manual/core/vector.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
4141

4242
/// Create a Vector from iterator
4343
pub fn from_iter<'a>(s: impl IntoIterator<Item=<T as OpenCVType<'a>>::Arg>) -> Self {
44-
<Self as FromIterator<_>>::from_iter(s)
44+
let mut out = Self::new();
45+
out.extend(s);
46+
out
4547
}
4648

4749
/// Return Vector length
@@ -145,20 +147,28 @@ impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
145147
VectorRefIterator::new(self)
146148
}
147149

150+
/// Return slice to the elements of the array.
151+
///
152+
/// This method is only available for OpenCV types that are Copy, with the exception of bool
153+
/// because bool is handled in a special way on the C++ side.
148154
pub fn as_slice(&self) -> &[T] where Self: VectorExternCopyNonBool<T> {
149155
unsafe {
150156
slice::from_raw_parts(self.extern_data(), self.len())
151157
}
152158
}
153159

160+
/// Return mutable slice to the elements of the array.
161+
///
162+
/// This method is only available for OpenCV types that are Copy, with the exception of bool
163+
/// because bool is handled in a special way on the C++ side.
154164
pub fn as_mut_slice(&mut self) -> &mut [T] where Self: VectorExternCopyNonBool<T> {
155165
unsafe {
156166
slice::from_raw_parts_mut(self.extern_data_mut(), self.len())
157167
}
158168
}
159169

160170
pub fn to_vec(&self) -> Vec<T> {
161-
T::convert_to_vec(self)
171+
T::opencv_vector_to_vec(self)
162172
}
163173
}
164174

@@ -176,11 +186,17 @@ impl<T: VectorElement> From<Vector<T>> for Vec<T> where Vector<T>: VectorExtern<
176186
}
177187
}
178188

189+
impl<T: VectorElement> From<Vec<<T as OpenCVType<'_>>::Arg>> for Vector<T> where Vector<T>: VectorExtern<T> {
190+
#[inline]
191+
fn from(from: Vec<<T as OpenCVType<'_>>::Arg>) -> Self {
192+
Self::from_iter(from)
193+
}
194+
}
195+
179196
impl<'a, T: VectorElement> FromIterator<<T as OpenCVType<'a>>::Arg> for Vector<T> where Self: VectorExtern<T> {
197+
#[inline]
180198
fn from_iter<I: IntoIterator<Item=<T as OpenCVType<'a>>::Arg>>(s: I) -> Vector<T> {
181-
let mut out = Self::new();
182-
out.extend(s);
183-
out
199+
Self::from_iter(s)
184200
}
185201
}
186202

src/manual/core/vector/vector_extern.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ use crate::{
77
traits::{OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer},
88
};
99

10-
#[doc(hidden)]
10+
/// This trait is implemented by any type that can be stored inside `Vector`.
11+
///
12+
/// It is mostly used internally, feasibility of implementing it for your own types hasn't been
13+
/// considered. Use is mostly for external type constraints.
1114
pub trait VectorElement: for<'a> OpenCVType<'a> where Vector<Self>: VectorExtern<Self> {
12-
#[doc(hidden)] fn convert_to_vec(v: &Vector<Self>) -> Vec<Self>;
15+
#[doc(hidden)] fn opencv_vector_to_vec(v: &Vector<Self>) -> Vec<Self>;
1316
}
1417

1518
#[doc(hidden)]
@@ -172,7 +175,7 @@ macro_rules! vector_copy_non_bool {
172175

173176
impl $crate::manual::core::VectorElement for $type where $crate::manual::core::Vector<$type>: $crate::manual::core::VectorExtern<$type> {
174177
#[inline(always)]
175-
fn convert_to_vec(v: &$crate::manual::core::Vector<$type>) -> std::vec::Vec<$type> {
178+
fn opencv_vector_to_vec(v: &$crate::manual::core::Vector<$type>) -> std::vec::Vec<$type> {
176179
v.as_slice().to_vec()
177180
}
178181
}
@@ -215,7 +218,7 @@ macro_rules! vector_non_copy_or_bool {
215218
($type: ty) => {
216219
impl $crate::manual::core::VectorElement for $type where $crate::manual::core::Vector<$type>: $crate::manual::core::VectorExtern<$type> {
217220
#[inline(always)]
218-
fn convert_to_vec(v: &$crate::manual::core::Vector<$type>) -> std::vec::Vec<$type> {
221+
fn opencv_vector_to_vec(v: &$crate::manual::core::Vector<$type>) -> std::vec::Vec<$type> {
219222
(0..v.len()).map(|x| unsafe { v.get_unchecked(x) }).collect()
220223
}
221224
}

0 commit comments

Comments
 (0)