entry.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. use crate::{
  2. BorrowError, BorrowMutError, GenerationalLocation, GenerationalRefBorrowGuard,
  3. GenerationalRefBorrowMutGuard,
  4. };
  5. pub(crate) struct StorageEntry<T> {
  6. generation: u64,
  7. pub data: Option<T>,
  8. }
  9. impl<T> Default for StorageEntry<T> {
  10. fn default() -> Self {
  11. Self {
  12. generation: 0,
  13. data: None,
  14. }
  15. }
  16. }
  17. impl<T> StorageEntry<T> {
  18. pub fn valid(&self, location: &GenerationalLocation) -> bool {
  19. self.generation == location.generation
  20. }
  21. pub fn increment_generation(&mut self) {
  22. self.generation += 1;
  23. }
  24. pub fn generation(&self) -> u64 {
  25. self.generation
  26. }
  27. }
  28. #[derive(Default)]
  29. pub(crate) struct MemoryLocationBorrowInfo(
  30. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  31. parking_lot::RwLock<MemoryLocationBorrowInfoInner>,
  32. );
  33. impl MemoryLocationBorrowInfo {
  34. pub(crate) fn borrow_mut_error(&self) -> BorrowMutError {
  35. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  36. {
  37. let borrow = self.0.read();
  38. if let Some(borrowed_mut_at) = borrow.borrowed_mut_at.as_ref() {
  39. BorrowMutError::AlreadyBorrowedMut(crate::error::AlreadyBorrowedMutError {
  40. borrowed_mut_at,
  41. })
  42. } else {
  43. BorrowMutError::AlreadyBorrowed(crate::error::AlreadyBorrowedError {
  44. borrowed_at: borrow.borrowed_at.clone(),
  45. })
  46. }
  47. }
  48. #[cfg(not(any(debug_assertions, feature = "debug_borrows")))]
  49. {
  50. BorrowMutError::AlreadyBorrowed(crate::error::AlreadyBorrowedError {})
  51. }
  52. }
  53. pub(crate) fn borrow_error(&self) -> BorrowError {
  54. BorrowError::AlreadyBorrowedMut(crate::error::AlreadyBorrowedMutError {
  55. #[cfg(any(debug_assertions, feature = "debug_ownership"))]
  56. borrowed_mut_at: self.0.read().borrowed_mut_at.unwrap(),
  57. })
  58. }
  59. /// Start a new borrow
  60. #[track_caller]
  61. pub(crate) fn borrow_guard(&'static self) -> GenerationalRefBorrowGuard {
  62. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  63. let borrowed_at = std::panic::Location::caller();
  64. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  65. {
  66. let mut borrow = self.0.write();
  67. borrow.borrowed_at.push(borrowed_at);
  68. }
  69. GenerationalRefBorrowGuard {
  70. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  71. borrowed_at,
  72. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  73. borrowed_from: self,
  74. }
  75. }
  76. /// Start a new mutable borrow
  77. #[track_caller]
  78. pub(crate) fn borrow_mut_guard(&'static self) -> GenerationalRefBorrowMutGuard {
  79. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  80. let borrowed_mut_at = std::panic::Location::caller();
  81. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  82. {
  83. let mut borrow = self.0.write();
  84. borrow.borrowed_mut_at = Some(borrowed_mut_at);
  85. }
  86. GenerationalRefBorrowMutGuard {
  87. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  88. borrowed_mut_at,
  89. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  90. borrowed_from: self,
  91. }
  92. }
  93. #[allow(unused)]
  94. pub(crate) fn drop_borrow(&self, borrowed_at: &'static std::panic::Location<'static>) {
  95. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  96. {
  97. let mut borrow = self.0.write();
  98. borrow
  99. .borrowed_at
  100. .retain(|location| *location != borrowed_at);
  101. }
  102. }
  103. #[allow(unused)]
  104. pub(crate) fn drop_borrow_mut(&self, borrowed_mut_at: &'static std::panic::Location<'static>) {
  105. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  106. {
  107. let mut borrow = self.0.write();
  108. if borrow.borrowed_mut_at == Some(borrowed_mut_at) {
  109. borrow.borrowed_mut_at = None;
  110. }
  111. }
  112. }
  113. }
  114. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  115. #[derive(Default)]
  116. struct MemoryLocationBorrowInfoInner {
  117. borrowed_at: Vec<&'static std::panic::Location<'static>>,
  118. borrowed_mut_at: Option<&'static std::panic::Location<'static>>,
  119. }