Add some common trait impls to Vector
MODIFY
src/manual/core/vector.rs
src/manual/core/vector.rs
@@ -84,12 +84,57 @@ impl<T: VectorElement> OpenCVTypeExternContainer for Vector<T> where Self: Vecto
}
}
+// technically VectorExternCopyNonBool isn't needed but it simplifies this impl
+impl<T: VectorElement + ::std::fmt::Debug> ::std::fmt::Debug for Vector<T> where Vector<T>: VectorExtern<T> + VectorExternCopyNonBool<T> {
+ fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
+ ::std::fmt::Debug::fmt(self.as_slice(), f)
+ }
+}
+
+impl<T: VectorElement> AsRef<[T]> for Vector<T> where Vector<T>: VectorExtern<T> + VectorExternCopyNonBool<T> {
+ fn as_ref(&self) -> &[T] {
+ self.as_slice()
+ }
+}
+
+impl<T: VectorElement> ::std::borrow::Borrow<[T]> for Vector<T> where Vector<T>: VectorExtern<T> + VectorExternCopyNonBool<T> {
+ fn borrow(&self) -> &[T] {
+ self.as_slice()
+ }
+}
+
impl<'a, T: VectorElement> From<Vector<T>> for Vec<T> where Vector<T>: VectorExtern<T> {
fn from(from: Vector<T>) -> Self {
from.to_vec()
}
}
+impl<T: VectorElement> Default for Vector<T> where Vector<T>: VectorExtern<T> {
+ fn default() -> Vector<T> {
+ Vector::new()
+ }
+}
+
+impl<'a, T: VectorElement> Extend<<T as OpenCVType<'a>>::Arg> for Vector<T> where Vector<T>: VectorExtern<T> {
+ #[inline]
+ fn extend<I: IntoIterator<Item=<T as OpenCVType<'a>>::Arg>>(&mut self, s: I) {
+ let s = s.into_iter();
+ let (lo, hi) = s.size_hint();
+ self.reserve(hi.unwrap_or(lo));
+ s.into_iter().for_each(|elem| {
+ self.push(elem);
+ });
+ }
+}
+
+impl<'a, T: VectorElement> ::std::iter::FromIterator<<T as OpenCVType<'a>>::Arg> for Vector<T> where Vector<T>: VectorExtern<T> {
+ fn from_iter<I: IntoIterator<Item=<T as OpenCVType<'a>>::Arg>>(s: I) -> Vector<T> {
+ let mut out = Self::new();
+ out.extend(s);
+ out
+ }
+}
+
impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
/// Create a new Vector
pub fn new() -> Self {
@@ -105,11 +150,7 @@ 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 {
- let s = s.into_iter();
- let (lo, hi) = s.size_hint();
- let mut out = Self::with_capacity(hi.unwrap_or(lo));
- s.for_each(|x| out.push(x));
- out
+ <Self as ::std::iter::FromIterator<<T as OpenCVType<'a>>::Arg>>::from_iter(s)
}
/// Return Vector length

Add some common trait impls to Vector