Skip to main content

datex_core/shared_values/shared_container/
common.rs

1use crate::{
2    shared_values::{
3        SharedContainer, SharedContainerInner, SharedContainerMutability,
4        SharedContainerOwnership,
5        base_shared_value_container::BaseSharedValueContainer,
6        traits::SharedContainerCommon,
7    },
8    types::type_definition::TypeDefinition,
9    values::value_container::ValueContainer,
10};
11use core::cell::{Ref, RefMut};
12
13use crate::shared_values::base_shared_value_container::observers::ObserverData;
14
15impl SharedContainerCommon for SharedContainer {
16    /// Get the [SharedContainerMutability] of the inner container.
17    fn container_mutability(&self) -> SharedContainerMutability {
18        match self {
19            SharedContainer::Owned(owned) => owned.container_mutability(),
20            SharedContainer::Referenced(referenced) => {
21                referenced.container_mutability()
22            }
23        }
24    }
25
26    /// Gets a [Ref] to the currently assigned [ValueContainer] of the shared container (not resolved recursively)
27    fn value_container(&self) -> Ref<'_, ValueContainer> {
28        match self {
29            SharedContainer::Owned(owned) => owned.value_container(),
30            SharedContainer::Referenced(referenced) => {
31                referenced.value_container()
32            }
33        }
34    }
35
36    /// Gets a [RefMut] to the currently assigned [ValueContainer] of the shared container (not resolved recursively)
37    fn value_container_mut(&self) -> RefMut<'_, ValueContainer> {
38        match self {
39            SharedContainer::Owned(owned) => owned.value_container_mut(),
40            SharedContainer::Referenced(referenced) => {
41                referenced.value_container_mut()
42            }
43        }
44    }
45
46    /// Gets a [Ref] to the currently assigned allowed [TypeDefinition] of the shared container (not resolved recursively)
47    fn allowed_type(&self) -> Ref<'_, TypeDefinition> {
48        match self {
49            SharedContainer::Owned(owned) => owned.allowed_type(),
50            SharedContainer::Referenced(referenced) => {
51                referenced.allowed_type()
52            }
53        }
54    }
55
56    fn is_borrowed(&self) -> bool {
57        match self {
58            SharedContainer::Owned(owned) => owned.is_borrowed(),
59            SharedContainer::Referenced(referenced) => referenced.is_borrowed(),
60        }
61    }
62
63    /// Checks if the shared container can be mutated by the local endpoint
64    fn can_mutate(&self) -> bool {
65        match self {
66            SharedContainer::Owned(owned) => owned.can_mutate(),
67            SharedContainer::Referenced(referenced) => referenced.can_mutate(),
68        }
69    }
70
71    fn inner(&self) -> Ref<'_, SharedContainerInner> {
72        match self {
73            SharedContainer::Owned(owned) => owned.inner(),
74            SharedContainer::Referenced(referenced) => referenced.inner(),
75        }
76    }
77
78    fn inner_mut(&self) -> RefMut<'_, SharedContainerInner> {
79        match self {
80            SharedContainer::Owned(owned) => owned.inner_mut(),
81            SharedContainer::Referenced(referenced) => referenced.inner_mut(),
82        }
83    }
84
85    /// Gets a [Ref] to the currently assigned [BaseSharedValueContainer] of the shared container (not resolved recursively)
86    fn base_shared_container(&self) -> Ref<'_, BaseSharedValueContainer> {
87        match self {
88            SharedContainer::Owned(owned) => owned.base_shared_container(),
89            SharedContainer::Referenced(referenced) => {
90                referenced.base_shared_container()
91            }
92        }
93    }
94    /// Gets a [RefMut] to the currently assigned [BaseSharedValueContainer] of the shared container (not resolved recursively)
95    fn base_shared_container_mut(
96        &self,
97    ) -> RefMut<'_, BaseSharedValueContainer> {
98        match self {
99            SharedContainer::Owned(owned) => owned.base_shared_container_mut(),
100            SharedContainer::Referenced(referenced) => {
101                referenced.base_shared_container_mut()
102            }
103        }
104    }
105
106    /// Returns the [SharedContainerOwnership] of this shared container
107    fn ownership(&self) -> SharedContainerOwnership {
108        match self {
109            SharedContainer::Owned(owned) => owned.ownership(),
110            SharedContainer::Referenced(referenced) => referenced.ownership(),
111        }
112    }
113
114    fn observer_data(&self) -> Ref<'_, ObserverData> {
115        match self {
116            SharedContainer::Owned(owned) => owned.observer_data(),
117            SharedContainer::Referenced(referenced) => {
118                referenced.observer_data()
119            }
120        }
121    }
122    fn observer_data_mut(&self) -> RefMut<'_, ObserverData> {
123        match self {
124            SharedContainer::Owned(owned) => owned.observer_data_mut(),
125            SharedContainer::Referenced(referenced) => {
126                referenced.observer_data_mut()
127            }
128        }
129    }
130
131    fn is_uninitialized(&self) -> bool {
132        match self {
133            SharedContainer::Owned(owned) => owned.is_uninitialized(),
134            SharedContainer::Referenced(referenced) => {
135                referenced.is_uninitialized()
136            }
137        }
138    }
139}