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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
pub mod datex_proxy;
use crate::{
runtime::cache::shared_references_cache::SharedReferencesCache,
shared_values::{
ExternalSharedContainer, PointerAddress, ReferenceMutability,
RemotePointerAddress, SharedContainerInner, SharedContainerMutability,
base_shared_value_container::BaseSharedValueContainer,
errors::{SharedValueCreationError, UnexpectedImmutableReferenceError},
internal_traits::_ExposeRcInternal,
},
traits::{
identity::Identity, structural_eq::StructuralEq, value_eq::ValueEq,
},
types::type_definition::TypeDefinition,
values::{value::Value, value_container::ValueContainer},
};
use alloc::rc::Rc;
use core::{
cell::{Ref, RefCell, RefMut},
fmt::Display,
hash::{Hash, Hasher},
};
/// Wrapper struct for a reference to a shared value (i.e. `'shared X` or `'mut shared X`).
///
/// The inner value can either be a [SharedContainerInner::EndpointOwned] or [SharedContainerInner::External]
#[derive(Debug, Clone)]
pub struct ReferencedSharedContainer {
/// The inner container contains the actual value which can be shared between multiple owners.
/// This can either be a [SharedContainerInner::EndpointOwned] or a [SharedContainerInner::External]
inner: Rc<RefCell<SharedContainerInner>>,
/// The mutability of the reference (either `'mut shared X` or `'shared X`)
reference_mutability: ReferenceMutability,
}
impl ReferencedSharedContainer {
/// Creates a new mutable [ReferencedSharedContainer] from an existing mutable [Rc<RefCell<SharedContainerInner>>]
///
/// IMPORTANT: this method should only be called after validating that
/// the [SharedContainerMutability] of the inner container is mutable.
pub(crate) fn new_mutable_unchecked(
inner: Rc<RefCell<SharedContainerInner>>,
) -> Self {
ReferencedSharedContainer {
inner,
reference_mutability: ReferenceMutability::Mutable,
}
}
/// Creates a new immutable [ReferencedSharedContainer] from an existing mutable or immutable [Rc<RefCell<SharedContainerInner>>]
pub(crate) fn new_immutable(
inner: Rc<RefCell<SharedContainerInner>>,
) -> Self {
ReferencedSharedContainer {
inner,
reference_mutability: ReferenceMutability::Immutable,
}
}
/// Tries to create a new immutable [ReferencedSharedContainer] containing a [SharedContainerInner::External]
/// Returns an [Err] if the provided [ReferenceMutability] is [ReferenceMutability::Mutable] while
/// the [SharedContainerMutability] of the container is [SharedContainerMutability::Immutable]
/// # Safety
/// The caller must ensure that the [RemotePointerAddress] does not yet exist in the [SharedReferencesCache]
pub(crate) unsafe fn try_new_external_from_base_container(
container: BaseSharedValueContainer,
address: RemotePointerAddress,
reference_mutability: ReferenceMutability,
memory: &SharedReferencesCache,
) -> Result<Self, ()> {
// invalid reference mutability
if reference_mutability == ReferenceMutability::Mutable
&& *container.mutability() == SharedContainerMutability::Immutable
{
return Err(());
}
Ok(ReferencedSharedContainer {
inner: Rc::new(RefCell::new(SharedContainerInner::External(
unsafe {
ExternalSharedContainer::create_external_shared_container(
container, address, memory,
)
},
))),
reference_mutability,
})
}
/// Creates a new immutable [ReferencedSharedContainer] containing a [SharedContainerInner::External]
/// with the provided [ValueContainer] and [RemotePointerAddress].
/// Automatically infers the allowed type of the shared container from the provided [ValueContainer].
/// # Safety
/// The caller must ensure that the [RemotePointerAddress] does not yet exist in the [SharedReferencesCache]
pub(crate) unsafe fn new_immutable_external_with_inferred_allowed_type(
value_container: ValueContainer,
address: RemotePointerAddress,
memory: &SharedReferencesCache,
) -> Self {
unsafe {
ReferencedSharedContainer::try_new_external_from_base_container(
BaseSharedValueContainer::new_with_inferred_allowed_type(
value_container,
SharedContainerMutability::Immutable,
),
address,
ReferenceMutability::Immutable,
memory,
)
.unwrap()
}
}
/// Tries to create a new immutable [ReferencedSharedContainer] containing a [SharedContainerInner::External]
/// with the provided [ValueContainer] and [RemotePointerAddress].
///
/// Sets the provided [SharedContainerMutability] and allowed [TypeDefinition].
/// If the allowed [TypeDefinition] is not a superset of the [ValueContainer]'s allowed type,
/// an error is returned
/// # Safety
/// The caller must ensure that the [RemotePointerAddress] does not yet exist in the [SharedReferencesCache]
pub(crate) unsafe fn try_new_external(
value_container: ValueContainer,
address: RemotePointerAddress,
mutability: SharedContainerMutability,
allowed_type: TypeDefinition,
memory: &SharedReferencesCache,
) -> Result<Self, SharedValueCreationError> {
Ok(unsafe {
ReferencedSharedContainer::try_new_external_from_base_container(
BaseSharedValueContainer::try_new(
value_container,
allowed_type,
mutability,
)?,
address,
ReferenceMutability::Immutable,
memory,
)
.unwrap()
})
}
pub fn inner(&self) -> Ref<'_, SharedContainerInner> {
self.inner.borrow()
}
pub fn inner_mut(&self) -> RefMut<'_, SharedContainerInner> {
self.inner.borrow_mut()
}
/// Gets a [Ref] to the currently assigned [BaseSharedValueContainer] of the shared container (not resolved recursively)
pub fn base_shared_container(&self) -> Ref<'_, BaseSharedValueContainer> {
Ref::map(self.inner(), |inner| inner.base_shared_container())
}
/// Gets a [RefMut] to the currently assigned [BaseSharedValueContainer] of the shared container (not resolved recursively)
pub fn base_shared_container_mut(
&self,
) -> RefMut<'_, BaseSharedValueContainer> {
RefMut::map(self.inner_mut(), |inner| inner.base_shared_container_mut())
}
/// Gets a [Ref] to the currently assigned [ValueContainer] of the shared container (not resolved recursively)
pub fn value_container(&self) -> Ref<'_, ValueContainer> {
Ref::map(self.base_shared_container(), |base_shared_container| {
base_shared_container.value_container()
})
}
/// Gets a [RefMut] to the currently assigned [ValueContainer] of the shared container (not resolved recursively)
pub fn value_container_mut(&self) -> RefMut<'_, ValueContainer> {
RefMut::map(self.base_shared_container_mut(), |base_shared_container| {
base_shared_container.value_container_mut()
})
}
/// Calls the provided callback with a mut reference to the recursively collapsed inner value of the shared container
pub fn with_collapsed_value_mut<R>(
&self,
f: impl FnOnce(&mut Value) -> R,
) -> R {
self.inner_mut()
.base_shared_container_mut()
.with_collapsed_value_mut(f)
}
/// Calls the provided callback with a reference to the recursively collapsed inner value of the shared container
pub fn with_collapsed_value<R>(&self, f: impl FnOnce(&Value) -> R) -> R {
self.inner().base_shared_container().with_collapsed_value(f)
}
/// Gets a [Ref] to the currently assigned allowed [TypeDefinition] of the shared container (not resolved recursively)
pub fn allowed_type(&self) -> Ref<'_, TypeDefinition> {
Ref::map(self.base_shared_container(), |base_shared_container| {
base_shared_container.allowed_type()
})
}
/// Get the inner [PointerAddress].
pub fn pointer_address(&self) -> PointerAddress {
self.inner().pointer_address()
}
/// Get the [SharedContainerMutability] of the inner [SelfOwnedSharedContainer].
pub fn container_mutability(&self) -> SharedContainerMutability {
*self.inner().base_shared_container().mutability()
}
/// Creates a new immutable [ReferencedSharedContainer] pointing to the same inner value as self.
pub fn derive_immutable_reference(&self) -> ReferencedSharedContainer {
ReferencedSharedContainer {
inner: self.inner.clone(),
reference_mutability: ReferenceMutability::Immutable,
}
}
/// Tries to create a new mutable [ReferencedSharedContainer] pointing to the same inner value as self.
/// Returns an [Err] if the current reference_mutability is [ReferenceMutability::Immutable]
pub fn try_derive_mutable_reference(
&self,
) -> Result<ReferencedSharedContainer, UnexpectedImmutableReferenceError>
{
match self.reference_mutability {
ReferenceMutability::Immutable => {
Err(UnexpectedImmutableReferenceError)
}
ReferenceMutability::Mutable => Ok(self.clone()),
}
}
/// Checks if the reference can be mutated by the local endpoint
pub(crate) fn can_mutate(&self) -> bool {
self.reference_mutability == ReferenceMutability::Mutable
}
/// Returns the [ReferenceMutability] of this reference
pub fn reference_mutability(&self) -> ReferenceMutability {
self.reference_mutability
}
}
impl Display for ReferencedSharedContainer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{}{}",
match self.reference_mutability {
ReferenceMutability::Immutable => "'",
ReferenceMutability::Mutable => "'mut ",
},
self.inner().base_shared_container(),
)
}
}
impl _ExposeRcInternal for ReferencedSharedContainer {
type Shared = SharedContainerInner;
fn get_rc_internal(&self) -> &Rc<RefCell<Self::Shared>> {
&self.inner
}
}
impl Identity for ReferencedSharedContainer {
fn identical(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.inner, &other.inner)
}
}
impl Eq for ReferencedSharedContainer {}
/// PartialEq corresponds to pointer equality / identity for `Reference`.
impl PartialEq for ReferencedSharedContainer {
fn eq(&self, other: &Self) -> bool {
self.identical(other)
}
}
impl Hash for ReferencedSharedContainer {
fn hash<H: Hasher>(&self, state: &mut H) {
let ptr = Rc::as_ptr(self.get_rc_internal());
ptr.hash(state); // hash the address
}
}
impl StructuralEq for ReferencedSharedContainer {
fn structural_eq(&self, other: &Self) -> bool {
self.inner()
.base_shared_container()
.value_container()
.structural_eq(
other.inner().base_shared_container().value_container(),
)
}
}
impl ValueEq for ReferencedSharedContainer {
fn value_eq(&self, other: &Self) -> bool {
self.inner()
.base_shared_container()
.value_container()
.value_eq(other.inner().base_shared_container().value_container())
}
}