I was going through and updating the opencv dependency in my Rust project. Due to some breaking changes, functions that previously returned a Mat
now return a BoxedRef<Mat>
. How would I return the underlying Mat
without cloning? Here is an example couple lines of code:
pub fn crop(input: &Mat, x: i32, y: i32, width: i32, height: i32) -> opencv::Result<Mat, RipsError> {
let cropped = Mat::roi(input, Rect::new(x, y, width, height))?;
Ok(cropped)
}
Now returns the error:
Type mismatch [E0308] expected `Mat`, but found `BoxedRef<Mat>`
Is there anyway to "unwrap
" the Mat
out of the BoxedRef
?
You can't.
Semantically BoxedRef<'_, Mat>
is equivalent to a shared reference &'_ Mat
so you can't simply take ownership over it.
These methods returning Mat
previously was just unsound (as the changelog of 0.89.0 tells us):
- Some functions, notably
Mat::roi()
, now return aBoxedRef<Mat>
wrapper instead of seemingly independentMat
. This fixes old soundness hole because the returnedMat
was actually a view into the sourceMat
and you could easily end up with 2 mutable references to the same memory. An additional*_mut()
is generated for each of such function to return a mutableBoxedRefMut<Mat>
. To get 2 mutable references to the sameMat
for the non-intersecting regions use the newMat::roi_2_mut()
function.