Skip to main content

datex_core/shared_values/
shared_container_inner.rs

1use crate::shared_values::{
2    ExternalSharedContainer, SelfOwnedPointerAddress, SelfOwnedSharedContainer,
3    base_shared_value_container::BaseSharedValueContainer,
4    pointer_address::PointerAddress,
5};
6use core::mem;
7
8/// Wrapper containing either an [SelfOwnedSharedContainer] or an [ExternalSharedContainer].
9#[derive(Debug)]
10pub enum SharedContainerInner {
11    EndpointOwned(SelfOwnedSharedContainer),
12    External(ExternalSharedContainer),
13}
14
15impl SharedContainerInner {
16    /// Get an immutable reference to the contained value
17    pub fn base_shared_container(&self) -> &BaseSharedValueContainer {
18        match self {
19            SharedContainerInner::EndpointOwned(endpoint_owned) => {
20                endpoint_owned.value()
21            }
22            SharedContainerInner::External(external) => external.value(),
23        }
24    }
25
26    /// Get a mutable reference to the contained value
27    pub fn base_shared_container_mut(
28        &mut self,
29    ) -> &mut BaseSharedValueContainer {
30        match self {
31            SharedContainerInner::EndpointOwned(endpoint_owned) => {
32                endpoint_owned.value_mut()
33            }
34            SharedContainerInner::External(external) => external.value_mut(),
35        }
36    }
37
38    /// Take the contained value out of the container, consuming the container in the process.
39    pub fn take_base_shared_container(self) -> BaseSharedValueContainer {
40        match self {
41            SharedContainerInner::EndpointOwned(owned) => owned.take_value(),
42            SharedContainerInner::External(referenced) => {
43                referenced.take_value()
44            }
45        }
46    }
47
48    /// Get the inner [PointerAddress].
49    pub fn pointer_address(&self) -> PointerAddress {
50        match self {
51            SharedContainerInner::EndpointOwned(endpoint_owned) => {
52                PointerAddress::SelfOwned(endpoint_owned.address().clone())
53            }
54            SharedContainerInner::External(external) => {
55                PointerAddress::Remote(external.address().clone())
56            }
57        }
58    }
59
60    /// Change the inner [PointerAddress] to a new one, potentially changing the type of the container.
61    /// # Safety
62    /// The caller must ensure that the new [PointerAddress] is not already used by another shared container
63    pub(super) unsafe fn change_address(
64        &mut self,
65        new_address: PointerAddress,
66    ) {
67        let previous = mem::replace(
68            &mut *self,
69            SharedContainerInner::EndpointOwned(unsafe {
70                SelfOwnedSharedContainer::new_with_address(
71                    BaseSharedValueContainer::null(),
72                    SelfOwnedPointerAddress([0; 5]),
73                )
74            }),
75        );
76
77        // TODO: handle subscriber switch etc
78        match new_address {
79            PointerAddress::SelfOwned(new_self_owned_address) => {
80                *self = SharedContainerInner::EndpointOwned(unsafe {
81                    SelfOwnedSharedContainer::new_with_address(
82                        previous.take_base_shared_container(),
83                        new_self_owned_address,
84                    )
85                });
86            }
87            PointerAddress::Remote(new_remote_address) => {
88                *self = SharedContainerInner::External(unsafe {
89                    ExternalSharedContainer::new(
90                        previous.take_base_shared_container(),
91                        new_remote_address,
92                    )
93                });
94            }
95        }
96    }
97}