Skip to main content

datex_core/shared_values/
external_shared_container.rs

1use crate::{
2    runtime::cache::shared_references_cache::SharedReferencesCache,
3    shared_values::{
4        PointerAddress, RemotePointerAddress,
5        base_shared_value_container::BaseSharedValueContainer,
6    },
7};
8
9/// A shared container with an external pointer
10#[derive(Debug)]
11pub struct ExternalSharedContainer {
12    value: BaseSharedValueContainer,
13    /// Address of the remote pointer, can be a remote or builtin pointer address
14    address: RemotePointerAddress,
15}
16
17impl ExternalSharedContainer {
18    /// Create a new [ExternalSharedContainer] with a given [RemotePointerAddress].
19    /// # Safety
20    /// The caller must ensure that the [RemotePointerAddress] does not yet exist in the [SharedReferencesCache]
21    pub unsafe fn create_external_shared_container(
22        shared_value_container: BaseSharedValueContainer,
23        address: RemotePointerAddress,
24        memory: &SharedReferencesCache,
25    ) -> ExternalSharedContainer {
26        if memory.has_reference(&PointerAddress::Remote(address.clone())) {
27            panic!(
28                "Cannot create ExternalSharedContainer with address that already exists in memory"
29            );
30        }
31
32        ExternalSharedContainer {
33            value: shared_value_container,
34            address,
35        }
36    }
37    pub fn value(&self) -> &BaseSharedValueContainer {
38        &self.value
39    }
40
41    pub fn value_mut(&mut self) -> &mut BaseSharedValueContainer {
42        &mut self.value
43    }
44
45    pub fn take_value(self) -> BaseSharedValueContainer {
46        self.value
47    }
48
49    pub fn address(&self) -> &RemotePointerAddress {
50        &self.address
51    }
52}