shipyard/unique/
thread_local.rs

1use crate::borrow::{NonSend, NonSendSync, NonSync};
2use crate::memory_usage::StorageMemoryUsage;
3use crate::storage::{SBoxBuilder, Storage};
4use crate::tracking::TrackingTimestamp;
5use crate::unique::{Unique, UniqueStorage};
6use core::any::type_name;
7
8impl<T: Unique + Sync> Storage for NonSend<UniqueStorage<T>> {
9    fn memory_usage(&self) -> Option<StorageMemoryUsage> {
10        Some(StorageMemoryUsage {
11            storage_name: type_name::<Self>().into(),
12            allocated_memory_bytes: size_of::<Self>(),
13            used_memory_bytes: size_of::<Self>(),
14            component_count: 1,
15        })
16    }
17
18    fn is_empty(&self) -> bool {
19        false
20    }
21
22    fn clear_all_inserted(&mut self, current: TrackingTimestamp) {
23        self.insert = current;
24    }
25
26    fn clear_all_modified(&mut self, current: TrackingTimestamp) {
27        self.modification = current;
28    }
29
30    fn try_clone(&self, other_current: TrackingTimestamp) -> Option<SBoxBuilder> {
31        self.clone.map(|clone| {
32            SBoxBuilder::new(NonSend(UniqueStorage {
33                value: clone(&self.value),
34                insert: other_current,
35                modification: TrackingTimestamp::origin(),
36                last_insert: TrackingTimestamp::origin(),
37                last_modification: TrackingTimestamp::origin(),
38                clone: None,
39            }))
40        })
41    }
42}
43
44impl<T: Unique + Send> Storage for NonSync<UniqueStorage<T>> {
45    fn memory_usage(&self) -> Option<StorageMemoryUsage> {
46        Some(StorageMemoryUsage {
47            storage_name: type_name::<Self>().into(),
48            allocated_memory_bytes: size_of::<Self>(),
49            used_memory_bytes: size_of::<Self>(),
50            component_count: 1,
51        })
52    }
53
54    fn is_empty(&self) -> bool {
55        false
56    }
57
58    fn clear_all_inserted(&mut self, current: TrackingTimestamp) {
59        self.insert = current;
60    }
61
62    fn clear_all_modified(&mut self, current: TrackingTimestamp) {
63        self.modification = current;
64    }
65
66    fn try_clone(&self, other_current: TrackingTimestamp) -> Option<SBoxBuilder> {
67        self.clone.map(|clone| {
68            SBoxBuilder::new(NonSync(UniqueStorage {
69                value: clone(&self.value),
70                insert: other_current,
71                modification: TrackingTimestamp::origin(),
72                last_insert: TrackingTimestamp::origin(),
73                last_modification: TrackingTimestamp::origin(),
74                clone: None,
75            }))
76        })
77    }
78}
79
80impl<T: Unique> Storage for NonSendSync<UniqueStorage<T>> {
81    fn memory_usage(&self) -> Option<StorageMemoryUsage> {
82        Some(StorageMemoryUsage {
83            storage_name: type_name::<Self>().into(),
84            allocated_memory_bytes: size_of::<Self>(),
85            used_memory_bytes: size_of::<Self>(),
86            component_count: 1,
87        })
88    }
89
90    fn is_empty(&self) -> bool {
91        false
92    }
93
94    fn clear_all_inserted(&mut self, current: TrackingTimestamp) {
95        self.insert = current;
96    }
97
98    fn clear_all_modified(&mut self, current: TrackingTimestamp) {
99        self.modification = current;
100    }
101
102    fn try_clone(&self, other_current: TrackingTimestamp) -> Option<SBoxBuilder> {
103        self.clone.map(|clone| {
104            SBoxBuilder::new(NonSendSync(UniqueStorage {
105                value: clone(&self.value),
106                insert: other_current,
107                modification: TrackingTimestamp::origin(),
108                last_insert: TrackingTimestamp::origin(),
109                last_modification: TrackingTimestamp::origin(),
110                clone: None,
111            }))
112        })
113    }
114}