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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
mod clone_unsafe;
mod common;
pub mod convert_parts;
mod convert_value_container;
pub mod get_datex_type;
pub mod serde_dif;
#[cfg(feature = "ast")]
mod to_datex_expression_data;
mod to_instructions;
use crate::{
prelude::*,
runtime::{
cache::shared_references_cache::SharedReferencesCache,
pointer_address_provider::SelfOwnedPointerAddressProvider,
},
shared_values::{
ReferencedSharedContainer, RemotePointerAddress,
SelfOwnedPointerAddress, SelfOwnedSharedContainer,
SharedContainerInner, SharedContainerMutability,
base_shared_value_container::{
BaseSharedValueContainer, observers::ObserverData,
},
errors::UnexpectedImmutableSharedContainerError,
traits::{_ExposeRcInternal, SharedContainerCommon},
},
traits::{
identity::Identity, structural_eq::StructuralEq, value_eq::ValueEq,
},
utils::impl_display_for_datex_value::impl_display_for_datex_value,
values::{
core_values::native::DatexNative, value_container::ValueContainer,
},
};
use alloc::rc::Rc;
use core::{
cell::{Ref, RefCell, RefMut},
hash::{Hash, Hasher},
mem,
};
/// Wrapper struct for an owned shared value (i.e. `shared X`)
/// It is guaranteed that the inner value is a [SharedContainerInner::EndpointOwned].
///
/// ([OwnedSharedContainer] implies [SharedContainerInner::EndpointOwned], but not vice versa,
/// since a [SharedContainerInner::EndpointOwned] can be wrapped in a [ReferencedSharedContainer])
///
/// When holding an [OwnedSharedContainer], it is guaranteed that the contained [SharedContainerInner] is
/// not moved and changed to [SharedContainerInner::External].
/// Only an [OwnedSharedContainer] can be moved to another endpoint or location.
#[derive(Debug)]
pub struct OwnedSharedContainer {
/// It is guaranteed that the inner value is a [SharedContainerInner::EndpointOwned].
inner: Rc<RefCell<SharedContainerInner>>,
/// This reflects the container mutability of the inner container, which is guaranteed to stay the same
container_mutability: SharedContainerMutability,
/// Observer data (e.g. observer list) for this shared container. Can be borrowed separately from the [SharedContainerInner]
observer_data: Rc<RefCell<ObserverData>>,
/// Flag indicating that the inner value is uninitialized. This must get reset to false when the inner value is updated.
/// When set to true, an update to the value is allowed even if the reference is immutable, since the value is not yet initialized.
/// TODO: better way to handle uninitialized values, maybe don't store on top level, but this is more efficient since we don't have to deref the inner value
pub(super) is_uninitialized: bool,
}
impl OwnedSharedContainer {
/// Creates a new owned container from an [SelfOwnedSharedContainer]
pub fn new_from_self_owned_container(
container: SelfOwnedSharedContainer,
) -> Self {
let container_mutability = *container.value().mutability();
OwnedSharedContainer {
inner: Rc::new(RefCell::new(SharedContainerInner::EndpointOwned(
container,
))),
container_mutability,
observer_data: Rc::new(RefCell::new(ObserverData::default())),
is_uninitialized: false,
}
}
/// Creates a new [OwnedSharedContainer] with an initial [ValueContainer],
/// a [SharedContainerMutability], and an [SelfOwnedPointerAddress].
///
/// The allowed type is inferred from the value_container's allowed type.
pub fn new_with_inferred_allowed_type<T: Into<ValueContainer>>(
value_container: T,
mutability: SharedContainerMutability,
address_provider: &mut SelfOwnedPointerAddressProvider,
) -> Self {
// Note: address provider guarantees new unique address
unsafe {
Self::new_with_inferred_allowed_type_unsafe(
value_container.into(),
mutability,
address_provider.get_new_self_owned_address(),
)
}
}
/// Creates a new [OwnedSharedContainer] containing an initial [DatexNative] value.
/// The allowed type is the type of the [DatexNative].
pub fn new_native<T: DatexNative>(
value: T,
mutability: SharedContainerMutability,
address_provider: &mut SelfOwnedPointerAddressProvider,
cache: &mut SharedReferencesCache,
) -> Self {
let ty = value.allowed_type(cache);
let value_container = value.to_value_container();
let is_uninitialized = value_container.is_uninitialized();
// SAFETY: address provider guarantees new unique address
unsafe {
Self::new_from_base_shared_container_unsafe(
BaseSharedValueContainer::try_new(
value_container,
ty,
mutability,
)
.unwrap(), // the unwrap is safe since the type of the native value is guaranteed to match the value
address_provider.get_new_self_owned_address(),
is_uninitialized,
)
}
}
/// Creates a new [OwnedSharedContainer] with an initial [ValueContainer],
/// a [SharedContainerMutability], and an [SelfOwnedPointerAddress].
///
/// The allowed type is inferred from the value_container's allowed type.
/// # Safety
/// The caller must ensure that the address is not used anywhere else.
pub unsafe fn new_with_inferred_allowed_type_unsafe(
value_container: ValueContainer,
mutability: SharedContainerMutability,
address: SelfOwnedPointerAddress,
) -> Self {
let is_uninitialized = value_container.is_uninitialized();
unsafe {
Self::new_from_base_shared_container_unsafe(
BaseSharedValueContainer::new_with_inferred_allowed_type(
value_container,
mutability,
),
address,
is_uninitialized,
)
}
}
/// Creates a new [OwnedSharedContainer] from a [BaseSharedValueContainer] and a [SelfOwnedPointerAddress].
/// # Safety
/// The caller must ensure that the address is not used anywhere else.
unsafe fn new_from_base_shared_container_unsafe(
base_shared_container: BaseSharedValueContainer,
address: SelfOwnedPointerAddress,
is_uninitialized: bool,
) -> Self {
let mut container =
OwnedSharedContainer::new_from_self_owned_container(unsafe {
SelfOwnedSharedContainer::new_with_address(
base_shared_container,
address,
)
});
if is_uninitialized {
container.mark_uninitialized();
}
container
}
/// Creates a new [OwnedSharedContainer] from parts.
///
/// # Safety
/// This function should only be called if you can guarantee that
/// no other [OwnedSharedContainer] exist that are using the same inner value.
pub unsafe fn new_unchecked(
container_mutability: SharedContainerMutability,
inner: Rc<RefCell<SharedContainerInner>>,
observer_data: Rc<RefCell<ObserverData>>,
) -> Self {
OwnedSharedContainer {
inner,
container_mutability,
observer_data,
is_uninitialized: false,
}
}
/// Get a [Ref] to the inner [SelfOwnedSharedContainer].
/// It is guaranteed that the contained [SharedContainerInner] is always a [SharedContainerInner::EndpointOwned].
pub fn as_self_owned_shared_container(
&self,
) -> Ref<'_, SelfOwnedSharedContainer> {
Ref::map(self.inner(), |inner| match inner {
SharedContainerInner::EndpointOwned(inner) => inner,
_ => unreachable!(
"OwnedSharedContainer must contain an EndpointOwned inner value"
),
})
}
/// Get a [RefMut] to the inner [SelfOwnedSharedContainer].
/// It is guaranteed that the contained [SharedContainerInner] is always a [SharedContainerInner::EndpointOwned].
pub fn as_self_owned_shared_container_mut(
&self,
) -> RefMut<'_, SelfOwnedSharedContainer> {
RefMut::map(self.inner_mut(), |inner| match inner {
SharedContainerInner::EndpointOwned(inner) => inner,
_ => unreachable!(
"OwnedSharedContainer must contain an EndpointOwned inner value"
),
})
}
/// Get a [Ref] to the inner [SelfOwnedPointerAddress].
/// It is guaranteed that the pointer address is always a [SelfOwnedPointerAddress].
pub fn pointer_address(&self) -> Ref<'_, SelfOwnedPointerAddress> {
Ref::map(self.as_self_owned_shared_container(), |inner| {
inner.address()
})
}
/// Creates a new immutable [ReferencedSharedContainer] pointing to the same inner value as this [OwnedSharedContainer].
pub fn derive_immutable_reference(&self) -> ReferencedSharedContainer {
ReferencedSharedContainer::new_immutable(
self.inner.clone(),
self.observer_data.clone(),
)
}
/// Tries to create a new mutable [ReferencedSharedContainer] pointing to the same inner value as this [OwnedSharedContainer].
/// Returns an [Err] if the container itself is not mutable
pub fn try_derive_mutable_reference(
&self,
) -> Result<
ReferencedSharedContainer,
UnexpectedImmutableSharedContainerError,
> {
if self.container_mutability() != SharedContainerMutability::Mutable {
return Err(UnexpectedImmutableSharedContainerError);
}
// new_mutable_unchecked is safe to call here since we checked the container mutability before
Ok(ReferencedSharedContainer::new_mutable_unchecked(
self.inner.clone(),
self.observer_data.clone(),
))
}
/// Clones the shared container as a mutable reference if possible, otherwise as an immutable reference
pub fn derive_with_max_mutability(&self) -> ReferencedSharedContainer {
self.try_derive_mutable_reference()
.unwrap_or_else(|_| self.derive_immutable_reference())
}
/// Clones the shared container as a mutable reference if possible,
/// otherwise as an immutable reference, and sets the move indicator on the cloned reference
pub fn clone_with_move_indicator(&self) -> ReferencedSharedContainer {
let mut reference = self.derive_with_max_mutability();
unsafe {
// SAFETY: since this is an OwnedSharedContainer, it always has an owned address
reference.set_move_indicator();
}
reference
}
/// Moves an owned shared container by converting it to a [ReferencedSharedContainer] with a [RemotePointerAddress] pointing to the given remote address.
/// Drops the original owned shared container
/// # Safety
/// The caller must ensure that the [RemotePointerAddress] does not yet exist in the [SharedReferencesCache]
pub unsafe fn move_to_remote(self, remote_address: RemotePointerAddress) {
let mut inner = self.inner_mut();
// replace previous with null value
// FIXME: find a more efficient way to do this enum variant swap
let previous = mem::replace(
&mut *inner,
SharedContainerInner::EndpointOwned(unsafe {
SelfOwnedSharedContainer::new_with_address(
BaseSharedValueContainer::null(),
SelfOwnedPointerAddress([0; 5]),
)
}),
);
*inner = match previous {
SharedContainerInner::EndpointOwned(owned) => {
SharedContainerInner::External(unsafe {
owned.convert_to_external_container(remote_address)
})
}
_ => unreachable!(
"OwnedSharedContainer must contain an EndpointOwned inner value"
),
};
}
pub fn to_string_omit_content(&self) -> String {
"(...)".to_string()
}
/// Marks the shared container as uninitialized.
pub(crate) fn mark_uninitialized(&mut self) {
self.is_uninitialized = true;
}
/// Unmarks the shared container as uninitialized.
pub(crate) fn unset_uninitialized(&mut self) {
self.is_uninitialized = false;
}
}
impl_display_for_datex_value!(
OwnedSharedContainer,
impl core::fmt::Display for OwnedSharedContainer {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_self_owned_shared_container().value())
}
}
);
impl _ExposeRcInternal for OwnedSharedContainer {
type Shared = SharedContainerInner;
fn get_rc_internal(&self) -> &Rc<RefCell<Self::Shared>> {
&self.inner
}
}
impl Identity for OwnedSharedContainer {
fn identical(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.inner, &other.inner)
}
}
impl Eq for OwnedSharedContainer {}
/// PartialEq corresponds to pointer equality / identity for `Reference`.
impl PartialEq for OwnedSharedContainer {
fn eq(&self, other: &Self) -> bool {
self.identical(other)
}
}
impl Hash for OwnedSharedContainer {
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 OwnedSharedContainer {
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 OwnedSharedContainer {
fn value_eq(&self, other: &Self) -> bool {
self.inner()
.base_shared_container()
.value_container()
.value_eq(other.inner().base_shared_container().value_container())
}
}
impl From<SelfOwnedSharedContainer> for OwnedSharedContainer {
fn from(container: SelfOwnedSharedContainer) -> Self {
OwnedSharedContainer::new_from_self_owned_container(container)
}
}