unsync.rs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. use crate::{
  2. entry::{MemoryLocationBorrowInfo, RcStorageEntry, StorageEntry},
  3. error,
  4. references::{GenerationalRef, GenerationalRefMut},
  5. AnyStorage, BorrowError, BorrowMutError, BorrowMutResult, BorrowResult, GenerationalLocation,
  6. GenerationalPointer, Storage, ValueDroppedError,
  7. };
  8. use std::{
  9. any::Any,
  10. cell::{Ref, RefCell, RefMut},
  11. fmt::Debug,
  12. num::NonZeroU64,
  13. };
  14. type RefCellStorageEntryRef = Ref<'static, StorageEntry<RefCellStorageEntryData>>;
  15. type RefCellStorageEntryMut = RefMut<'static, StorageEntry<RefCellStorageEntryData>>;
  16. thread_local! {
  17. static UNSYNC_RUNTIME: RefCell<Vec<&'static UnsyncStorage>> = const { RefCell::new(Vec::new()) };
  18. }
  19. pub(crate) enum RefCellStorageEntryData {
  20. Reference(GenerationalPointer<UnsyncStorage>),
  21. Rc(RcStorageEntry<Box<dyn Any>>),
  22. Data(Box<dyn Any>),
  23. Empty,
  24. }
  25. impl Debug for RefCellStorageEntryData {
  26. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  27. match self {
  28. Self::Reference(pointer) => write!(f, "Reference({:?})", pointer.location),
  29. Self::Rc(_) => write!(f, "Rc"),
  30. Self::Data(_) => write!(f, "Data"),
  31. Self::Empty => write!(f, "Empty"),
  32. }
  33. }
  34. }
  35. impl Default for RefCellStorageEntryData {
  36. fn default() -> Self {
  37. Self::Empty
  38. }
  39. }
  40. /// A unsync storage. This is the default storage type.
  41. #[derive(Default)]
  42. pub struct UnsyncStorage {
  43. borrow_info: MemoryLocationBorrowInfo,
  44. data: RefCell<StorageEntry<RefCellStorageEntryData>>,
  45. }
  46. impl UnsyncStorage {
  47. pub(crate) fn read(
  48. pointer: GenerationalPointer<Self>,
  49. ) -> BorrowResult<Ref<'static, Box<dyn Any>>> {
  50. Self::get_split_ref(pointer).map(|(_, guard)| {
  51. Ref::map(guard, |data| match &data.data {
  52. RefCellStorageEntryData::Data(data) => data,
  53. RefCellStorageEntryData::Rc(data) => &data.data,
  54. _ => unreachable!(),
  55. })
  56. })
  57. }
  58. pub(crate) fn get_split_ref(
  59. mut pointer: GenerationalPointer<Self>,
  60. ) -> BorrowResult<(GenerationalPointer<Self>, RefCellStorageEntryRef)> {
  61. loop {
  62. let borrow = pointer
  63. .storage
  64. .data
  65. .try_borrow()
  66. .map_err(|_| pointer.storage.borrow_info.borrow_error())?;
  67. if !borrow.valid(&pointer.location) {
  68. return Err(BorrowError::Dropped(ValueDroppedError::new_for_location(
  69. pointer.location,
  70. )));
  71. }
  72. match &borrow.data {
  73. // If this is a reference, keep traversing the pointers
  74. RefCellStorageEntryData::Reference(data) => {
  75. pointer = *data;
  76. }
  77. // Otherwise return the value
  78. RefCellStorageEntryData::Rc(_) | RefCellStorageEntryData::Data(_) => {
  79. return Ok((pointer, borrow));
  80. }
  81. RefCellStorageEntryData::Empty => {
  82. return Err(BorrowError::Dropped(ValueDroppedError::new_for_location(
  83. pointer.location,
  84. )));
  85. }
  86. }
  87. }
  88. }
  89. pub(crate) fn write(
  90. pointer: GenerationalPointer<Self>,
  91. ) -> BorrowMutResult<RefMut<'static, Box<dyn Any>>> {
  92. Self::get_split_mut(pointer).map(|(_, guard)| {
  93. RefMut::map(guard, |data| match &mut data.data {
  94. RefCellStorageEntryData::Data(data) => data,
  95. RefCellStorageEntryData::Rc(data) => &mut data.data,
  96. _ => unreachable!(),
  97. })
  98. })
  99. }
  100. pub(crate) fn get_split_mut(
  101. mut pointer: GenerationalPointer<Self>,
  102. ) -> BorrowMutResult<(GenerationalPointer<Self>, RefCellStorageEntryMut)> {
  103. loop {
  104. let borrow = pointer
  105. .storage
  106. .data
  107. .try_borrow_mut()
  108. .map_err(|_| pointer.storage.borrow_info.borrow_mut_error())?;
  109. if !borrow.valid(&pointer.location) {
  110. return Err(BorrowMutError::Dropped(
  111. ValueDroppedError::new_for_location(pointer.location),
  112. ));
  113. }
  114. match &borrow.data {
  115. // If this is a reference, keep traversing the pointers
  116. RefCellStorageEntryData::Reference(data) => {
  117. pointer = *data;
  118. }
  119. // Otherwise return the value
  120. RefCellStorageEntryData::Data(_) | RefCellStorageEntryData::Rc(_) => {
  121. return Ok((pointer, borrow));
  122. }
  123. RefCellStorageEntryData::Empty => {
  124. return Err(BorrowMutError::Dropped(
  125. ValueDroppedError::new_for_location(pointer.location),
  126. ));
  127. }
  128. }
  129. }
  130. }
  131. fn create_new(
  132. value: RefCellStorageEntryData,
  133. #[allow(unused)] caller: &'static std::panic::Location<'static>,
  134. ) -> GenerationalPointer<Self> {
  135. UNSYNC_RUNTIME.with(|runtime| match runtime.borrow_mut().pop() {
  136. Some(storage) => {
  137. let mut write = storage.data.borrow_mut();
  138. let location = GenerationalLocation {
  139. generation: write.generation(),
  140. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  141. created_at: caller,
  142. };
  143. write.data = value;
  144. GenerationalPointer { storage, location }
  145. }
  146. None => {
  147. let storage: &'static Self = &*Box::leak(Box::new(Self {
  148. borrow_info: Default::default(),
  149. data: RefCell::new(StorageEntry::new(value)),
  150. }));
  151. let location = GenerationalLocation {
  152. generation: NonZeroU64::MIN,
  153. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  154. created_at: caller,
  155. };
  156. GenerationalPointer { storage, location }
  157. }
  158. })
  159. }
  160. }
  161. impl AnyStorage for UnsyncStorage {
  162. type Ref<'a, R: ?Sized + 'static> = GenerationalRef<Ref<'a, R>>;
  163. type Mut<'a, W: ?Sized + 'static> = GenerationalRefMut<RefMut<'a, W>>;
  164. fn downcast_lifetime_ref<'a: 'b, 'b, T: ?Sized + 'static>(
  165. ref_: Self::Ref<'a, T>,
  166. ) -> Self::Ref<'b, T> {
  167. ref_
  168. }
  169. fn downcast_lifetime_mut<'a: 'b, 'b, T: ?Sized + 'static>(
  170. mut_: Self::Mut<'a, T>,
  171. ) -> Self::Mut<'b, T> {
  172. mut_
  173. }
  174. fn map<T: ?Sized + 'static, U: ?Sized + 'static>(
  175. ref_: Self::Ref<'_, T>,
  176. f: impl FnOnce(&T) -> &U,
  177. ) -> Self::Ref<'_, U> {
  178. ref_.map(|inner| Ref::map(inner, f))
  179. }
  180. fn map_mut<T: ?Sized + 'static, U: ?Sized + 'static>(
  181. mut_ref: Self::Mut<'_, T>,
  182. f: impl FnOnce(&mut T) -> &mut U,
  183. ) -> Self::Mut<'_, U> {
  184. mut_ref.map(|inner| RefMut::map(inner, f))
  185. }
  186. fn try_map<I: ?Sized + 'static, U: ?Sized + 'static>(
  187. _self: Self::Ref<'_, I>,
  188. f: impl FnOnce(&I) -> Option<&U>,
  189. ) -> Option<Self::Ref<'_, U>> {
  190. _self.try_map(|inner| Ref::filter_map(inner, f).ok())
  191. }
  192. fn try_map_mut<I: ?Sized + 'static, U: ?Sized + 'static>(
  193. mut_ref: Self::Mut<'_, I>,
  194. f: impl FnOnce(&mut I) -> Option<&mut U>,
  195. ) -> Option<Self::Mut<'_, U>> {
  196. mut_ref.try_map(|inner| RefMut::filter_map(inner, f).ok())
  197. }
  198. fn data_ptr(&self) -> *const () {
  199. self.data.as_ptr() as *const ()
  200. }
  201. fn recycle(pointer: GenerationalPointer<Self>) {
  202. let mut borrow_mut = pointer.storage.data.borrow_mut();
  203. // First check if the generation is still valid
  204. if !borrow_mut.valid(&pointer.location) {
  205. return;
  206. }
  207. borrow_mut.increment_generation();
  208. // Then decrement the reference count or drop the value if it's the last reference
  209. match &mut borrow_mut.data {
  210. // If this is the original reference, drop the value
  211. RefCellStorageEntryData::Data(_) => borrow_mut.data = RefCellStorageEntryData::Empty,
  212. // If this is a rc, just ignore the drop
  213. RefCellStorageEntryData::Rc(_) => {}
  214. // If this is a reference, decrement the reference count
  215. RefCellStorageEntryData::Reference(reference) => {
  216. let reference = *reference;
  217. drop(borrow_mut);
  218. drop_ref(reference);
  219. }
  220. RefCellStorageEntryData::Empty => {}
  221. }
  222. UNSYNC_RUNTIME.with(|runtime| runtime.borrow_mut().push(pointer.storage));
  223. }
  224. }
  225. fn drop_ref(pointer: GenerationalPointer<UnsyncStorage>) {
  226. let mut borrow_mut = pointer.storage.data.borrow_mut();
  227. // First check if the generation is still valid
  228. if !borrow_mut.valid(&pointer.location) {
  229. return;
  230. }
  231. if let RefCellStorageEntryData::Rc(entry) = &mut borrow_mut.data {
  232. // Decrement the reference count
  233. if entry.drop_ref() {
  234. // If the reference count is now zero, drop the value
  235. borrow_mut.data = RefCellStorageEntryData::Empty;
  236. UNSYNC_RUNTIME.with(|runtime| runtime.borrow_mut().push(pointer.storage));
  237. }
  238. } else {
  239. unreachable!("References should always point to a data entry directly",);
  240. }
  241. }
  242. impl<T: 'static> Storage<T> for UnsyncStorage {
  243. #[track_caller]
  244. fn try_read(
  245. pointer: GenerationalPointer<Self>,
  246. ) -> Result<Self::Ref<'static, T>, error::BorrowError> {
  247. let read = Self::read(pointer)?;
  248. let ref_ = Ref::filter_map(read, |any| {
  249. // Then try to downcast
  250. any.downcast_ref()
  251. });
  252. match ref_ {
  253. Ok(guard) => Ok(GenerationalRef::new(
  254. guard,
  255. pointer.storage.borrow_info.borrow_guard(),
  256. )),
  257. Err(_) => Err(error::BorrowError::Dropped(
  258. error::ValueDroppedError::new_for_location(pointer.location),
  259. )),
  260. }
  261. }
  262. #[track_caller]
  263. fn try_write(
  264. pointer: GenerationalPointer<Self>,
  265. ) -> Result<Self::Mut<'static, T>, error::BorrowMutError> {
  266. let write = Self::write(pointer)?;
  267. let ref_mut = RefMut::filter_map(write, |any| {
  268. // Then try to downcast
  269. any.downcast_mut()
  270. });
  271. match ref_mut {
  272. Ok(guard) => Ok(GenerationalRefMut::new(
  273. guard,
  274. pointer.storage.borrow_info.borrow_mut_guard(),
  275. )),
  276. Err(_) => Err(error::BorrowMutError::Dropped(
  277. error::ValueDroppedError::new_for_location(pointer.location),
  278. )),
  279. }
  280. }
  281. fn new(value: T, caller: &'static std::panic::Location<'static>) -> GenerationalPointer<Self> {
  282. Self::create_new(RefCellStorageEntryData::Data(Box::new(value)), caller)
  283. }
  284. fn new_rc(
  285. value: T,
  286. caller: &'static std::panic::Location<'static>,
  287. ) -> GenerationalPointer<Self> {
  288. // Create the data that the rc points to
  289. let data = Self::create_new(
  290. RefCellStorageEntryData::Rc(RcStorageEntry::new(Box::new(value))),
  291. caller,
  292. );
  293. Self::create_new(RefCellStorageEntryData::Reference(data), caller)
  294. }
  295. fn new_reference(
  296. pointer: GenerationalPointer<Self>,
  297. ) -> BorrowResult<GenerationalPointer<Self>> {
  298. // Chase the reference to get the final location
  299. let (pointer, value) = Self::get_split_ref(pointer)?;
  300. if let RefCellStorageEntryData::Rc(data) = &value.data {
  301. data.add_ref();
  302. } else {
  303. unreachable!()
  304. }
  305. Ok(Self::create_new(
  306. RefCellStorageEntryData::Reference(pointer),
  307. pointer
  308. .location
  309. .created_at()
  310. .unwrap_or(std::panic::Location::caller()),
  311. ))
  312. }
  313. fn change_reference(
  314. location: GenerationalPointer<Self>,
  315. other: GenerationalPointer<Self>,
  316. ) -> BorrowResult {
  317. if location == other {
  318. return Ok(());
  319. }
  320. let (other_final, other_write) = Self::get_split_ref(other)?;
  321. let mut write = location.storage.data.borrow_mut();
  322. // First check if the generation is still valid
  323. if !write.valid(&location.location) {
  324. return Err(BorrowError::Dropped(ValueDroppedError::new_for_location(
  325. location.location,
  326. )));
  327. }
  328. if let (RefCellStorageEntryData::Reference(reference), RefCellStorageEntryData::Rc(data)) =
  329. (&mut write.data, &other_write.data)
  330. {
  331. if reference == &other_final {
  332. return Ok(());
  333. }
  334. drop_ref(*reference);
  335. *reference = other_final;
  336. data.add_ref();
  337. } else {
  338. tracing::trace!(
  339. "References should always point to a data entry directly found {:?} instead",
  340. other_write.data
  341. );
  342. return Err(BorrowError::Dropped(ValueDroppedError::new_for_location(
  343. other_final.location,
  344. )));
  345. }
  346. Ok(())
  347. }
  348. }