-
-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathboxed_ref.rs
188 lines (159 loc) · 5.03 KB
/
boxed_ref.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::fmt;
use std::marker::PhantomData;
use crate::mod_prelude::OpenCVTypeExternContainer;
use crate::traits::{Boxed, OpenCVFromExtern, OpenCVIntoExternContainer, OpenCVType};
/// Wrapper for the type implementing [Boxed] trait that allows to retain the lifetime of the referenced object.
///
/// This wrapper implements all traits that the underlying type does, but explicitly doesn't implement [Deref](core::ops::Deref) and
/// [DerefMut](core::ops::DerefMut) to avoid being able to [swap](core::mem::swap) the reference out of the wrapper. It relies on
/// functions accepting generic arguments (e.g. `impl MatTrait` or `impl MatTraitConst`) which are implemented by both main struct
/// and its `BoxedRef` (e.g. [Mat](crate::core::Mat) and `BoxedRef<Mat>`).
#[repr(transparent)]
pub struct BoxedRef<'r, T: Boxed> {
pub(crate) reference: T,
referenced_object: PhantomData<&'r T>,
}
impl<T: Boxed> From<T> for BoxedRef<'_, T> {
#[inline]
fn from(value: T) -> Self {
Self {
reference: value,
referenced_object: PhantomData,
}
}
}
impl<T: Boxed + fmt::Debug> fmt::Debug for BoxedRef<'_, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.reference, f)
}
}
impl<T: Boxed + Clone> BoxedRef<'_, T> {
/// Clones the pointee of this [BoxedRef]
#[inline]
pub fn clone_pointee(&self) -> T {
self.reference.clone()
}
}
impl<T: OpenCVIntoExternContainer + Boxed> OpenCVIntoExternContainer for BoxedRef<'_, T> {
type ExternContainer = T::ExternContainer;
#[inline]
fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
self.reference.opencv_into_extern_container_nofail()
}
}
impl<'t, 'b, T: OpenCVType<'t> + Boxed> OpenCVType<'t> for BoxedRef<'b, T> {
type Arg = BoxedRef<'b, T>;
}
impl<T: OpenCVFromExtern + Boxed> OpenCVFromExtern for BoxedRef<'_, T> {
type ExternReceive = T::ExternReceive;
#[inline]
unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
Self {
reference: T::opencv_from_extern(s),
referenced_object: PhantomData,
}
}
}
impl<T: Boxed + OpenCVTypeExternContainer> OpenCVTypeExternContainer for BoxedRef<'_, T> {
type ExternSend = T::ExternSend;
type ExternSendMut = T::ExternSendMut;
#[inline]
fn opencv_as_extern(&self) -> Self::ExternSend {
self.reference.opencv_as_extern()
}
#[inline]
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.reference.opencv_as_extern_mut()
}
}
/// Mutable version of [BoxedRef], implements the traits that take `&mut self`.
#[repr(transparent)]
pub struct BoxedRefMut<'r, T: Boxed> {
pub(crate) reference: T,
referenced_object: PhantomData<&'r mut T>,
}
impl<T: Boxed> From<T> for BoxedRefMut<'_, T> {
#[inline]
fn from(value: T) -> Self {
Self {
reference: value,
referenced_object: PhantomData,
}
}
}
impl<T: Boxed + fmt::Debug> fmt::Debug for BoxedRefMut<'_, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.reference, f)
}
}
impl<T: Boxed + Clone> BoxedRefMut<'_, T> {
/// Clones the pointee of this [BoxedRefMut]
#[inline]
pub fn clone_pointee(&self) -> T {
self.reference.clone()
}
}
impl<'r, T: Boxed> From<BoxedRefMut<'r, T>> for BoxedRef<'r, T> {
/// Irreversibly convert this [BoxedRefMut] into a non-mutable [BoxedRef]
fn from(value: BoxedRefMut<'r, T>) -> Self {
BoxedRef::from(value.reference)
}
}
impl<T: OpenCVIntoExternContainer + Boxed> OpenCVIntoExternContainer for BoxedRefMut<'_, T> {
type ExternContainer = T::ExternContainer;
#[inline]
fn opencv_into_extern_container_nofail(self) -> Self::ExternContainer {
self.reference.opencv_into_extern_container_nofail()
}
}
impl<'t, 'b, T: OpenCVType<'t> + Boxed> OpenCVType<'t> for BoxedRefMut<'b, T> {
type Arg = BoxedRefMut<'b, T>;
}
impl<T: OpenCVFromExtern + Boxed> OpenCVFromExtern for BoxedRefMut<'_, T> {
type ExternReceive = T::ExternReceive;
#[inline]
unsafe fn opencv_from_extern(s: Self::ExternReceive) -> Self {
Self {
reference: T::opencv_from_extern(s),
referenced_object: PhantomData,
}
}
}
impl<T: Boxed + OpenCVTypeExternContainer> OpenCVTypeExternContainer for BoxedRefMut<'_, T> {
type ExternSend = T::ExternSend;
type ExternSendMut = T::ExternSendMut;
#[inline]
fn opencv_as_extern(&self) -> Self::ExternSend {
self.reference.opencv_as_extern()
}
#[inline]
fn opencv_as_extern_mut(&mut self) -> Self::ExternSendMut {
self.reference.opencv_as_extern_mut()
}
}
#[doc(hidden)]
#[macro_export]
macro_rules! boxed_ref {
($typ: ty, $const_trait: ty, $const_method: ident, $mut_trait: ty, $mut_method: ident) => {
impl $const_trait for $crate::boxed_ref::BoxedRef<'_, $typ> {
#[inline]
fn $const_method(&self) -> extern_send!(Self) {
self.reference.$const_method()
}
}
impl $const_trait for $crate::boxed_ref::BoxedRefMut<'_, $typ> {
#[inline]
fn $const_method(&self) -> extern_send!(Self) {
self.reference.$const_method()
}
}
impl $mut_trait for $crate::boxed_ref::BoxedRefMut<'_, $typ> {
#[inline]
fn $mut_method(&mut self) -> extern_send!(mut Self) {
self.reference.$mut_method()
}
}
};
}