unsync.rs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. use crate::{
  2. error,
  3. references::{GenerationalRef, GenerationalRefMut},
  4. AnyStorage, MemoryLocation, MemoryLocationInner, Storage,
  5. };
  6. use std::cell::{Ref, RefCell, RefMut};
  7. /// A unsync storage. This is the default storage type.
  8. #[derive(Default)]
  9. pub struct UnsyncStorage(RefCell<Option<Box<dyn std::any::Any>>>);
  10. impl<T: 'static> Storage<T> for UnsyncStorage {
  11. type Ref<R: ?Sized + 'static> = GenerationalRef<Ref<'static, R>>;
  12. type Mut<W: ?Sized + 'static> = GenerationalRefMut<RefMut<'static, W>>;
  13. fn try_read(
  14. &'static self,
  15. #[cfg(any(debug_assertions, feature = "debug_ownership"))]
  16. at: crate::GenerationalRefBorrowInfo,
  17. ) -> Result<Self::Ref<T>, error::BorrowError> {
  18. let borrow = self.0.try_borrow();
  19. #[cfg(any(debug_assertions, feature = "debug_ownership"))]
  20. let borrow = borrow.map_err(|_| at.borrowed_from.borrow_error())?;
  21. #[cfg(not(any(debug_assertions, feature = "debug_ownership")))]
  22. let borrow = borrow.map_err(|_| {
  23. error::BorrowError::AlreadyBorrowedMut(error::AlreadyBorrowedMutError {})
  24. })?;
  25. Ref::filter_map(borrow, |any| any.as_ref()?.downcast_ref())
  26. .map_err(|_| {
  27. error::BorrowError::Dropped(error::ValueDroppedError {
  28. #[cfg(any(debug_assertions, feature = "debug_ownership"))]
  29. created_at: at.created_at,
  30. })
  31. })
  32. .map(|guard| {
  33. GenerationalRef::new(
  34. guard,
  35. #[cfg(any(debug_assertions, feature = "debug_ownership"))]
  36. at,
  37. )
  38. })
  39. }
  40. fn try_write(
  41. &'static self,
  42. #[cfg(any(debug_assertions, feature = "debug_ownership"))]
  43. at: crate::GenerationalRefMutBorrowInfo,
  44. ) -> Result<Self::Mut<T>, error::BorrowMutError> {
  45. let borrow = self.0.try_borrow_mut();
  46. #[cfg(any(debug_assertions, feature = "debug_ownership"))]
  47. let borrow = borrow.map_err(|_| at.borrowed_from.borrow_mut_error())?;
  48. #[cfg(not(any(debug_assertions, feature = "debug_ownership")))]
  49. let borrow = borrow
  50. .map_err(|_| error::BorrowMutError::AlreadyBorrowed(error::AlreadyBorrowedError {}))?;
  51. RefMut::filter_map(borrow, |any| any.as_mut()?.downcast_mut())
  52. .map_err(|_| {
  53. error::BorrowMutError::Dropped(error::ValueDroppedError {
  54. #[cfg(any(debug_assertions, feature = "debug_ownership"))]
  55. created_at: at.created_at,
  56. })
  57. })
  58. .map(|guard| {
  59. GenerationalRefMut::new(
  60. guard,
  61. #[cfg(any(debug_assertions, feature = "debug_ownership"))]
  62. at,
  63. )
  64. })
  65. }
  66. fn set(&self, value: T) {
  67. *self.0.borrow_mut() = Some(Box::new(value));
  68. }
  69. fn try_map<I, U: ?Sized + 'static>(
  70. _self: Self::Ref<I>,
  71. f: impl FnOnce(&I) -> Option<&U>,
  72. ) -> Option<Self::Ref<U>> {
  73. let GenerationalRef {
  74. inner,
  75. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  76. borrow,
  77. ..
  78. } = _self;
  79. Ref::filter_map(inner, f).ok().map(|inner| GenerationalRef {
  80. inner,
  81. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  82. borrow,
  83. })
  84. }
  85. fn try_map_mut<I, U: ?Sized + 'static>(
  86. mut_ref: Self::Mut<I>,
  87. f: impl FnOnce(&mut I) -> Option<&mut U>,
  88. ) -> Option<Self::Mut<U>> {
  89. let GenerationalRefMut {
  90. inner,
  91. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  92. borrow,
  93. ..
  94. } = mut_ref;
  95. RefMut::filter_map(inner, f)
  96. .ok()
  97. .map(|inner| GenerationalRefMut {
  98. inner,
  99. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  100. borrow: crate::GenerationalRefMutBorrowInfo {
  101. borrowed_from: borrow.borrowed_from,
  102. created_at: borrow.created_at,
  103. },
  104. })
  105. }
  106. }
  107. thread_local! {
  108. static UNSYNC_RUNTIME: RefCell<Vec<MemoryLocation<UnsyncStorage>>> = RefCell::new(Vec::new());
  109. }
  110. impl AnyStorage for UnsyncStorage {
  111. fn data_ptr(&self) -> *const () {
  112. self.0.as_ptr() as *const ()
  113. }
  114. fn take(&self) -> bool {
  115. self.0.borrow_mut().take().is_some()
  116. }
  117. fn claim() -> MemoryLocation<Self> {
  118. UNSYNC_RUNTIME.with(|runtime| {
  119. if let Some(location) = runtime.borrow_mut().pop() {
  120. location
  121. } else {
  122. let data: &'static MemoryLocationInner =
  123. &*Box::leak(Box::new(MemoryLocationInner {
  124. data: Self::default(),
  125. #[cfg(any(debug_assertions, feature = "check_generation"))]
  126. generation: 0.into(),
  127. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  128. borrow: Default::default(),
  129. }));
  130. MemoryLocation(data)
  131. }
  132. })
  133. }
  134. fn recycle(location: &MemoryLocation<Self>) {
  135. location.drop();
  136. UNSYNC_RUNTIME.with(|runtime| runtime.borrow_mut().push(*location));
  137. }
  138. }