Skip to content

Commit

Permalink
Drop VectorElement trait in favor of VectorToVec
Browse files Browse the repository at this point in the history
  • Loading branch information
twistedfall committed Apr 25, 2024
1 parent edc4431 commit d718187
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 64 deletions.
44 changes: 22 additions & 22 deletions src/manual/core/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::mem::ManuallyDrop;
use std::{fmt, slice};

pub use iter::{VectorIterator, VectorRefIterator};
pub use vector_extern::{VectorElement, VectorExtern, VectorExternCopyNonBool};
pub use vector_extern::{VectorExtern, VectorExternCopyNonBool};

use crate::platform_types::size_t;
use crate::traits::{Boxed, OpenCVType, OpenCVTypeArg, OpenCVTypeExternContainer};
Expand All @@ -16,15 +16,15 @@ mod iter;
mod vector_extern;

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

impl<T: VectorElement> Vector<T>
impl<T: for<'o> OpenCVType<'o>> Vector<T>
where
Self: VectorExtern<T>,
{
Expand Down Expand Up @@ -191,7 +191,7 @@ where

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

/// 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
/// This method is only available for OpenCV types that are Copy, except for bool
/// because bool is handled in a special way on the C++ side.
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [T]
Expand All @@ -212,14 +212,14 @@ where
{
unsafe { slice::from_raw_parts_mut(self.extern_data_mut(), self.len()) }
}
}

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

impl<T: VectorElement> Default for Vector<T>
impl<T: for<'o> OpenCVType<'o>> Default for Vector<T>
where
Self: VectorExtern<T>,
{
Expand All @@ -229,17 +229,17 @@ where
}
}

impl<T: VectorElement> From<Vector<T>> for Vec<T>
impl<T: for<'o> OpenCVType<'o>> From<Vector<T>> for Vec<T>
where
Vector<T>: VectorExtern<T>,
Vector<T>: VectorExtern<T> + VectorToVec<T>,
{
#[inline]
fn from(from: Vector<T>) -> Self {
from.to_vec()
}
}

impl<T: VectorElement> From<Vec<<T as OpenCVType<'_>>::Arg>> for Vector<T>
impl<T: for<'o> OpenCVType<'o>> From<Vec<<T as OpenCVType<'_>>::Arg>> for Vector<T>
where
Vector<T>: VectorExtern<T>,
{
Expand All @@ -249,7 +249,7 @@ where
}
}

impl<'a, T: VectorElement> FromIterator<<T as OpenCVType<'a>>::Arg> for Vector<T>
impl<'a, T: for<'o> OpenCVType<'o>> FromIterator<<T as OpenCVType<'a>>::Arg> for Vector<T>
where
Self: VectorExtern<T>,
{
Expand All @@ -259,7 +259,7 @@ where
}
}

impl<T: VectorElement> AsRef<[T]> for Vector<T>
impl<T: for<'o> OpenCVType<'o>> AsRef<[T]> for Vector<T>
where
Self: VectorExtern<T> + VectorExternCopyNonBool<T>,
{
Expand All @@ -269,7 +269,7 @@ where
}
}

impl<T: VectorElement> Borrow<[T]> for Vector<T>
impl<T: for<'o> OpenCVType<'o>> Borrow<[T]> for Vector<T>
where
Self: VectorExtern<T> + VectorExternCopyNonBool<T>,
{
Expand All @@ -279,7 +279,7 @@ where
}
}

impl<T: VectorElement + fmt::Debug> fmt::Debug for Vector<T>
impl<T: for<'o> OpenCVType<'o> + fmt::Debug> fmt::Debug for Vector<T>
where
Self: VectorExtern<T>,
{
Expand All @@ -289,7 +289,7 @@ where
}
}

impl<T: VectorElement> Drop for Vector<T>
impl<T: for<'o> OpenCVType<'o>> Drop for Vector<T>
where
Self: VectorExtern<T>,
{
Expand All @@ -298,11 +298,11 @@ where
}
}

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

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

impl<'a, T: VectorElement> Extend<<T as OpenCVType<'a>>::Arg> for Vector<T>
impl<'a, T: for<'o> OpenCVType<'o>> Extend<<T as OpenCVType<'a>>::Arg> for Vector<T>
where
Self: VectorExtern<T>,
{
Expand All @@ -316,7 +316,7 @@ where
}
}

impl<T: VectorElement> Boxed for Vector<T>
impl<T: for<'o> OpenCVType<'o>> Boxed for Vector<T>
where
Self: VectorExtern<T>,
{
Expand Down
33 changes: 16 additions & 17 deletions src/manual/core/vector/iter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::iter::FusedIterator;

use crate::{
core::{Vector, VectorElement, VectorExtern},
platform_types::size_t,
};
use crate::core::{Vector, VectorExtern};
use crate::platform_types::size_t;
use crate::traits::OpenCVType;

impl<T: VectorElement> IntoIterator for Vector<T>
impl<T: for<'o> OpenCVType<'o>> IntoIterator for Vector<T>
where
Vector<T>: VectorExtern<T>,
{
Expand All @@ -18,7 +17,7 @@ where
}
}

impl<'v, T: VectorElement> IntoIterator for &'v Vector<T>
impl<'v, T: for<'o> OpenCVType<'o>> IntoIterator for &'v Vector<T>
where
Vector<T>: VectorExtern<T>,
{
Expand All @@ -31,7 +30,7 @@ where
}
}

pub struct VectorIterator<T: VectorElement>
pub struct VectorIterator<T: for<'o> OpenCVType<'o>>
where
Vector<T>: VectorExtern<T>,
{
Expand All @@ -40,7 +39,7 @@ where
len: size_t,
}

impl<T: VectorElement> VectorIterator<T>
impl<T: for<'o> OpenCVType<'o>> VectorIterator<T>
where
Vector<T>: VectorExtern<T>,
{
Expand All @@ -54,7 +53,7 @@ where
}
}

impl<T: VectorElement> Iterator for VectorIterator<T>
impl<T: for<'o> OpenCVType<'o>> Iterator for VectorIterator<T>
where
Vector<T>: VectorExtern<T>,
{
Expand Down Expand Up @@ -85,11 +84,11 @@ where
}
}

impl<T: VectorElement> ExactSizeIterator for VectorIterator<T> where Vector<T>: VectorExtern<T> {}
impl<T: for<'o> OpenCVType<'o>> ExactSizeIterator for VectorIterator<T> where Vector<T>: VectorExtern<T> {}

impl<T: VectorElement> FusedIterator for VectorIterator<T> where Vector<T>: VectorExtern<T> {}
impl<T: for<'o> OpenCVType<'o>> FusedIterator for VectorIterator<T> where Vector<T>: VectorExtern<T> {}

pub struct VectorRefIterator<'v, T: VectorElement>
pub struct VectorRefIterator<'v, T: for<'o> OpenCVType<'o>>
where
Vector<T>: VectorExtern<T>,
{
Expand All @@ -98,7 +97,7 @@ where
len: size_t,
}

impl<'v, T: VectorElement> VectorRefIterator<'v, T>
impl<'v, T: for<'o> OpenCVType<'o>> VectorRefIterator<'v, T>
where
Vector<T>: VectorExtern<T>,
{
Expand All @@ -112,7 +111,7 @@ where
}
}

impl<T: VectorElement> Iterator for VectorRefIterator<'_, T>
impl<T: for<'o> OpenCVType<'o>> Iterator for VectorRefIterator<'_, T>
where
Vector<T>: VectorExtern<T>,
{
Expand Down Expand Up @@ -143,11 +142,11 @@ where
}
}

impl<T: VectorElement> ExactSizeIterator for VectorRefIterator<'_, T> where Vector<T>: VectorExtern<T> {}
impl<T: for<'o> OpenCVType<'o>> ExactSizeIterator for VectorRefIterator<'_, T> where Vector<T>: VectorExtern<T> {}

impl<T: VectorElement> FusedIterator for VectorRefIterator<'_, T> where Vector<T>: VectorExtern<T> {}
impl<T: for<'o> OpenCVType<'o>> FusedIterator for VectorRefIterator<'_, T> where Vector<T>: VectorExtern<T> {}

impl<T: VectorElement> Clone for VectorRefIterator<'_, T>
impl<T: for<'o> OpenCVType<'o>> Clone for VectorRefIterator<'_, T>
where
Vector<T>: VectorExtern<T>,
{
Expand Down
32 changes: 7 additions & 25 deletions src/manual/core/vector/vector_extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,6 @@ use crate::platform_types::size_t;
use crate::traits::OpenCVType;
use crate::{extern_arg_send, extern_container_send, extern_receive};

/// 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 opencv_vector_to_vec(v: &Vector<Self>) -> Vec<Self>;
}

#[doc(hidden)]
pub trait VectorExtern<T: for<'a> OpenCVType<'a>> {
#[doc(hidden)]
Expand Down Expand Up @@ -154,7 +142,7 @@ macro_rules! vector_extern {
}

#[doc(hidden)]
pub trait VectorExternCopyNonBool<T: VectorElement>
pub trait VectorExternCopyNonBool<T: for<'o> OpenCVType<'o>>
where
Vector<T>: VectorExtern<T>,
{
Expand All @@ -176,13 +164,10 @@ macro_rules! vector_copy_non_bool {
$extern_from_slice: ident,
$extern_clone: ident $(,)?
) => {
impl $crate::core::VectorElement for $type
where
$crate::core::Vector<$type>: $crate::core::VectorExtern<$type>,
{
impl $crate::core::VectorToVec<$type> for $crate::core::Vector<$type> {
#[inline]
fn opencv_vector_to_vec(v: &$crate::core::Vector<$type>) -> std::vec::Vec<$type> {
v.as_slice().to_vec()
fn to_vec(&self) -> std::vec::Vec<$type> {
self.as_slice().to_vec()
}
}

Expand Down Expand Up @@ -233,13 +218,10 @@ macro_rules! vector_non_copy_or_bool {
vector_non_copy_or_bool! { $type }
};
($type: ty) => {
impl $crate::core::VectorElement for $type
where
$crate::core::Vector<$type>: $crate::core::VectorExtern<$type>,
{
impl $crate::core::VectorToVec<$type> for $crate::core::Vector<$type> {
#[inline]
fn opencv_vector_to_vec(v: &$crate::core::Vector<$type>) -> std::vec::Vec<$type> {
(0..v.len()).map(|x| unsafe { v.get_unchecked(x) }).collect()
fn to_vec(&self) -> std::vec::Vec<$type> {
(0..self.len()).map(|x| unsafe { self.get_unchecked(x) }).collect()
}
}
};
Expand Down
1 change: 1 addition & 0 deletions src/manual/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod types;
pub mod prelude {
#[cfg(all(ocvrs_has_module_core, ocvrs_opencv_branch_32))]
pub use super::core::MatSizeTraitConstManual;
pub use super::core::VectorToVec;
#[cfg(ocvrs_has_module_core)]
pub use super::core::{MatConstIteratorTraitManual, MatTraitConstManual, MatTraitManual, MatxTrait};
}

0 comments on commit d718187

Please sign in to comment.