check whether mat is continuous when calling data_typed() / data_typed_mut()
MODIFY
CHANGES.md
CHANGES.md
+* 0.30
+ * data_typed() and data_typed_mut() now do is_continuous() check
+
* 0.29.2
* add ability to get reference to the inner value of non-trait `PtrOf*` types (kudos to jerry73204)

MODIFY
src/manual/core/mat.rs
src/manual/core/mat.rs
@@ -172,6 +172,14 @@ impl Mat {
}
}
+ fn match_is_continuous(&self) -> Result<()> {
+ if self.is_continuous()? {
+ Ok(())
+ } else {
+ Err(Error::new(core::StsUnmatchedSizes, format!("Mat is not continuous, operation is not applicable")))
+ }
+ }
+
#[inline(always)]
fn idx_to_row_col(&self, i0: i32) -> Result<(i32, i32)> {
Ok(if self.is_continuous()? {
@@ -425,6 +433,7 @@ impl Mat {
pub fn data_typed<T: DataType>(&self) -> Result<&[T]> {
self.match_format::<T>()
+ .and_then(|_| self.match_is_continuous())
.and_then(|_| unsafe { self.data_typed_unchecked() })
}
@@ -434,7 +443,8 @@ impl Mat {
}
pub fn data_typed_mut<T: DataType>(&mut self) -> Result<&mut [T]> {
- self.match_format::<T>()?;
+ self.match_format::<T>()
+ .and_then(|_| self.match_is_continuous())?;
unsafe { self.data_typed_mut_unchecked() }
}

MODIFY
tests/mat.rs
tests/mat.rs
@@ -360,6 +360,13 @@ fn mat_continuous() -> Result<()> {
assert_eq!(9., vec[1][2]);
}
+ {
+ let mut sub_mat_non_cont = Mat::roi(&mat, Rect::new(1, 1, 1, 2))?;
+ assert!(!sub_mat_non_cont.is_continuous()?);
+ assert_matches!(sub_mat_non_cont.data_typed::<f32>(), Err(Error { code: core::StsUnmatchedSizes, .. }));
+ assert_matches!(sub_mat_non_cont.data_typed_mut::<f32>(), Err(Error { code: core::StsUnmatchedSizes, .. }));
+ }
+
Ok(())
}

MODIFY
tests/umat.rs
tests/umat.rs
@@ -88,7 +88,7 @@ fn umat_for_rows_and_cols() -> Result<()> {
}
#[test]
-fn umat_continous() -> Result<()> {
+fn umat_continuous() -> Result<()> {
let s: Vec<Vec<f32>> = vec![
vec![1., 2., 3.],
vec![4., 5., 6.],

check whether mat is continuous when calling data_typed() / data_typed_mut()