open_entry_bindings/
extension_data.rs1use std::{collections::HashMap, sync::{Arc, atomic::Ordering}};
2
3use tokio::sync::{Mutex, OwnedMutexGuard};
4
5use crate::EXTENSION_ID;
6
7pub struct ExtensionData(Arc<Mutex<HashMap<u32, usize>>>);
8
9impl ExtensionData {
10 #[inline(always)] async fn lock(&self) -> OwnedMutexGuard<HashMap<u32, usize>> {
11 unsafe { crate::ExtensionData__lock.unwrap_unchecked()(self).await }
12 }
13
14 pub async unsafe fn set<T>(&self, data: T) {
16 let arc = Arc::into_raw(Arc::new(data)) as usize;
17
18 Arc::increment_strong_count(arc as *const T);
19
20 if let Some(bef) = self.lock().await.insert(EXTENSION_ID.load(Ordering::Relaxed), arc) {
21 Arc::decrement_strong_count(bef as *const T);
22 }
23 }
24
25 pub async unsafe fn get<T>(&self) -> Option<Arc<T>> {
27 Some(Arc::from_raw(*self.lock().await.get(&EXTENSION_ID.load(Ordering::Relaxed))? as *const T).clone())
28 }
29}