Skip to content

Commit 7de6e6d

Browse files
committed
Add BoxedRefMut::into_const()
1 parent c2ff7dc commit 7de6e6d

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/boxed_ref.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ use crate::traits::{Boxed, OpenCVFromExtern, OpenCVIntoExternContainer, OpenCVTy
66

77
/// Wrapper for the type implementing [Boxed] trait that allows to retain the lifetime of the referenced object.
88
///
9-
/// This wrapper implements all traits that the underlying type does, but explicitly doesn't implement `Deref` and `DerefMut` to
10-
/// avoid being able to `mem::swap` the reference out of the wrapper.
9+
/// This wrapper implements all traits that the underlying type does, but explicitly doesn't implement [Deref](core::ops::Deref) and
10+
/// [DerefMut](core::ops::DerefMut) to avoid being able to [swap](core::mem::swap) the reference out of the wrapper. It relies on
11+
/// functions accepting generic arguments (e.g. `impl MatTrait` or `impl MatTraitConst`) which are implemented by both main struct
12+
/// and its `BoxedRef` (e.g. [Mat](crate::core::Mat) and `BoxedRef<Mat>`).
1113
#[repr(transparent)]
1214
pub struct BoxedRef<'r, T: Boxed> {
1315
pub(crate) reference: T,
@@ -32,7 +34,7 @@ impl<T: Boxed + fmt::Debug> fmt::Debug for BoxedRef<'_, T> {
3234
}
3335

3436
impl<T: Boxed + Clone> BoxedRef<'_, T> {
35-
/// Clones the pointee of this BoxedRef
37+
/// Clones the pointee of this [BoxedRef]
3638
#[inline]
3739
pub fn clone_pointee(&self) -> T {
3840
self.reference.clone()
@@ -104,13 +106,20 @@ impl<T: Boxed + fmt::Debug> fmt::Debug for BoxedRefMut<'_, T> {
104106
}
105107

106108
impl<T: Boxed + Clone> BoxedRefMut<'_, T> {
107-
/// Clones the pointee of this BoxedRef
109+
/// Clones the pointee of this [BoxedRefMut]
108110
#[inline]
109111
pub fn clone_pointee(&self) -> T {
110112
self.reference.clone()
111113
}
112114
}
113115

116+
impl<'r, T: Boxed> From<BoxedRefMut<'r, T>> for BoxedRef<'r, T> {
117+
/// Irreversibly convert this [BoxedRefMut] into a non-mutable [BoxedRef]
118+
fn from(value: BoxedRefMut<'r, T>) -> Self {
119+
BoxedRef::from(value.reference)
120+
}
121+
}
122+
114123
impl<T: OpenCVIntoExternContainer + Boxed> OpenCVIntoExternContainer for BoxedRefMut<'_, T> {
115124
type ExternContainer = T::ExternContainer;
116125

tests/boxed_ref.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,16 @@ fn vector_boxed_ref() -> Result<()> {
122122

123123
Ok(())
124124
}
125+
126+
#[test]
127+
fn boxed_ref_into() -> Result<()> {
128+
let mut s = [1i32, 2, 3, 4];
129+
let mut mut_m = Mat::from_slice_mut(&mut s)?;
130+
*mut_m.at_mut::<i32>(1)? = 100;
131+
let const_m = BoxedRef::<Mat>::from(mut_m);
132+
133+
assert_eq!(100, *const_m.at::<i32>(1)?);
134+
assert_eq!(100, s[1]);
135+
136+
Ok(())
137+
}

0 commit comments

Comments
 (0)