Add Vector::from_elem()
MODIFY
src/manual/core/vector.rs
src/manual/core/vector.rs
@@ -56,6 +56,15 @@ impl<T: VectorElement> Vector<T> where Self: VectorExtern<T> {
unsafe { Self::from_raw(Self::extern_from_slice(s.as_ptr(), s.len())) }
}
+ #[inline]
+ pub fn from_elem<'a>(elem: <T as OpenCVType<'a>>::Arg, n: size_t) -> Self where <T as OpenCVType<'a>>::Arg: Clone {
+ let mut out = Self::with_capacity(n);
+ for _ in 0..n {
+ out.push(elem.clone());
+ }
+ out
+ }
+
/// Return Vector length
#[inline]
pub fn len(&self) -> size_t {

MODIFY
tests/vector.rs
tests/vector.rs
@@ -221,6 +221,20 @@ fn vector_of_vector_simple_struct() -> Result<()> {
Ok(())
}
+#[test]
+fn from_elem() {
+ let v = VectorOfi32::from_elem(5, 10);
+ assert_eq!(10, v.len());
+ assert!(v.iter().all(|v| v == 5));
+
+ let v = VectorOfMat::from_elem(Mat::default(), 0);
+ assert_eq!(0, v.len());
+
+ let v = VectorOfString::from_elem("test", 3);
+ assert_eq!(3, v.len());
+ assert!(v.iter().all(|v| v == "test"));
+}
+
#[test]
fn capacity() {
{

Add Vector::from_elem()