Skip to content

Commit f153fb8

Browse files
committed
Add Deref for MatStep
1 parent 72e0bd5 commit f153fb8

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

src/manual/core/mat.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::convert::TryInto;
22
use std::ffi::c_void;
33
use std::marker::PhantomData;
4-
use std::ops::Deref;
4+
use std::ops::{Deref, DerefMut};
55
use std::{fmt, mem, ptr, slice};
66

77
pub use mat_::*;
88

99
use crate::boxed_ref::{BoxedRef, BoxedRefMut};
10-
use crate::core::{MatConstIterator, MatExpr, MatSize, Point, Rect, Scalar, Size, UMat};
10+
use crate::core::{MatConstIterator, MatExpr, MatSize, MatStep, Point, Rect, Scalar, Size, UMat};
1111
use crate::manual::core::DataType;
1212
use crate::prelude::*;
1313
use crate::{core, input_output_array, input_output_array_vector, Error, Result};
@@ -850,6 +850,23 @@ impl fmt::Debug for MatSize<'_> {
850850
}
851851
}
852852

853+
impl Deref for MatStep {
854+
#[cfg(not(ocvrs_opencv_branch_5))]
855+
type Target = [usize; 2];
856+
#[cfg(ocvrs_opencv_branch_5)]
857+
type Target = [usize; 3];
858+
859+
fn deref(&self) -> &Self::Target {
860+
self.buf()
861+
}
862+
}
863+
864+
impl DerefMut for MatStep {
865+
fn deref_mut(&mut self) -> &mut Self::Target {
866+
self.buf_mut()
867+
}
868+
}
869+
853870
pub trait MatConstIteratorTraitManual: MatConstIteratorTrait {
854871
#[inline]
855872
fn has_elements(&self) -> bool {

tests/marshalling.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ fn fixed_array_return() -> Result<()> {
116116
// mutable fixed array return and modification
117117
let m = Mat::new_rows_cols_with_default(5, 3, i32::opencv_type(), 1.into())?;
118118
let mut mat_step = m.mat_step();
119-
assert_eq!([12, 4], &mat_step.buf()[0..2]);
120-
mat_step.buf_mut()[0] = 16;
121-
mat_step.buf_mut()[1] = 2;
122-
assert_eq!([16, 2], &mat_step.buf()[0..2]);
119+
assert_eq!([12, 4], &mat_step[0..2]);
120+
mat_step[0] = 16;
121+
mat_step[1] = 2;
122+
assert_eq!([16, 2], &mat_step[0..2]);
123123

124124
Ok(())
125125
}

tests/mat.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ fn mat_for_rows_and_cols() -> Result<()> {
9797
assert_eq!(mast_step_len, mat.mat_step().buf().len());
9898
assert_eq!(7200, mat.mat_step().buf()[0]);
9999
assert_eq!(24, mat.mat_step().buf()[1]);
100+
assert_eq!(mast_step_len, mat.mat_step().len());
101+
assert_eq!(7200, mat.mat_step()[0]);
102+
assert_eq!(24, mat.mat_step()[1]);
100103
assert_eq!(24, mat.elem_size()?);
101104
assert_eq!(8, mat.elem_size1());
102105
assert_eq!(900, mat.step1(0)?);

0 commit comments

Comments
 (0)