read.rs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. use std::{mem::MaybeUninit, ops::Index, rc::Rc};
  2. use generational_box::AnyStorage;
  3. use crate::MappedSignal;
  4. /// A reference to a value that can be read from.
  5. #[allow(type_alias_bounds)]
  6. pub type ReadableRef<'a, T: Readable, O = <T as Readable>::Target> =
  7. <T::Storage as AnyStorage>::Ref<'a, O>;
  8. /// A trait for states that can be read from like [`crate::Signal`], [`crate::GlobalSignal`], or [`crate::ReadOnlySignal`]. You may choose to accept this trait as a parameter instead of the concrete type to allow for more flexibility in your API. For example, instead of creating two functions, one that accepts a [`crate::Signal`] and one that accepts a [`crate::GlobalSignal`], you can create one function that accepts a [`Readable`] type.
  9. pub trait Readable {
  10. /// The target type of the reference.
  11. type Target: ?Sized + 'static;
  12. /// The type of the storage this readable uses.
  13. type Storage: AnyStorage;
  14. /// Map the readable type to a new type.
  15. fn map<O>(self, f: impl Fn(&Self::Target) -> &O + 'static) -> MappedSignal<O, Self::Storage>
  16. where
  17. Self: Clone + Sized + 'static,
  18. {
  19. let mapping = Rc::new(f);
  20. let try_read = Rc::new({
  21. let self_ = self.clone();
  22. let mapping = mapping.clone();
  23. move || {
  24. self_
  25. .try_read_unchecked()
  26. .map(|ref_| <Self::Storage as AnyStorage>::map(ref_, |r| mapping(r)))
  27. }
  28. })
  29. as Rc<
  30. dyn Fn() -> Result<ReadableRef<'static, Self, O>, generational_box::BorrowError>
  31. + 'static,
  32. >;
  33. let peek = Rc::new(move || {
  34. <Self::Storage as AnyStorage>::map(self.peek_unchecked(), |r| mapping(r))
  35. }) as Rc<dyn Fn() -> ReadableRef<'static, Self, O> + 'static>;
  36. MappedSignal::new(try_read, peek)
  37. }
  38. /// Get the current value of the state. If this is a signal, this will subscribe the current scope to the signal.
  39. /// If the value has been dropped, this will panic. Calling this on a Signal is the same as
  40. /// using the signal() syntax to read and subscribe to its value
  41. #[track_caller]
  42. fn read(&self) -> ReadableRef<Self> {
  43. self.try_read().unwrap()
  44. }
  45. /// Try to get the current value of the state. If this is a signal, this will subscribe the current scope to the signal.
  46. #[track_caller]
  47. fn try_read(&self) -> Result<ReadableRef<Self>, generational_box::BorrowError> {
  48. self.try_read_unchecked()
  49. .map(Self::Storage::downcast_lifetime_ref)
  50. }
  51. /// Try to get a reference to the value without checking the lifetime.
  52. ///
  53. /// NOTE: This method is completely safe because borrow checking is done at runtime.
  54. fn try_read_unchecked(
  55. &self,
  56. ) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError>;
  57. /// Tet a reference to the value without checking the lifetime.
  58. ///
  59. /// NOTE: This method is completely safe because borrow checking is done at runtime.
  60. fn read_unchecked(&self) -> ReadableRef<'static, Self> {
  61. self.try_read_unchecked().unwrap()
  62. }
  63. /// Get the current value of the signal without checking the lifetime. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
  64. ///
  65. /// If the signal has been dropped, this will panic.
  66. ///
  67. /// NOTE: This method is completely safe because borrow checking is done at runtime.
  68. fn peek_unchecked(&self) -> ReadableRef<'static, Self>;
  69. /// Get the current value of the state without subscribing to updates. If the value has been dropped, this will panic.
  70. #[track_caller]
  71. fn peek(&self) -> ReadableRef<Self> {
  72. Self::Storage::downcast_lifetime_ref(self.peek_unchecked())
  73. }
  74. /// Clone the inner value and return it. If the value has been dropped, this will panic.
  75. #[track_caller]
  76. fn cloned(&self) -> Self::Target
  77. where
  78. Self::Target: Clone,
  79. {
  80. self.read().clone()
  81. }
  82. /// Run a function with a reference to the value. If the value has been dropped, this will panic.
  83. #[track_caller]
  84. fn with<O>(&self, f: impl FnOnce(&Self::Target) -> O) -> O {
  85. f(&*self.read())
  86. }
  87. /// Run a function with a reference to the value. If the value has been dropped, this will panic.
  88. #[track_caller]
  89. fn with_peek<O>(&self, f: impl FnOnce(&Self::Target) -> O) -> O {
  90. f(&*self.peek())
  91. }
  92. /// Index into the inner value and return a reference to the result. If the value has been dropped or the index is invalid, this will panic.
  93. #[track_caller]
  94. fn index<I>(&self, index: I) -> ReadableRef<Self, <Self::Target as std::ops::Index<I>>::Output>
  95. where
  96. Self::Target: std::ops::Index<I>,
  97. {
  98. <Self::Storage as AnyStorage>::map(self.read(), |v| v.index(index))
  99. }
  100. #[doc(hidden)]
  101. fn deref_impl<'a>(&self) -> &'a dyn Fn() -> Self::Target
  102. where
  103. Self: Sized + 'a,
  104. Self::Target: Clone,
  105. {
  106. // https://github.com/dtolnay/case-studies/tree/master/callable-types
  107. // First we create a closure that captures something with the Same in memory layout as Self (MaybeUninit<Self>).
  108. let uninit_callable = MaybeUninit::<Self>::uninit();
  109. // Then move that value into the closure. We assume that the closure now has a in memory layout of Self.
  110. let uninit_closure = move || Self::read(unsafe { &*uninit_callable.as_ptr() }).clone();
  111. // Check that the size of the closure is the same as the size of Self in case the compiler changed the layout of the closure.
  112. let size_of_closure = std::mem::size_of_val(&uninit_closure);
  113. assert_eq!(size_of_closure, std::mem::size_of::<Self>());
  114. // Then cast the lifetime of the closure to the lifetime of &self.
  115. fn cast_lifetime<'a, T>(_a: &T, b: &'a T) -> &'a T {
  116. b
  117. }
  118. let reference_to_closure = cast_lifetime(
  119. {
  120. // The real closure that we will never use.
  121. &uninit_closure
  122. },
  123. // We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &Self.
  124. unsafe { std::mem::transmute(self) },
  125. );
  126. // Cast the closure to a trait object.
  127. reference_to_closure as &_
  128. }
  129. }
  130. /// An extension trait for Readable<Vec<T>> that provides some convenience methods.
  131. pub trait ReadableVecExt<T: 'static>: Readable<Target = Vec<T>> {
  132. /// Returns the length of the inner vector.
  133. #[track_caller]
  134. fn len(&self) -> usize {
  135. self.with(|v| v.len())
  136. }
  137. /// Returns true if the inner vector is empty.
  138. #[track_caller]
  139. fn is_empty(&self) -> bool {
  140. self.with(|v| v.is_empty())
  141. }
  142. /// Get the first element of the inner vector.
  143. #[track_caller]
  144. fn first(&self) -> Option<ReadableRef<Self, T>> {
  145. <Self::Storage as AnyStorage>::try_map(self.read(), |v| v.first())
  146. }
  147. /// Get the last element of the inner vector.
  148. #[track_caller]
  149. fn last(&self) -> Option<ReadableRef<Self, T>> {
  150. <Self::Storage as AnyStorage>::try_map(self.read(), |v| v.last())
  151. }
  152. /// Get the element at the given index of the inner vector.
  153. #[track_caller]
  154. fn get(&self, index: usize) -> Option<ReadableRef<Self, T>> {
  155. <Self::Storage as AnyStorage>::try_map(self.read(), |v| v.get(index))
  156. }
  157. /// Get an iterator over the values of the inner vector.
  158. #[track_caller]
  159. fn iter(&self) -> ReadableValueIterator<'_, Self>
  160. where
  161. Self: Sized,
  162. {
  163. ReadableValueIterator {
  164. index: 0,
  165. value: self,
  166. }
  167. }
  168. }
  169. /// An iterator over the values of a `Readable<Vec<T>>`.
  170. pub struct ReadableValueIterator<'a, R> {
  171. index: usize,
  172. value: &'a R,
  173. }
  174. impl<'a, T: 'static, R: Readable<Target = Vec<T>>> Iterator for ReadableValueIterator<'a, R> {
  175. type Item = ReadableRef<'a, R, T>;
  176. fn next(&mut self) -> Option<Self::Item> {
  177. let index = self.index;
  178. self.index += 1;
  179. self.value.get(index)
  180. }
  181. }
  182. impl<T, R> ReadableVecExt<T> for R
  183. where
  184. T: 'static,
  185. R: Readable<Target = Vec<T>>,
  186. {
  187. }
  188. /// An extension trait for Readable<Option<T>> that provides some convenience methods.
  189. pub trait ReadableOptionExt<T: 'static>: Readable<Target = Option<T>> {
  190. /// Unwraps the inner value and clones it.
  191. #[track_caller]
  192. fn unwrap(&self) -> T
  193. where
  194. T: Clone,
  195. {
  196. self.as_ref().unwrap().clone()
  197. }
  198. /// Attempts to read the inner value of the Option.
  199. #[track_caller]
  200. fn as_ref(&self) -> Option<ReadableRef<Self, T>> {
  201. <Self::Storage as AnyStorage>::try_map(self.read(), |v| v.as_ref())
  202. }
  203. }
  204. impl<T, R> ReadableOptionExt<T> for R
  205. where
  206. T: 'static,
  207. R: Readable<Target = Option<T>>,
  208. {
  209. }