references.rs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. use std::{
  2. fmt::{Debug, Display},
  3. marker::PhantomData,
  4. ops::{Deref, DerefMut},
  5. };
  6. use crate::{Mappable, MappableMut};
  7. /// A reference to a value in a generational box.
  8. pub struct GenerationalRef<T: ?Sized + 'static, R: Mappable<T>> {
  9. inner: R,
  10. phantom: PhantomData<T>,
  11. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  12. borrow: GenerationalRefBorrowInfo,
  13. }
  14. impl<T: 'static, R: Mappable<T>> GenerationalRef<T, R> {
  15. pub(crate) fn new(
  16. inner: R,
  17. #[cfg(any(debug_assertions, feature = "debug_borrows"))] borrow: GenerationalRefBorrowInfo,
  18. ) -> Self {
  19. Self {
  20. inner,
  21. phantom: PhantomData,
  22. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  23. borrow,
  24. }
  25. }
  26. }
  27. impl<T: ?Sized + 'static, R: Mappable<T>> Mappable<T> for GenerationalRef<T, R> {
  28. type Mapped<U: ?Sized + 'static> = GenerationalRef<U, R::Mapped<U>>;
  29. fn map<U: ?Sized + 'static>(_self: Self, f: impl FnOnce(&T) -> &U) -> Self::Mapped<U> {
  30. GenerationalRef {
  31. inner: R::map(_self.inner, f),
  32. phantom: PhantomData,
  33. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  34. borrow: GenerationalRefBorrowInfo {
  35. borrowed_at: _self.borrow.borrowed_at,
  36. borrowed_from: _self.borrow.borrowed_from,
  37. },
  38. }
  39. }
  40. fn try_map<U: ?Sized + 'static>(
  41. _self: Self,
  42. f: impl FnOnce(&T) -> Option<&U>,
  43. ) -> Option<Self::Mapped<U>> {
  44. let Self {
  45. inner,
  46. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  47. borrow,
  48. ..
  49. } = _self;
  50. R::try_map(inner, f).map(|inner| GenerationalRef {
  51. inner,
  52. phantom: PhantomData,
  53. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  54. borrow: GenerationalRefBorrowInfo {
  55. borrowed_at: borrow.borrowed_at,
  56. borrowed_from: borrow.borrowed_from,
  57. },
  58. })
  59. }
  60. }
  61. impl<T: ?Sized + Debug, R: Mappable<T>> Debug for GenerationalRef<T, R> {
  62. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  63. self.inner.deref().fmt(f)
  64. }
  65. }
  66. impl<T: ?Sized + Display, R: Mappable<T>> Display for GenerationalRef<T, R> {
  67. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  68. self.inner.deref().fmt(f)
  69. }
  70. }
  71. impl<T: ?Sized + 'static, R: Mappable<T>> Deref for GenerationalRef<T, R> {
  72. type Target = T;
  73. fn deref(&self) -> &Self::Target {
  74. self.inner.deref()
  75. }
  76. }
  77. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  78. /// Information about a borrow.
  79. pub struct GenerationalRefBorrowInfo {
  80. pub(crate) borrowed_at: &'static std::panic::Location<'static>,
  81. pub(crate) borrowed_from: &'static crate::MemoryLocationBorrowInfo,
  82. }
  83. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  84. impl Drop for GenerationalRefBorrowInfo {
  85. fn drop(&mut self) {
  86. self.borrowed_from
  87. .borrowed_at
  88. .write()
  89. .retain(|location| std::ptr::eq(*location, self.borrowed_at as *const _));
  90. }
  91. }
  92. /// A mutable reference to a value in a generational box.
  93. pub struct GenerationalRefMut<T: ?Sized + 'static, W: MappableMut<T>> {
  94. inner: W,
  95. phantom: PhantomData<T>,
  96. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  97. borrow: GenerationalRefMutBorrowInfo,
  98. }
  99. impl<T: 'static, R: MappableMut<T>> GenerationalRefMut<T, R> {
  100. pub(crate) fn new(
  101. inner: R,
  102. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  103. borrow: GenerationalRefMutBorrowInfo,
  104. ) -> Self {
  105. Self {
  106. inner,
  107. phantom: PhantomData,
  108. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  109. borrow,
  110. }
  111. }
  112. }
  113. impl<T: ?Sized + 'static, W: MappableMut<T>> MappableMut<T> for GenerationalRefMut<T, W> {
  114. type Mapped<U: ?Sized + 'static> = GenerationalRefMut<U, W::Mapped<U>>;
  115. fn map<U: ?Sized + 'static>(_self: Self, f: impl FnOnce(&mut T) -> &mut U) -> Self::Mapped<U> {
  116. GenerationalRefMut {
  117. inner: W::map(_self.inner, f),
  118. phantom: PhantomData,
  119. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  120. borrow: _self.borrow,
  121. }
  122. }
  123. fn try_map<U: ?Sized + 'static>(
  124. _self: Self,
  125. f: impl FnOnce(&mut T) -> Option<&mut U>,
  126. ) -> Option<Self::Mapped<U>> {
  127. let Self {
  128. inner,
  129. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  130. borrow,
  131. ..
  132. } = _self;
  133. W::try_map(inner, f).map(|inner| GenerationalRefMut {
  134. inner,
  135. phantom: PhantomData,
  136. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  137. borrow,
  138. })
  139. }
  140. }
  141. impl<T: ?Sized + 'static, W: MappableMut<T>> Deref for GenerationalRefMut<T, W> {
  142. type Target = T;
  143. fn deref(&self) -> &Self::Target {
  144. self.inner.deref()
  145. }
  146. }
  147. impl<T: ?Sized + 'static, W: MappableMut<T>> DerefMut for GenerationalRefMut<T, W> {
  148. fn deref_mut(&mut self) -> &mut Self::Target {
  149. self.inner.deref_mut()
  150. }
  151. }
  152. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  153. /// Information about a mutable borrow.
  154. pub struct GenerationalRefMutBorrowInfo {
  155. /// The location where the borrow occurred.
  156. pub(crate) borrowed_from: &'static crate::MemoryLocationBorrowInfo,
  157. }
  158. #[cfg(any(debug_assertions, feature = "debug_borrows"))]
  159. impl Drop for GenerationalRefMutBorrowInfo {
  160. fn drop(&mut self) {
  161. self.borrowed_from.borrowed_mut_at.write().take();
  162. }
  163. }