lib.rs 17 KB

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