opencvrust

How to get underlying Mat from BoxedRef<Mat> in opencv-rust without cloning


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?


Solution

  • 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 a BoxedRef<Mat> wrapper instead of seemingly independent Mat. This fixes old soundness hole because the returned Mat was actually a view into the source Mat 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 mutable BoxedRefMut<Mat>. To get 2 mutable references to the same Mat for the non-intersecting regions use the new Mat::roi_2_mut() function.