add .get() method to acquire immutable reference to the smart pointer contents
MODIFY
gen_rust.py
gen_rust.py
@@ -2993,37 +2993,61 @@ class SmartPtrTypeInfo(TypeInfo):
"""),
"rust_deref": template("""
- pub struct ${rust_deref_guard}<'a> {
- guarded: &'a ${rust_local},
+ impl ${rust_local} {
+ #[inline(always)] fn get_inner(&self) -> ${rust_extern} {
+ let me = self.ptr;
+ cpp!(unsafe [me as "cv::Ptr<${inner_cpp_type}>*"] -> ${rust_extern} as "${cpp_extern}" {
+ return me->get();
+ })
+ }
+
+ pub fn get(&self) -> ${inner_rust_local}Ref {
+ let inner = ${inner_rust_full} { ptr: self.get_inner() };
+ ${inner_rust_local}Ref {
+ inner: std::mem::ManuallyDrop::new(inner),
+ owner: std::marker::PhantomData,
+ }
+ }
+
+ pub fn get_mut(&mut self) -> ${inner_rust_local}RefMut {
+ let inner = ${inner_rust_full} { ptr: self.get_inner() };
+ ${inner_rust_local}RefMut {
+ inner: std::mem::ManuallyDrop::new(inner),
+ owner: std::marker::PhantomData,
+ }
+ }
+ }
+
+ pub struct ${inner_rust_local}Ref<'o> {
inner: std::mem::ManuallyDrop<${inner_rust_full}>,
+ owner: std::marker::PhantomData<&'o ${rust_full}>,
}
- impl<'a> std::ops::Deref for ${rust_deref_guard}<'a> {
+ impl std::ops::Deref for ${inner_rust_local}Ref<'_> {
type Target = ${inner_rust_full};
fn deref(&self) -> &Self::Target {
- self.inner.deref()
+ &*self.inner
}
}
- impl<'a> std::ops::DerefMut for ${rust_deref_guard}<'a> {
- fn deref_mut(&mut self) -> &mut Self::Target {
- self.inner.deref_mut()
- }
+ pub struct ${inner_rust_local}RefMut<'o> {
+ inner: std::mem::ManuallyDrop<${inner_rust_full}>,
+ owner: std::marker::PhantomData<&'o mut ${rust_full}>,
}
- impl ${rust_local} {
- pub fn deref_mut<'a>(&'a mut self) -> ${rust_deref_guard}<'a> {
- let me = self.ptr;
- let inner_ptr = cpp!(unsafe [me as "cv::Ptr<${inner_cpp_type}>*"] -> ${rust_extern} as "${cpp_extern}" {
- return me->get();
- });
- let inner = ${inner_rust_full} { ptr: inner_ptr };
- ${rust_deref_guard} {
- guarded: self,
- inner: std::mem::ManuallyDrop::new(inner),
+ impl std::ops::Deref for ${inner_rust_local}RefMut<'_> {
+ type Target = ${inner_rust_full};
+
+ fn deref(&self) -> &Self::Target {
+ &*self.inner
}
}
+
+ impl std::ops::DerefMut for ${inner_rust_local}RefMut<'_> {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut *self.inner
+ }
}
"""),
}
@@ -3061,11 +3085,10 @@ class SmartPtrTypeInfo(TypeInfo):
"base_cpp_type": cibase.cpptype,
})))
else:
- rust_deref_guard = "{}Guard".format(self.rust_local)
f.write(SmartPtrTypeInfo.TEMPLATES["rust_deref"].substitute(combine_dicts(self.__dict__ , {
+ "inner_rust_local": self.inner.rust_local,
"inner_rust_full": self.inner.rust_full,
"inner_cpp_type": self.inner.cpptype,
- "rust_deref_guard": rust_deref_guard,
})))
write_exc("{}/{}-{}.type.rs".format(self.gen.rust_dir, self.rust_module(), self.rust_safe_id), write_rust)

MODIFY
tests/aruco.rs
tests/aruco.rs
@@ -5,85 +5,88 @@ use opencv::{aruco::DetectorParameters, Result};
#[test]
fn aruco_detector_parameters() -> Result<()> {
let mut ptr = DetectorParameters::create()?;
- let mut params = ptr.deref_mut();
+ let mut plain = DetectorParameters::default()?;
- assert_eq!(params.adaptive_thresh_win_size_min()?, 3);
- params.set_adaptive_thresh_win_size_min(4)?;
- assert_eq!(params.adaptive_thresh_win_size_min()?, 4);
+ assert_eq!(plain.adaptive_thresh_win_size_min()?, ptr.get().adaptive_thresh_win_size_min()?);
+ ptr.get_mut().set_adaptive_thresh_win_size_min(4)?;
+ assert_eq!(ptr.get().adaptive_thresh_win_size_min()?, 4);
- assert_eq!(params.adaptive_thresh_win_size_max()?, 23);
- params.set_adaptive_thresh_win_size_max(24)?;
- assert_eq!(params.adaptive_thresh_win_size_max()?, 24);
+ assert_eq!(plain.adaptive_thresh_win_size_max()?, ptr.get().adaptive_thresh_win_size_max()?);
+ ptr.get_mut().set_adaptive_thresh_win_size_max(24)?;
+ assert_eq!(ptr.get().adaptive_thresh_win_size_max()?, 24);
- assert_eq!(params.adaptive_thresh_win_size_step()?, 10);
- params.set_adaptive_thresh_win_size_step(11)?;
- assert_eq!(params.adaptive_thresh_win_size_step()?, 11);
+ assert_eq!(plain.adaptive_thresh_win_size_step()?, ptr.get().adaptive_thresh_win_size_step()?);
+ ptr.get_mut().set_adaptive_thresh_win_size_step(11)?;
+ assert_eq!(ptr.get().adaptive_thresh_win_size_step()?, 11);
- assert_eq!(params.adaptive_thresh_constant()?, 7.0);
- params.set_adaptive_thresh_constant(8.0)?;
- assert_eq!(params.adaptive_thresh_constant()?, 8.0);
+ assert_eq!(plain.adaptive_thresh_constant()?, ptr.get().adaptive_thresh_constant()?);
+ ptr.get_mut().set_adaptive_thresh_constant(8.0)?;
+ assert_eq!(ptr.get().adaptive_thresh_constant()?, 8.0);
- assert_eq!(params.min_marker_perimeter_rate()?, 0.03);
- params.set_min_marker_perimeter_rate(1.0)?;
- assert_eq!(params.min_marker_perimeter_rate()?, 1.0);
+ assert_eq!(plain.min_marker_perimeter_rate()?, ptr.get().min_marker_perimeter_rate()?);
+ ptr.get_mut().set_min_marker_perimeter_rate(1.0)?;
+ assert_eq!(ptr.get().min_marker_perimeter_rate()?, 1.0);
- assert_eq!(params.max_marker_perimeter_rate()?, 4.0);
- params.set_max_marker_perimeter_rate(5.0)?;
- assert_eq!(params.max_marker_perimeter_rate()?, 5.0);
+ assert_eq!(plain.max_marker_perimeter_rate()?, ptr.get().max_marker_perimeter_rate()?);
+ ptr.get_mut().set_max_marker_perimeter_rate(5.0)?;
+ assert_eq!(ptr.get().max_marker_perimeter_rate()?, 5.0);
- assert_eq!(params.min_corner_distance_rate()?, 0.05);
- params.set_min_corner_distance_rate(1.0)?;
- assert_eq!(params.min_corner_distance_rate()?, 1.0);
+ assert_eq!(plain.min_corner_distance_rate()?, ptr.get().min_corner_distance_rate()?);
+ ptr.get_mut().set_min_corner_distance_rate(1.0)?;
+ assert_eq!(ptr.get().min_corner_distance_rate()?, 1.0);
- assert_eq!(params.min_distance_to_border()?, 3);
- params.set_min_distance_to_border(4)?;
- assert_eq!(params.min_distance_to_border()?, 4);
+ assert_eq!(plain.min_distance_to_border()?, ptr.get().min_distance_to_border()?);
+ ptr.get_mut().set_min_distance_to_border(4)?;
+ assert_eq!(ptr.get().min_distance_to_border()?, 4);
- assert_eq!(params.min_marker_distance_rate()?, 0.05);
- params.set_min_marker_distance_rate(1.0)?;
- assert_eq!(params.min_marker_distance_rate()?, 1.0);
+ assert_eq!(plain.min_marker_distance_rate()?, ptr.get().min_marker_distance_rate()?);
+ ptr.get_mut().set_min_marker_distance_rate(1.0)?;
+ assert_eq!(ptr.get().min_marker_distance_rate()?, 1.0);
- assert_eq!(params.corner_refinement_win_size()?, 5);
- params.set_corner_refinement_win_size(6)?;
- assert_eq!(params.corner_refinement_win_size()?, 6);
+ assert_eq!(plain.corner_refinement_win_size()?, ptr.get().corner_refinement_win_size()?);
+ ptr.get_mut().set_corner_refinement_win_size(6)?;
+ assert_eq!(ptr.get().corner_refinement_win_size()?, 6);
- assert_eq!(params.corner_refinement_max_iterations()?, 30);
- params.set_corner_refinement_max_iterations(31)?;
- assert_eq!(params.corner_refinement_max_iterations()?, 31);
+ assert_eq!(plain.corner_refinement_max_iterations()?, ptr.get().corner_refinement_max_iterations()?);
+ ptr.get_mut().set_corner_refinement_max_iterations(31)?;
+ assert_eq!(ptr.get().corner_refinement_max_iterations()?, 31);
- assert_eq!(params.corner_refinement_min_accuracy()?, 0.1);
- params.set_corner_refinement_min_accuracy(1.0)?;
- assert_eq!(params.corner_refinement_min_accuracy()?, 1.0);
+ assert_eq!(plain.corner_refinement_min_accuracy()?, ptr.get().corner_refinement_min_accuracy()?);
+ ptr.get_mut().set_corner_refinement_min_accuracy(1.0)?;
+ assert_eq!(ptr.get().corner_refinement_min_accuracy()?, 1.0);
- assert_eq!(params.marker_border_bits()?, 1);
- params.set_marker_border_bits(2)?;
- assert_eq!(params.marker_border_bits()?, 2);
+ assert_eq!(plain.marker_border_bits()?, ptr.get().marker_border_bits()?);
+ ptr.get_mut().set_marker_border_bits(2)?;
+ assert_eq!(ptr.get().marker_border_bits()?, 2);
- assert_eq!(params.perspective_remove_ignored_margin_per_cell()?, 0.13);
- params.set_perspective_remove_ignored_margin_per_cell(1.0)?;
- assert_eq!(params.perspective_remove_ignored_margin_per_cell()?, 1.0);
+ assert_eq!(plain.perspective_remove_ignored_margin_per_cell()?, ptr.get().perspective_remove_ignored_margin_per_cell()?);
+ ptr.get_mut().set_perspective_remove_ignored_margin_per_cell(1.0)?;
+ assert_eq!(ptr.get().perspective_remove_ignored_margin_per_cell()?, 1.0);
- assert_eq!(params.max_erroneous_bits_in_border_rate()?, 0.35);
- params.set_max_erroneous_bits_in_border_rate(1.0)?;
- assert_eq!(params.max_erroneous_bits_in_border_rate()?, 1.0);
+ assert_eq!(plain.max_erroneous_bits_in_border_rate()?, ptr.get().max_erroneous_bits_in_border_rate()?);
+ ptr.get_mut().set_max_erroneous_bits_in_border_rate(1.0)?;
+ assert_eq!(ptr.get().max_erroneous_bits_in_border_rate()?, 1.0);
- assert_eq!(params.min_otsu_std_dev()?, 5.0);
- params.set_min_otsu_std_dev(6.0)?;
- assert_eq!(params.min_otsu_std_dev()?, 6.0);
+ assert_eq!(plain.min_otsu_std_dev()?, ptr.get().min_otsu_std_dev()?);
+ ptr.get_mut().set_min_otsu_std_dev(6.0)?;
+ assert_eq!(ptr.get().min_otsu_std_dev()?, 6.0);
- assert_eq!(params.error_correction_rate()?, 0.6);
- params.set_error_correction_rate(1.0)?;
- assert_eq!(params.error_correction_rate()?, 1.0);
+ assert_eq!(plain.error_correction_rate()?, ptr.get().error_correction_rate()?);
+ ptr.get_mut().set_error_correction_rate(1.0)?;
+ assert_eq!(ptr.get().error_correction_rate()?, 1.0);
- assert_eq!(params.detect_inverted_marker()?, false);
- params.set_detect_inverted_marker(true)?;
- assert_eq!(params.detect_inverted_marker()?, true);
+ assert_eq!(plain.detect_inverted_marker()?, ptr.get().detect_inverted_marker()?);
+ ptr.get_mut().set_detect_inverted_marker(true)?;
+ assert_eq!(ptr.get().detect_inverted_marker()?, true);
- // documented default value is 8, but actually it's 4
- // see issue: https://github.com/opencv/opencv_contrib/issues/2420
- assert_eq!(params.perspective_remove_pixel_per_cell()?, 4);
- params.set_perspective_remove_pixel_per_cell(5)?;
- assert_eq!(params.perspective_remove_pixel_per_cell()?, 5);
+ assert_eq!(plain.perspective_remove_pixel_per_cell()?, ptr.get().perspective_remove_pixel_per_cell()?);
+ ptr.get_mut().set_perspective_remove_pixel_per_cell(5)?;
+ assert_eq!(ptr.get().perspective_remove_pixel_per_cell()?, 5);
+
+ plain.set_adaptive_thresh_constant(123.)?;
+ assert_eq!(123., plain.adaptive_thresh_constant()?);
+ plain.set_adaptive_thresh_constant(87.)?;
+ assert_eq!(87., plain.adaptive_thresh_constant()?);
Ok(())
}

add .get() method to acquire immutable reference to the smart pointer contents