lib.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. #![doc = include_str!("../README.md")]
  2. #![warn(missing_docs)]
  3. use parking_lot::{
  4. MappedRwLockReadGuard, MappedRwLockWriteGuard, Mutex, RwLock, RwLockReadGuard, RwLockWriteGuard,
  5. };
  6. use std::{
  7. cell::{Ref, RefCell, RefMut},
  8. fmt::Debug,
  9. marker::PhantomData,
  10. ops::{Deref, DerefMut},
  11. sync::{atomic::AtomicU32, Arc, OnceLock},
  12. };
  13. /// # Example
  14. ///
  15. /// ```compile_fail
  16. /// let data = String::from("hello world");
  17. /// let owner = UnsyncStorage::owner();
  18. /// let key = owner.insert(&data);
  19. /// drop(data);
  20. /// assert_eq!(*key.read(), "hello world");
  21. /// ```
  22. #[allow(unused)]
  23. fn compile_fail() {}
  24. #[test]
  25. fn reused() {
  26. let first_ptr;
  27. {
  28. let owner = UnsyncStorage::owner();
  29. first_ptr = owner.insert(1).raw.data.data_ptr();
  30. drop(owner);
  31. }
  32. {
  33. let owner = UnsyncStorage::owner();
  34. let second_ptr = owner.insert(1234).raw.data.data_ptr();
  35. assert_eq!(first_ptr, second_ptr);
  36. drop(owner);
  37. }
  38. }
  39. #[test]
  40. fn leaking_is_ok() {
  41. let data = String::from("hello world");
  42. let key;
  43. {
  44. // create an owner
  45. let owner = UnsyncStorage::owner();
  46. // insert data into the store
  47. key = owner.insert(data);
  48. // don't drop the owner
  49. std::mem::forget(owner);
  50. }
  51. assert_eq!(key.try_read().as_deref(), Some(&"hello world".to_string()));
  52. }
  53. #[test]
  54. fn drops() {
  55. let data = String::from("hello world");
  56. let key;
  57. {
  58. // create an owner
  59. let owner = UnsyncStorage::owner();
  60. // insert data into the store
  61. key = owner.insert(data);
  62. // drop the owner
  63. }
  64. assert!(key.try_read().is_none());
  65. }
  66. #[test]
  67. fn works() {
  68. let owner = UnsyncStorage::owner();
  69. let key = owner.insert(1);
  70. assert_eq!(*key.read(), 1);
  71. }
  72. #[test]
  73. fn insert_while_reading() {
  74. let owner = UnsyncStorage::owner();
  75. let key;
  76. {
  77. let data: String = "hello world".to_string();
  78. key = owner.insert(data);
  79. }
  80. let value = key.read();
  81. owner.insert(&1);
  82. assert_eq!(*value, "hello world");
  83. }
  84. #[test]
  85. #[should_panic]
  86. fn panics() {
  87. let owner = UnsyncStorage::owner();
  88. let key = owner.insert(1);
  89. drop(owner);
  90. assert_eq!(*key.read(), 1);
  91. }
  92. #[test]
  93. fn fuzz() {
  94. fn maybe_owner_scope(
  95. valid_keys: &mut Vec<GenerationalBox<String>>,
  96. invalid_keys: &mut Vec<GenerationalBox<String>>,
  97. path: &mut Vec<u8>,
  98. ) {
  99. let branch_cutoff = 5;
  100. let children = if path.len() < branch_cutoff {
  101. rand::random::<u8>() % 4
  102. } else {
  103. rand::random::<u8>() % 2
  104. };
  105. for i in 0..children {
  106. let owner = UnsyncStorage::owner();
  107. let key = owner.insert(format!("hello world {path:?}"));
  108. valid_keys.push(key);
  109. path.push(i);
  110. // read all keys
  111. println!("{:?}", path);
  112. for key in valid_keys.iter() {
  113. let value = key.read();
  114. println!("{:?}", value);
  115. assert!(value.starts_with("hello world"));
  116. }
  117. #[cfg(any(debug_assertions, feature = "check_generation"))]
  118. for key in invalid_keys.iter() {
  119. assert!(!key.validate());
  120. }
  121. maybe_owner_scope(valid_keys, invalid_keys, path);
  122. invalid_keys.push(valid_keys.pop().unwrap());
  123. path.pop();
  124. }
  125. }
  126. for _ in 0..10 {
  127. maybe_owner_scope(&mut Vec::new(), &mut Vec::new(), &mut Vec::new());
  128. }
  129. }
  130. /// The type erased id of a generational box.
  131. #[derive(Clone, Copy, PartialEq, Eq, Hash)]
  132. pub struct GenerationalBoxId {
  133. data_ptr: *const (),
  134. #[cfg(any(debug_assertions, feature = "check_generation"))]
  135. generation: u32,
  136. }
  137. // Safety: GenerationalBoxId is Send and Sync because there is no way to access the pointer.
  138. unsafe impl Send for GenerationalBoxId {}
  139. unsafe impl Sync for GenerationalBoxId {}
  140. impl Debug for GenerationalBoxId {
  141. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  142. #[cfg(any(debug_assertions, feature = "check_generation"))]
  143. f.write_fmt(format_args!("{:?}@{:?}", self.data_ptr, self.generation))?;
  144. #[cfg(not(any(debug_assertions, feature = "check_generation")))]
  145. f.write_fmt(format_args!("{:?}", self.data_ptr))?;
  146. Ok(())
  147. }
  148. }
  149. /// The core Copy state type. The generational box will be dropped when the [Owner] is dropped.
  150. pub struct GenerationalBox<T, S = UnsyncStorage> {
  151. raw: MemoryLocation<S>,
  152. #[cfg(any(debug_assertions, feature = "check_generation"))]
  153. generation: u32,
  154. _marker: PhantomData<T>,
  155. }
  156. impl<T: 'static, S: AnyStorage> Debug for GenerationalBox<T, S> {
  157. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  158. #[cfg(any(debug_assertions, feature = "check_generation"))]
  159. f.write_fmt(format_args!(
  160. "{:?}@{:?}",
  161. self.raw.data.data_ptr(),
  162. self.generation
  163. ))?;
  164. #[cfg(not(any(debug_assertions, feature = "check_generation")))]
  165. f.write_fmt(format_args!("{:?}", self.raw.data.as_ptr()))?;
  166. Ok(())
  167. }
  168. }
  169. impl<T: 'static, S: Storage<T>> GenerationalBox<T, S> {
  170. #[inline(always)]
  171. fn validate(&self) -> bool {
  172. #[cfg(any(debug_assertions, feature = "check_generation"))]
  173. {
  174. self.raw
  175. .generation
  176. .load(std::sync::atomic::Ordering::Relaxed)
  177. == self.generation
  178. }
  179. #[cfg(not(any(debug_assertions, feature = "check_generation")))]
  180. {
  181. true
  182. }
  183. }
  184. /// Get the id of the generational box.
  185. pub fn id(&self) -> GenerationalBoxId {
  186. GenerationalBoxId {
  187. data_ptr: self.raw.data.data_ptr(),
  188. #[cfg(any(debug_assertions, feature = "check_generation"))]
  189. generation: self.generation,
  190. }
  191. }
  192. /// Try to read the value. Returns None if the value is no longer valid.
  193. pub fn try_read(&self) -> Option<S::Ref> {
  194. self.validate().then(|| self.raw.data.try_read()).flatten()
  195. }
  196. /// Read the value. Panics if the value is no longer valid.
  197. pub fn read(&self) -> S::Ref {
  198. self.try_read().unwrap()
  199. }
  200. /// Try to write the value. Returns None if the value is no longer valid.
  201. pub fn try_write(&self) -> Option<S::Mut> where {
  202. self.validate().then(|| self.raw.data.try_write()).flatten()
  203. }
  204. /// Write the value. Panics if the value is no longer valid.
  205. pub fn write(&self) -> S::Mut {
  206. self.try_write().unwrap()
  207. }
  208. /// Set the value. Panics if the value is no longer valid.
  209. pub fn set(&self, value: T) {
  210. self.validate().then(|| {
  211. self.raw.data.set(value);
  212. });
  213. }
  214. /// Returns true if the pointer is equal to the other pointer.
  215. pub fn ptr_eq(&self, other: &Self) -> bool {
  216. #[cfg(any(debug_assertions, feature = "check_generation"))]
  217. {
  218. self.raw.data.data_ptr() == other.raw.data.data_ptr()
  219. && self.generation == other.generation
  220. }
  221. #[cfg(not(any(debug_assertions, feature = "check_generation")))]
  222. {
  223. self.raw.data.as_ptr() == other.raw.data.as_ptr()
  224. }
  225. }
  226. }
  227. impl<T, S: Copy> Copy for GenerationalBox<T, S> {}
  228. impl<T, S: Copy> Clone for GenerationalBox<T, S> {
  229. fn clone(&self) -> Self {
  230. *self
  231. }
  232. }
  233. #[derive(Clone, Copy)]
  234. pub struct UnsyncStorage(&'static RefCell<Option<Box<dyn std::any::Any>>>);
  235. impl Default for UnsyncStorage {
  236. fn default() -> Self {
  237. Self(Box::leak(Box::new(RefCell::new(None))))
  238. }
  239. }
  240. #[derive(Clone, Copy)]
  241. pub struct SyncStorage(&'static RwLock<Option<Box<dyn std::any::Any + Send + Sync>>>);
  242. impl Default for SyncStorage {
  243. fn default() -> Self {
  244. Self(Box::leak(Box::new(RwLock::new(None))))
  245. }
  246. }
  247. pub trait Mappable<T>: Deref<Target = T> {
  248. type Mapped<U: 'static>: Mappable<U> + Deref<Target = U>;
  249. fn map<U: 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U>;
  250. fn try_map<U: 'static>(
  251. _self: Self,
  252. f: impl FnOnce(&T) -> Option<&U>,
  253. ) -> Option<Self::Mapped<U>>;
  254. }
  255. impl<T> Mappable<T> for Ref<'static, T> {
  256. type Mapped<U: 'static> = Ref<'static, U>;
  257. fn map<U: 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
  258. Ref::map(_self, f)
  259. }
  260. fn try_map<U: 'static>(
  261. _self: Self,
  262. f: impl FnOnce(&T) -> Option<&U>,
  263. ) -> Option<Self::Mapped<U>> {
  264. Ref::filter_map(_self, f).ok()
  265. }
  266. }
  267. impl<T> Mappable<T> for MappedRwLockReadGuard<'static, T> {
  268. type Mapped<U: 'static> = MappedRwLockReadGuard<'static, U>;
  269. fn map<U: 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
  270. MappedRwLockReadGuard::map(_self, f)
  271. }
  272. fn try_map<U: 'static>(
  273. _self: Self,
  274. f: impl FnOnce(&T) -> Option<&U>,
  275. ) -> Option<Self::Mapped<U>> {
  276. MappedRwLockReadGuard::try_map(_self, f).ok()
  277. }
  278. }
  279. pub trait MappableMut<T>: DerefMut<Target = T> {
  280. type Mapped<U: 'static>: MappableMut<U> + DerefMut<Target = U>;
  281. fn map<U: 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U>;
  282. fn try_map<U: 'static>(
  283. _self: Self,
  284. f: impl FnOnce(&mut T) -> Option<&mut U>,
  285. ) -> Option<Self::Mapped<U>>;
  286. }
  287. impl<T> MappableMut<T> for RefMut<'static, T> {
  288. type Mapped<U: 'static> = RefMut<'static, U>;
  289. fn map<U: 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U> {
  290. RefMut::map(_self, f)
  291. }
  292. fn try_map<U: 'static>(
  293. _self: Self,
  294. f: impl FnOnce(&mut T) -> Option<&mut U>,
  295. ) -> Option<Self::Mapped<U>> {
  296. RefMut::try_map(_self, f)
  297. }
  298. }
  299. impl<T> MappableMut<T> for MappedRwLockWriteGuard<'static, T> {
  300. type Mapped<U: 'static> = MappedRwLockWriteGuard<'static, U>;
  301. fn map<U: 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U> {
  302. MappedRwLockWriteGuard::map(_self, f)
  303. }
  304. fn try_map<U: 'static>(
  305. _self: Self,
  306. f: impl FnOnce(&mut T) -> Option<&mut U>,
  307. ) -> Option<Self::Mapped<U>> {
  308. MappedRwLockWriteGuard::try_map(_self, f).ok()
  309. }
  310. }
  311. pub trait Storage<Data>: Copy + AnyStorage + 'static {
  312. type Ref: Mappable<Data> + Deref<Target = Data>;
  313. type Mut: MappableMut<Data> + DerefMut<Target = Data>;
  314. fn try_read(&self) -> Option<Self::Ref>;
  315. fn read(&self) -> Self::Ref {
  316. self.try_read()
  317. .expect("generational box has been invalidated or the type has changed")
  318. }
  319. fn try_write(&self) -> Option<Self::Mut>;
  320. fn write(&self) -> Self::Mut {
  321. self.try_write()
  322. .expect("generational box has been invalidated or the type has changed")
  323. }
  324. fn set(&self, value: Data);
  325. }
  326. pub trait AnyStorage: Default {
  327. fn data_ptr(&self) -> *const ();
  328. fn take(&self) -> bool;
  329. fn recycle(location: &MemoryLocation<Self>);
  330. // {
  331. // location.drop();
  332. // self.recycled.lock().push(location);
  333. // }
  334. fn claim() -> MemoryLocation<Self>;
  335. // where
  336. // S: Default,
  337. // {
  338. // if let Some(location) = self.recycled.lock().pop() {
  339. // location
  340. // } else {
  341. // MemoryLocation {
  342. // data: Default::default(),
  343. // #[cfg(any(debug_assertions, feature = "check_generation"))]
  344. // generation: Box::leak(Box::new(Default::default())),
  345. // }
  346. // }
  347. // }
  348. /// Create a new owner. The owner will be responsible for dropping all of the generational boxes that it creates.
  349. fn owner() -> Owner<Self> {
  350. Owner {
  351. owned: Default::default(),
  352. phantom: PhantomData,
  353. }
  354. }
  355. }
  356. impl<T: 'static> Storage<T> for UnsyncStorage {
  357. type Ref = Ref<'static, T>;
  358. type Mut = RefMut<'static, T>;
  359. fn try_read(&self) -> Option<Self::Ref> {
  360. Ref::filter_map(self.0.borrow(), |any| any.as_ref()?.downcast_ref()).ok()
  361. }
  362. fn try_write(&self) -> Option<Self::Mut> {
  363. RefMut::filter_map(self.0.borrow_mut(), |any| any.as_mut()?.downcast_mut()).ok()
  364. }
  365. fn set(&self, value: T) {
  366. *self.0.borrow_mut() = Some(Box::new(value));
  367. }
  368. }
  369. thread_local! {
  370. static UNSYNC_RUNTIME: RefCell<Vec<MemoryLocation<UnsyncStorage>>> = RefCell::new(Vec::new());
  371. }
  372. impl AnyStorage for UnsyncStorage {
  373. fn data_ptr(&self) -> *const () {
  374. self.0.as_ptr() as *const ()
  375. }
  376. fn take(&self) -> bool {
  377. self.0.borrow_mut().take().is_some()
  378. }
  379. fn claim() -> MemoryLocation<Self> {
  380. UNSYNC_RUNTIME.with(|runtime| {
  381. if let Some(location) = runtime.borrow_mut().pop() {
  382. location
  383. } else {
  384. MemoryLocation {
  385. data: UnsyncStorage(Box::leak(Box::new(RefCell::new(None)))),
  386. #[cfg(any(debug_assertions, feature = "check_generation"))]
  387. generation: Box::leak(Box::new(Default::default())),
  388. }
  389. }
  390. })
  391. }
  392. fn recycle(location: &MemoryLocation<Self>) {
  393. location.drop();
  394. UNSYNC_RUNTIME.with(|runtime| runtime.borrow_mut().push(*location));
  395. }
  396. }
  397. impl<T: Sync + Send + 'static> Storage<T> for SyncStorage {
  398. type Ref = MappedRwLockReadGuard<'static, T>;
  399. type Mut = MappedRwLockWriteGuard<'static, T>;
  400. fn try_read(&self) -> Option<Self::Ref> {
  401. RwLockReadGuard::try_map(self.0.read(), |any| any.as_ref()?.downcast_ref()).ok()
  402. }
  403. fn try_write(&self) -> Option<Self::Mut> {
  404. RwLockWriteGuard::try_map(self.0.write(), |any| any.as_mut()?.downcast_mut()).ok()
  405. }
  406. fn set(&self, value: T) {
  407. *self.0.write() = Some(Box::new(value));
  408. }
  409. }
  410. static SYNC_RUNTIME: OnceLock<Arc<Mutex<Vec<MemoryLocation<SyncStorage>>>>> = OnceLock::new();
  411. fn sync_runtime() -> &'static Arc<Mutex<Vec<MemoryLocation<SyncStorage>>>> {
  412. SYNC_RUNTIME.get_or_init(|| Arc::new(Mutex::new(Vec::new())))
  413. }
  414. impl AnyStorage for SyncStorage {
  415. fn data_ptr(&self) -> *const () {
  416. self.0.data_ptr() as *const ()
  417. }
  418. fn take(&self) -> bool {
  419. self.0.write().take().is_some()
  420. }
  421. fn claim() -> MemoryLocation<Self> {
  422. MemoryLocation {
  423. data: SyncStorage(Box::leak(Box::new(RwLock::new(None)))),
  424. #[cfg(any(debug_assertions, feature = "check_generation"))]
  425. generation: Box::leak(Box::new(Default::default())),
  426. }
  427. }
  428. fn recycle(location: &MemoryLocation<Self>) {
  429. location.drop();
  430. sync_runtime().lock().push(*location);
  431. }
  432. }
  433. #[derive(Clone, Copy)]
  434. struct MemoryLocation<S = UnsyncStorage> {
  435. data: S,
  436. #[cfg(any(debug_assertions, feature = "check_generation"))]
  437. generation: &'static AtomicU32,
  438. }
  439. impl<S> MemoryLocation<S> {
  440. #[allow(unused)]
  441. fn drop(&self)
  442. where
  443. S: AnyStorage,
  444. {
  445. let old = self.data.take();
  446. #[cfg(any(debug_assertions, feature = "check_generation"))]
  447. if old {
  448. let new_generation = self.generation.load(std::sync::atomic::Ordering::Relaxed) + 1;
  449. self.generation
  450. .store(new_generation, std::sync::atomic::Ordering::Relaxed);
  451. }
  452. }
  453. fn replace<T: 'static>(&mut self, value: T) -> GenerationalBox<T, S>
  454. where
  455. S: Storage<T> + Copy,
  456. {
  457. self.data.set(value);
  458. GenerationalBox {
  459. raw: *self,
  460. #[cfg(any(debug_assertions, feature = "check_generation"))]
  461. generation: self.generation.load(std::sync::atomic::Ordering::Relaxed),
  462. _marker: PhantomData,
  463. }
  464. }
  465. }
  466. /// Owner: Handles dropping generational boxes. The owner acts like a runtime lifetime guard. Any states that you create with an owner will be dropped when that owner is dropped.
  467. pub struct Owner<S: AnyStorage = UnsyncStorage> {
  468. owned: Arc<Mutex<Vec<MemoryLocation<S>>>>,
  469. phantom: PhantomData<S>,
  470. }
  471. impl<S: AnyStorage + Copy> Owner<S> {
  472. /// Insert a value into the store. The value will be dropped when the owner is dropped.
  473. pub fn insert<T: 'static>(&self, value: T) -> GenerationalBox<T, S>
  474. where
  475. S: Storage<T>,
  476. {
  477. let mut location = S::claim();
  478. let key = location.replace(value);
  479. self.owned.lock().push(location);
  480. key
  481. }
  482. /// Creates an invalid handle. This is useful for creating a handle that will be filled in later. If you use this before the value is filled in, you will get may get a panic or an out of date value.
  483. pub fn invalid<T: 'static>(&self) -> GenerationalBox<T, S> {
  484. let location = S::claim();
  485. GenerationalBox {
  486. raw: location,
  487. #[cfg(any(debug_assertions, feature = "check_generation"))]
  488. generation: location
  489. .generation
  490. .load(std::sync::atomic::Ordering::Relaxed),
  491. _marker: PhantomData,
  492. }
  493. }
  494. }
  495. impl<S: AnyStorage> Drop for Owner<S> {
  496. fn drop(&mut self) {
  497. for location in self.owned.lock().iter() {
  498. S::recycle(location)
  499. }
  500. }
  501. }