datex-core 0.0.14

The DATEX Core Rust implementation
Documentation
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! This module contains the implementation of the shared container, which is the holder of [SharedContainerInner]
//! and the top-level wrapper for any owned or referenced shared container.
use crate::{
    runtime::pointer_address_provider::SelfOwnedPointerAddressProvider,
    shared_values::{
        OwnedSharedContainer, PointerAddress, ReferencedSharedContainer,
        SelfOwnedPointerAddress, SharedContainerInner,
        SharedContainerMutability, SharedContainerOwnership,
        errors::{
            UnexpectedImmutableReferenceError,
            UnexpectedSharedContainerOwnershipError,
        },
        traits::_ExposeRcInternal,
    },
    values::value_container::ValueContainer,
};
pub mod identity;
pub(crate) mod to_instructions;
use crate::{
    prelude::*,
    runtime::cache::shared_references_cache::SharedReferencesCache,
    shared_values::{
        ReferenceMutability,
        base_shared_value_container::observers::{
            ObserveOptions, Observer, ObserverCallback, ObserverError,
            ObserverId,
        },
        collapsed_container_value::{
            CollapsedContainerValue, CollapsedContainerValueMut,
        },
        shared_mut::SharedMut,
        traits::SharedContainerCommon,
    },
    traits::convert_value_container::ConvertValueContainer,
    types::type_definition::TypeDefinition,
    value_updates::update_handler::{
        InternalMutabilityUpdateHandler, UpdateCallbackData,
    },
    values::{core_value::CoreValue, core_values::native::DatexNative},
};
use alloc::rc::Rc;
use core::{
    cell::{Ref, RefCell, RefMut},
    fmt::{Debug, Display, Formatter},
    mem,
    ops::Deref,
};

pub mod apply;
pub mod serde_dif;
/// Top-level wrapper for any owned or referenced shared container,
/// which can either be an owned shared container or a reference to a shared container.
pub enum SharedContainer {
    /// An owned shared container (`shared X`). This is always points to a [SharedContainerInner::EndpointOwned]
    Owned(OwnedSharedContainer),
    /// A referenced shared container (`'shared X` or `'mut shared X`).
    /// This can point to either a [SharedContainerInner::EndpointOwned] or a [SharedContainerInner::External]
    Referenced(ReferencedSharedContainer),
}

impl Debug for SharedContainer {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        // recursive reference
        if self.is_borrowed() {
            f.write_str("(...)")
        } else {
            match self {
                SharedContainer::Owned(owned) => {
                    f.debug_tuple("SharedContainer").field(owned).finish()
                }
                SharedContainer::Referenced(reference) => {
                    f.debug_tuple("SharedContainer").field(reference).finish()
                }
            }
        }
    }
}

impl SharedContainer {
    /// Creates a new owned [SharedContainer] with an initial [ValueContainer],
    /// a [SharedContainerMutability], and a [SelfOwnedPointerAddressProvider].
    ///
    /// The allowed type is inferred from the value_container's allowed type.
    pub fn new_owned_with_inferred_allowed_type<T: Into<ValueContainer>>(
        value_container: T,
        mutability: SharedContainerMutability,
        address_provider: &mut SelfOwnedPointerAddressProvider,
    ) -> Self {
        SharedContainer::Owned(
            OwnedSharedContainer::new_with_inferred_allowed_type(
                value_container,
                mutability,
                address_provider,
            ),
        )
    }

    /// Creates a new owned [SharedContainer] 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 {
        SharedContainer::Owned(OwnedSharedContainer::new_native(
            value,
            mutability,
            address_provider,
            cache,
        ))
    }

    /// Creates a new owned [SharedContainer] with an initial [ValueContainer],
    /// a [SharedContainerMutability], and a [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_owned_with_inferred_allowed_type_unsafe<
        T: Into<ValueContainer>,
    >(
        value_container: T,
        mutability: SharedContainerMutability,
        address: SelfOwnedPointerAddress,
    ) -> Self {
        SharedContainer::Owned(unsafe {
            OwnedSharedContainer::new_with_inferred_allowed_type_unsafe(
                value_container.into(),
                mutability,
                address,
            )
        })
    }

    /// Ensures that the shared container is mutable and returns it.
    /// Returns an ObserverError if the reference is immutable (or a type container).
    fn ensure_mutable_container(&self) -> Result<(), ObserverError> {
        if self.container_mutability() != SharedContainerMutability::Mutable {
            return Err(ObserverError::ImmutableValue);
        }
        Ok(())
    }

    /// Adds an observer to this shared container that will be notified on value changes.
    pub fn observe(
        &self,
        observer: Observer,
    ) -> Result<ObserverId, ObserverError> {
        self.ensure_mutable_container()?;
        let res = self.observer_data_mut().observe(observer)?;
        self.ensure_local_nested_observe_callbacks();

        Ok(res)
    }

    pub fn unobserve(
        &self,
        observer_id: ObserverId,
    ) -> Result<(), ObserverError> {
        self.ensure_mutable_container()?;
        self.observer_data_mut().unobserve(observer_id)?;

        // also disable local nested observe callbacks if there are no more observers registered
        if !self.observer_data().has_observers() {
            self.disable_local_nested_observe_callbacks();
        }

        Ok(())
    }

    /// Updates the options for an existing observer by its ID.
    /// Returns an error if the observer ID is not found or the reference is immutable.
    pub fn update_observer_options(
        &self,
        observer_id: ObserverId,
        options: ObserveOptions,
    ) -> Result<(), ObserverError> {
        self.ensure_mutable_container()?;
        self.observer_data_mut()
            .update_observer_options(observer_id, options)
    }

    // Enables observe callbacks for the inner local value if not yet enabled
    fn ensure_local_nested_observe_callbacks(&self) {
        let enabled = if !self.observer_data().get_local_observers_enabled()
            && let ValueContainer::Local(local_value) =
                self.base_shared_container_mut().value_container_mut()
        {
            let self_clone = self.clone();

            let callback: ObserverCallback = Rc::new(move |update| {
                // TODO: check if not already borrowed?
                self_clone.observer_data_mut().call_observers(update);
            });
            local_value.set_update_callback_data(Some(UpdateCallbackData {
                callback,
                path: vec![],
            }));
            true
        } else {
            false
        };

        if enabled {
            self.observer_data_mut().set_local_observers_enabled(true);
        }
    }

    fn disable_local_nested_observe_callbacks(&self) {
        let mut base = self.base_shared_container_mut();
        if self.observer_data().get_local_observers_enabled()
            && let ValueContainer::Local(local_value) =
                base.value_container_mut()
        {
            local_value.set_update_callback_data(None);
        }
        self.observer_data_mut().set_local_observers_enabled(false);
    }

    /// Gets the current actual [TypeDefinition] of the collapsed inner [Value]
    pub fn actual_type(&self) -> TypeDefinition {
        let value = self.collapsed_value();
        value.borrow().actual_type()
    }

    pub fn collapsed_value(&self) -> CollapsedContainerValue<'_> {
        CollapsedContainerValue::new_shared(self.get_collapsed_rc())
    }

    pub fn collapsed_value_mut(&self) -> CollapsedContainerValueMut<'_> {
        CollapsedContainerValueMut::new_shared(self.get_collapsed_rc())
    }

    /// Returns the Rc<RefCell<SharedContainerInner>> of the most inner local value of the shared container.
    fn get_collapsed_rc(&self) -> Rc<RefCell<SharedContainerInner>> {
        // TODO: no clone of RC?
        // collapse nested direct shared children until most inner local value is found
        let mut inner_rc = self.get_rc_internal().clone();

        loop {
            let next_rc = {
                let borrowed = inner_rc.borrow();

                match borrowed.base_shared_container().value_container() {
                    ValueContainer::Shared(shared) => {
                        Some(shared.get_rc_internal().clone())
                    }
                    _ => None,
                }
            };

            match next_rc {
                Some(rc) => inner_rc = rc,
                None => break,
            }
        }

        inner_rc
    }

    pub fn pointer_address(&self) -> PointerAddress {
        match self {
            SharedContainer::Owned(owned) => {
                PointerAddress::SelfOwned(owned.pointer_address().clone())
            }
            SharedContainer::Referenced(referenced) => {
                referenced.pointer_address()
            }
        }
    }

    /// Returns true if the shared container has a self owned pointer address
    pub fn is_self_owned(&self) -> bool {
        match self {
            SharedContainer::Owned(_owned) => true,
            SharedContainer::Referenced(referenced) => {
                matches!(
                    referenced.inner().deref(),
                    SharedContainerInner::EndpointOwned(_)
                )
            }
        }
    }

    /// Creates a new immutable [ReferencedSharedContainer] pointing to the same inner value as self.
    pub fn derive_immutable_reference(&self) -> ReferencedSharedContainer {
        match self {
            SharedContainer::Owned(owned) => owned.derive_immutable_reference(),
            SharedContainer::Referenced(referenced) => {
                referenced.derive_immutable_reference()
            }
        }
    }
    /// Tries to create a new mutable or immutable [ReferencedSharedContainer] pointing to the same inner value as this [OwnedSharedContainer].
    /// Returns an [Err] if the requested mutability is [ReferenceMutability::Mutable],
    /// but the current reference_mutability is [ReferenceMutability::Immutable] or the container itself is not mutable
    pub fn try_derive_reference_with_mutability(
        &self,
        mutability: ReferenceMutability,
    ) -> Result<ReferencedSharedContainer, UnexpectedImmutableReferenceError>
    {
        match mutability {
            ReferenceMutability::Immutable => {
                Ok(self.derive_immutable_reference())
            }
            ReferenceMutability::Mutable => self.try_derive_mutable_reference(),
        }
    }

    /// Tries to create a new mutable [ReferencedSharedContainer] pointing to the same inner value as this [OwnedSharedContainer].
    /// Returns an [Err] if the current reference_mutability is [ReferenceMutability::Immutable] or the container itself is not mutable
    pub fn try_derive_mutable_reference(
        &self,
    ) -> Result<ReferencedSharedContainer, UnexpectedImmutableReferenceError>
    {
        match self {
            SharedContainer::Owned(owned) => owned
                .try_derive_mutable_reference()
                .map_err(|_| UnexpectedImmutableReferenceError),
            SharedContainer::Referenced(referenced) => {
                referenced.try_derive_mutable_reference()
            }
        }
    }

    /// Returns the owned shared container if it is owned, otherwise returns an error.
    pub fn try_get_owned(
        &self,
    ) -> Result<&OwnedSharedContainer, UnexpectedSharedContainerOwnershipError>
    {
        match self {
            SharedContainer::Owned(owned) => Ok(owned),
            SharedContainer::Referenced(reference) => {
                Err(UnexpectedSharedContainerOwnershipError {
                    actual: SharedContainerOwnership::Referenced(
                        reference.reference_mutability(),
                    ),
                    expected: SharedContainerOwnership::Owned,
                })
            }
        }
    }

    /// Clones the shared container as a mutable reference if possible, otherwise as an immutable reference
    pub fn derive_reference_with_max_mutability(
        &self,
    ) -> ReferencedSharedContainer {
        self.try_derive_mutable_reference()
            .unwrap_or_else(|_| self.derive_immutable_reference())
    }

    /// Downgrades an owned shared container to a referenced shared container.
    /// If the shared container is already a referenced shared container, it will just be returned.
    /// If the shared container is owned, it will be replaced with a new referenced shared container pointing to the same inner value
    /// and the original owned shared container will be returned.
    /// Also sets the move indicator on the original owned shared container, so that we know it should be treated as moved.
    pub fn downgrade_to_reference(&mut self) -> SharedContainer {
        match self {
            SharedContainer::Owned(_) => {
                // replace previous with null value
                // FIXME: find a more efficient way to do this enum variant swap
                let previous = mem::replace(&mut *self, unsafe {
                    SharedContainer::new_owned_with_inferred_allowed_type_unsafe(
                        CoreValue::Null,
                        SharedContainerMutability::Immutable,
                        SelfOwnedPointerAddress::new([0; 5]),
                    )
                });

                // create a new referenced shared container and assign to self
                *self = previous.clone_with_move_indicator_if_owned();

                // return original, potentially owned shared container
                previous
            }
            SharedContainer::Referenced(referenced) => {
                SharedContainer::Referenced(referenced.clone())
            }
        }
    }

    /// Tries to upgrade an inner [ReferencedSharedContainer] to an [OwnedSharedContainer].
    /// If the container is already an [OwnedSharedContainer], it will be returned as is.
    /// If the move indicator is set, the inner value is moved into a new [OwnedSharedContainer] and returned.
    /// If the move indicator is not set, an [Err] is returned with the original [ReferencedSharedContainer].
    /// Panics if the move indicator is set but the address is not a [SelfOwnedPointerAddress].
    ///
    /// # Safety
    /// The caller must ensure that no other [OwnedSharedContainer] for the same inner value exists
    /// if move_indicator of the [ReferencedSharedContainer] is set.
    pub unsafe fn try_upgrade_to_owned(
        self,
    ) -> Result<OwnedSharedContainer, ReferencedSharedContainer> {
        match self {
            SharedContainer::Referenced(referenced_container) => unsafe {
                referenced_container.try_upgrade_to_owned()
            },
            SharedContainer::Owned(owned_container) => Ok(owned_container),
        }
    }

    /// Clones the shared container with the move indicator if it is an [OwnedSharedContainer],
    /// otherwise as a normal reference
    pub fn clone_with_move_indicator_if_owned(&self) -> SharedContainer {
        SharedContainer::Referenced(match self {
            SharedContainer::Owned(owned) => owned.clone_with_move_indicator(),
            SharedContainer::Referenced(referenced) => referenced.clone(),
        })
    }

    /// Returns true if the shared container is an [OwnedSharedContainer] or a [ReferencedSharedContainer] that is marked as moved.
    pub fn treat_as_move(&self) -> bool {
        match self {
            SharedContainer::Owned(_owned) => true,
            SharedContainer::Referenced(referenced) => {
                referenced.treat_as_move()
            }
        }
    }

    pub fn to_string_omit_content(&self) -> String {
        match self {
            SharedContainer::Owned(owned) => owned.to_string_omit_content(),
            SharedContainer::Referenced(referenced) => {
                referenced.to_string_omit_content()
            }
        }
    }

    /// Tries to get an immutable reference to the value as a specified type.
    /// Does not perform any type conversion.
    /// This only works for local values, not for shared values.
    pub fn try_as<T>(&self) -> Option<Ref<'_, T>>
    where
        T: ConvertValueContainer,
    {
        Ref::filter_map(self.value_container(), |value| value.try_as::<T>())
            .ok()
    }

    /// Tries to get a mutable reference to the value as a specified type.
    /// Does not perform any type conversion.
    /// This only works for local values, not for shared values.
    pub fn try_as_mut<T>(&self) -> Option<SharedMut<'_, T>>
    where
        T: ConvertValueContainer,
    {
        RefMut::filter_map(self.value_container_mut(), |value| {
            value.try_as_mut::<T>()
        })
        .ok()
        .map(|v| SharedMut::new(v, self))
    }

    /// This method is called when a borrow of the inner shared container is dropped.
    pub fn notify_borrow_dropped(&self) {
        // TODO: this could be used in the future to trigger queued updates only after a borrow was dropped
    }

    /// Marks the shared container as uninitialized.
    #[allow(unused)]
    pub(crate) fn mark_uninitialized(&mut self) {
        match self {
            SharedContainer::Owned(owned) => owned.mark_uninitialized(),
            SharedContainer::Referenced(referenced) => {
                referenced.mark_uninitialized()
            }
        }
    }

    /// Unmarks the shared container as uninitialized.
    pub(crate) fn unset_uninitialized(&mut self) {
        match self {
            SharedContainer::Owned(owned) => owned.unset_uninitialized(),
            SharedContainer::Referenced(referenced) => {
                referenced.unset_uninitialized()
            }
        }
    }

    pub fn mark_debug(&self, is_debug: bool) {
        self.base_shared_container_mut().mark_debug(is_debug);
    }

    pub fn is_debug(&self) -> bool {
        self.base_shared_container().is_debug()
    }
}

/// Custom clone implementation for [SharedContainer].
/// A [SharedContainer::Owned] cannot be cloned as is, only a new reference can be created
/// A [SharedContainer::Referenced] can be cloned normally
impl Clone for SharedContainer {
    fn clone(&self) -> Self {
        match self {
            // An owned container cannot be cloned, only a new reference can be created
            SharedContainer::Owned(owned) => {
                SharedContainer::Referenced(owned.derive_with_max_mutability())
            }
            // A referenced container can be cloned
            SharedContainer::Referenced(referenced) => {
                SharedContainer::Referenced(referenced.clone())
            }
        }
    }
}

// TODO: use impl_display_for_datex_value
impl Display for SharedContainer {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match self {
            SharedContainer::Owned(owned) => write!(f, "{}", owned),
            SharedContainer::Referenced(referenced) => {
                write!(f, "{}", referenced)
            }
        }
    }
}

pub mod clone_unsafe;
mod common;
mod convert_value_container;
pub mod equality;
pub mod get_datex_type;
pub mod update_handler;

impl From<OwnedSharedContainer> for SharedContainer {
    fn from(value: OwnedSharedContainer) -> Self {
        SharedContainer::Owned(value)
    }
}

impl From<ReferencedSharedContainer> for SharedContainer {
    fn from(value: ReferencedSharedContainer) -> Self {
        SharedContainer::Referenced(value)
    }
}

impl _ExposeRcInternal for SharedContainer {
    type Shared = SharedContainerInner;
    fn get_rc_internal(&self) -> &Rc<RefCell<Self::Shared>> {
        match self {
            SharedContainer::Owned(owned) => owned.get_rc_internal(),
            SharedContainer::Referenced(referenced) => {
                referenced.get_rc_internal()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use core::assert_matches;

    #[test]
    fn immutable_reference_observe_fails() {
        let address_provider = &mut SelfOwnedPointerAddressProvider::default();
        let shared = SharedContainer::new_owned_with_inferred_allowed_type(
            42,
            SharedContainerMutability::Immutable,
            address_provider,
        );
        assert_matches!(
            shared.observe(Observer::new(|_| {})),
            Err(ObserverError::ImmutableValue)
        );

        let r = SharedContainer::new_owned_with_inferred_allowed_type(
            42,
            SharedContainerMutability::Mutable,
            address_provider,
        );
        assert_matches!(r.observe(Observer::new(|_| {})), Ok(_));
    }
}