opencv-rust icon indicating copy to clipboard operation
opencv-rust copied to clipboard

[Question] BoxedRefMut to BoxedRef possible?

Open BrettHemes-3M opened this issue 1 year ago • 5 comments

Currently updating our code to use the new BoxedRef semantics and came across the following:

I have a BoxedRefMut and want to pass along to a function expecting a (non-mutable) BoxedRef and can't figure out how to make it work. Is this possible?

Thanks!

BrettHemes-3M avatar Aug 14 '24 16:08 BrettHemes-3M

Ah, I think I missed that transform, let me see if this can be added. But otherwise, generally, is it a requirement that your function takes BoxedRef? Maybe you can take something like &impl MatTraitConst? Then you can pass Mat, BoxedRef<Mat> and BoxedRefMut<Mat>.

twistedfall avatar Aug 15 '24 08:08 twistedfall

Maybe you can take something like &impl MatTraitConst?

For sure! We tried this first after reading through some other comment threads but ran into issue when trying to pass the MatTraitConst object to cv::highgui::imshow... we get the error

the trait bound `impl MatTraitConst: opencv::core::ToInputArray` is not satisfied
the trait `opencv::core::ToInputArray` is not implemented for `impl MatTraitConst`

BrettHemes-3M avatar Aug 15 '24 13:08 BrettHemes-3M

What about having impl MatTraitConst + ToInputArray then on your argument? Or if you're using the argument only in imshow you can just have impl ToInputArray.

twistedfall avatar Aug 15 '24 13:08 twistedfall

Yes, that can work, it just seemed a bit awkward and then I started second guessing my approach. If this is the suggested way, then we can make it work.

...as I think about it some more, the impl trait approach probably saves us some into's when sending non-boxed Mats, etc.

Thank you so much for the crate and your quick response. (as an aside, we actually found a potential issue in our code that the borrow checker can now see with the new BoxedRefs 👍)

Feel free to close this as you feel fit.

BrettHemes-3M avatar Aug 15 '24 14:08 BrettHemes-3M

I still think that conversion from BoxedRefMut to BoxedRef might be useful for some use cases so I'll close this issue when I push those changes. Thank you for you kind words!

twistedfall avatar Aug 15 '24 14:08 twistedfall

The necessary impl From is now added in 0.92.3

twistedfall avatar Sep 02 '24 19:09 twistedfall

Thank you much!

BrettHemes-3M avatar Sep 03 '24 14:09 BrettHemes-3M