Skip to content

Commit d718187

Browse files
committed
Drop VectorElement trait in favor of VectorToVec
1 parent edc4431 commit d718187

File tree

4 files changed

+46
-64
lines changed

4 files changed

+46
-64
lines changed

src/manual/core/vector.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::mem::ManuallyDrop;
66
use std::{fmt, slice};
77

88
pub use iter::{VectorIterator, VectorRefIterator};
9-
pub use vector_extern::{VectorElement, VectorExtern, VectorExternCopyNonBool};
9+
pub use vector_extern::{VectorExtern, VectorExternCopyNonBool};
1010

1111
use crate::platform_types::size_t;
1212
use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
@@ -16,15 +16,15 @@ mod iter;
1616
mod vector_extern;
1717

1818
/// Wrapper for C++ [std::vector](https://en.cppreference.com/w/cpp/container/vector)
19-
pub struct Vector<T: VectorElement>
19+
pub struct Vector<T: for<'o> OpenCVType<'o>>
2020
where
2121
Self: VectorExtern<T>,
2222
{
2323
ptr: *mut c_void,
2424
_d: PhantomData<T>,
2525
}
2626

27-
impl<T: VectorElement> Vector<T>
27+
impl<T: for<'o> OpenCVType<'o>> Vector<T>
2828
where
2929
Self: VectorExtern<T>,
3030
{
@@ -191,7 +191,7 @@ where
191191

192192
/// Return slice to the elements of the array.
193193
///
194-
/// This method is only available for OpenCV types that are Copy, with the exception of bool
194+
/// This method is only available for OpenCV types that are Copy, except for bool
195195
/// because bool is handled in a special way on the C++ side.
196196
#[inline]
197197
pub fn as_slice(&self) -> &[T]
@@ -203,7 +203,7 @@ where
203203

204204
/// Return mutable slice to the elements of the array.
205205
///
206-
/// This method is only available for OpenCV types that are Copy, with the exception of bool
206+
/// This method is only available for OpenCV types that are Copy, except for bool
207207
/// because bool is handled in a special way on the C++ side.
208208
#[inline]
209209
pub fn as_mut_slice(&mut self) -> &mut [T]
@@ -212,14 +212,14 @@ where
212212
{
213213
unsafe { slice::from_raw_parts_mut(self.extern_data_mut(), self.len()) }
214214
}
215+
}
215216

216-
#[inline]
217-
pub fn to_vec(&self) -> Vec<T> {
218-
T::opencv_vector_to_vec(self)
219-
}
217+
pub trait VectorToVec<T> {
218+
/// Convert `Vector` to `Vec`
219+
fn to_vec(&self) -> Vec<T>;
220220
}
221221

222-
impl<T: VectorElement> Default for Vector<T>
222+
impl<T: for<'o> OpenCVType<'o>> Default for Vector<T>
223223
where
224224
Self: VectorExtern<T>,
225225
{
@@ -229,17 +229,17 @@ where
229229
}
230230
}
231231

232-
impl<T: VectorElement> From<Vector<T>> for Vec<T>
232+
impl<T: for<'o> OpenCVType<'o>> From<Vector<T>> for Vec<T>
233233
where
234-
Vector<T>: VectorExtern<T>,
234+
Vector<T>: VectorExtern<T> + VectorToVec<T>,
235235
{
236236
#[inline]
237237
fn from(from: Vector<T>) -> Self {
238238
from.to_vec()
239239
}
240240
}
241241

242-
impl<T: VectorElement> From<Vec<<T as OpenCVType<'_>>::Arg>> for Vector<T>
242+
impl<T: for<'o> OpenCVType<'o>> From<Vec<<T as OpenCVType<'_>>::Arg>> for Vector<T>
243243
where
244244
Vector<T>: VectorExtern<T>,
245245
{
@@ -249,7 +249,7 @@ where
249249
}
250250
}
251251

252-
impl<'a, T: VectorElement> FromIterator<<T as OpenCVType<'a>>::Arg> for Vector<T>
252+
impl<'a, T: for<'o> OpenCVType<'o>> FromIterator<<T as OpenCVType<'a>>::Arg> for Vector<T>
253253
where
254254
Self: VectorExtern<T>,
255255
{
@@ -259,7 +259,7 @@ where
259259
}
260260
}
261261

262-
impl<T: VectorElement> AsRef<[T]> for Vector<T>
262+
impl<T: for<'o> OpenCVType<'o>> AsRef<[T]> for Vector<T>
263263
where
264264
Self: VectorExtern<T> + VectorExternCopyNonBool<T>,
265265
{
@@ -269,7 +269,7 @@ where
269269
}
270270
}
271271

272-
impl<T: VectorElement> Borrow<[T]> for Vector<T>
272+
impl<T: for<'o> OpenCVType<'o>> Borrow<[T]> for Vector<T>
273273
where
274274
Self: VectorExtern<T> + VectorExternCopyNonBool<T>,
275275
{
@@ -279,7 +279,7 @@ where
279279
}
280280
}
281281

282-
impl<T: VectorElement + fmt::Debug> fmt::Debug for Vector<T>
282+
impl<T: for<'o> OpenCVType<'o> + fmt::Debug> fmt::Debug for Vector<T>
283283
where
284284
Self: VectorExtern<T>,
285285
{
@@ -289,7 +289,7 @@ where
289289
}
290290
}
291291

292-
impl<T: VectorElement> Drop for Vector<T>
292+
impl<T: for<'o> OpenCVType<'o>> Drop for Vector<T>
293293
where
294294
Self: VectorExtern<T>,
295295
{
@@ -298,11 +298,11 @@ where
298298
}
299299
}
300300

301-
unsafe impl<T: Send + VectorElement> Send for Vector<T> where Self: VectorExtern<T> {}
301+
unsafe impl<T: for<'o> OpenCVType<'o> + Send> Send for Vector<T> where Self: VectorExtern<T> {}
302302

303-
unsafe impl<T: Sync + VectorElement> Sync for Vector<T> where Self: VectorExtern<T> {}
303+
unsafe impl<T: for<'o> OpenCVType<'o> + Sync> Sync for Vector<T> where Self: VectorExtern<T> {}
304304

305-
impl<'a, T: VectorElement> Extend<<T as OpenCVType<'a>>::Arg> for Vector<T>
305+
impl<'a, T: for<'o> OpenCVType<'o>> Extend<<T as OpenCVType<'a>>::Arg> for Vector<T>
306306
where
307307
Self: VectorExtern<T>,
308308
{
@@ -316,7 +316,7 @@ where
316316
}
317317
}
318318

319-
impl<T: VectorElement> Boxed for Vector<T>
319+
impl<T: for<'o> OpenCVType<'o>> Boxed for Vector<T>
320320
where
321321
Self: VectorExtern<T>,
322322
{

src/manual/core/vector/iter.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use std::iter::FusedIterator;
22

3-
use crate::{
4-
core::{Vector, VectorElement, VectorExtern},
5-
platform_types::size_t,
6-
};
3+
use crate::core::{Vector, VectorExtern};
4+
use crate::platform_types::size_t;
5+
use crate::traits::OpenCVType;
76

8-
impl<T: VectorElement> IntoIterator for Vector<T>
7+
impl<T: for<'o> OpenCVType<'o>> IntoIterator for Vector<T>
98
where
109
Vector<T>: VectorExtern<T>,
1110
{
@@ -18,7 +17,7 @@ where
1817
}
1918
}
2019

21-
impl<'v, T: VectorElement> IntoIterator for &'v Vector<T>
20+
impl<'v, T: for<'o> OpenCVType<'o>> IntoIterator for &'v Vector<T>
2221
where
2322
Vector<T>: VectorExtern<T>,
2423
{
@@ -31,7 +30,7 @@ where
3130
}
3231
}
3332

34-
pub struct VectorIterator<T: VectorElement>
33+
pub struct VectorIterator<T: for<'o> OpenCVType<'o>>
3534
where
3635
Vector<T>: VectorExtern<T>,
3736
{
@@ -40,7 +39,7 @@ where
4039
len: size_t,
4140
}
4241

43-
impl<T: VectorElement> VectorIterator<T>
42+
impl<T: for<'o> OpenCVType<'o>> VectorIterator<T>
4443
where
4544
Vector<T>: VectorExtern<T>,
4645
{
@@ -54,7 +53,7 @@ where
5453
}
5554
}
5655

57-
impl<T: VectorElement> Iterator for VectorIterator<T>
56+
impl<T: for<'o> OpenCVType<'o>> Iterator for VectorIterator<T>
5857
where
5958
Vector<T>: VectorExtern<T>,
6059
{
@@ -85,11 +84,11 @@ where
8584
}
8685
}
8786

88-
impl<T: VectorElement> ExactSizeIterator for VectorIterator<T> where Vector<T>: VectorExtern<T> {}
87+
impl<T: for<'o> OpenCVType<'o>> ExactSizeIterator for VectorIterator<T> where Vector<T>: VectorExtern<T> {}
8988

90-
impl<T: VectorElement> FusedIterator for VectorIterator<T> where Vector<T>: VectorExtern<T> {}
89+
impl<T: for<'o> OpenCVType<'o>> FusedIterator for VectorIterator<T> where Vector<T>: VectorExtern<T> {}
9190

92-
pub struct VectorRefIterator<'v, T: VectorElement>
91+
pub struct VectorRefIterator<'v, T: for<'o> OpenCVType<'o>>
9392
where
9493
Vector<T>: VectorExtern<T>,
9594
{
@@ -98,7 +97,7 @@ where
9897
len: size_t,
9998
}
10099

101-
impl<'v, T: VectorElement> VectorRefIterator<'v, T>
100+
impl<'v, T: for<'o> OpenCVType<'o>> VectorRefIterator<'v, T>
102101
where
103102
Vector<T>: VectorExtern<T>,
104103
{
@@ -112,7 +111,7 @@ where
112111
}
113112
}
114113

115-
impl<T: VectorElement> Iterator for VectorRefIterator<'_, T>
114+
impl<T: for<'o> OpenCVType<'o>> Iterator for VectorRefIterator<'_, T>
116115
where
117116
Vector<T>: VectorExtern<T>,
118117
{
@@ -143,11 +142,11 @@ where
143142
}
144143
}
145144

146-
impl<T: VectorElement> ExactSizeIterator for VectorRefIterator<'_, T> where Vector<T>: VectorExtern<T> {}
145+
impl<T: for<'o> OpenCVType<'o>> ExactSizeIterator for VectorRefIterator<'_, T> where Vector<T>: VectorExtern<T> {}
147146

148-
impl<T: VectorElement> FusedIterator for VectorRefIterator<'_, T> where Vector<T>: VectorExtern<T> {}
147+
impl<T: for<'o> OpenCVType<'o>> FusedIterator for VectorRefIterator<'_, T> where Vector<T>: VectorExtern<T> {}
149148

150-
impl<T: VectorElement> Clone for VectorRefIterator<'_, T>
149+
impl<T: for<'o> OpenCVType<'o>> Clone for VectorRefIterator<'_, T>
151150
where
152151
Vector<T>: VectorExtern<T>,
153152
{

src/manual/core/vector/vector_extern.rs

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@ use crate::platform_types::size_t;
33
use crate::traits::OpenCVType;
44
use crate::{extern_arg_send, extern_container_send, extern_receive};
55

6-
/// This trait is implemented by any type that can be stored inside `Vector`.
7-
///
8-
/// It is mostly used internally, feasibility of implementing it for your own types hasn't been
9-
/// considered. Use is mostly for external type constraints.
10-
pub trait VectorElement: for<'a> OpenCVType<'a>
11-
where
12-
Vector<Self>: VectorExtern<Self>,
13-
{
14-
#[doc(hidden)]
15-
fn opencv_vector_to_vec(v: &Vector<Self>) -> Vec<Self>;
16-
}
17-
186
#[doc(hidden)]
197
pub trait VectorExtern<T: for<'a> OpenCVType<'a>> {
208
#[doc(hidden)]
@@ -154,7 +142,7 @@ macro_rules! vector_extern {
154142
}
155143

156144
#[doc(hidden)]
157-
pub trait VectorExternCopyNonBool<T: VectorElement>
145+
pub trait VectorExternCopyNonBool<T: for<'o> OpenCVType<'o>>
158146
where
159147
Vector<T>: VectorExtern<T>,
160148
{
@@ -176,13 +164,10 @@ macro_rules! vector_copy_non_bool {
176164
$extern_from_slice: ident,
177165
$extern_clone: ident $(,)?
178166
) => {
179-
impl $crate::core::VectorElement for $type
180-
where
181-
$crate::core::Vector<$type>: $crate::core::VectorExtern<$type>,
182-
{
167+
impl $crate::core::VectorToVec<$type> for $crate::core::Vector<$type> {
183168
#[inline]
184-
fn opencv_vector_to_vec(v: &$crate::core::Vector<$type>) -> std::vec::Vec<$type> {
185-
v.as_slice().to_vec()
169+
fn to_vec(&self) -> std::vec::Vec<$type> {
170+
self.as_slice().to_vec()
186171
}
187172
}
188173

@@ -233,13 +218,10 @@ macro_rules! vector_non_copy_or_bool {
233218
vector_non_copy_or_bool! { $type }
234219
};
235220
($type: ty) => {
236-
impl $crate::core::VectorElement for $type
237-
where
238-
$crate::core::Vector<$type>: $crate::core::VectorExtern<$type>,
239-
{
221+
impl $crate::core::VectorToVec<$type> for $crate::core::Vector<$type> {
240222
#[inline]
241-
fn opencv_vector_to_vec(v: &$crate::core::Vector<$type>) -> std::vec::Vec<$type> {
242-
(0..v.len()).map(|x| unsafe { v.get_unchecked(x) }).collect()
223+
fn to_vec(&self) -> std::vec::Vec<$type> {
224+
(0..self.len()).map(|x| unsafe { self.get_unchecked(x) }).collect()
243225
}
244226
}
245227
};

src/manual/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod types;
88
pub mod prelude {
99
#[cfg(all(ocvrs_has_module_core, ocvrs_opencv_branch_32))]
1010
pub use super::core::MatSizeTraitConstManual;
11+
pub use super::core::VectorToVec;
1112
#[cfg(ocvrs_has_module_core)]
1213
pub use super::core::{MatConstIteratorTraitManual, MatTraitConstManual, MatTraitManual, MatxTrait};
1314
}

0 commit comments

Comments
 (0)