write.rs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. use std::ops::{DerefMut, IndexMut};
  2. use crate::read::Readable;
  3. /// A reference to a value that can be read from.
  4. #[allow(type_alias_bounds)]
  5. pub type WritableRef<'a, T: Writable, O = <T as Readable>::Target> = T::Mut<'a, O>;
  6. /// A trait for states that can be read from like [`crate::Signal`], or [`crate::GlobalSignal`]. 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 [`Writable`] type.
  7. pub trait Writable: Readable {
  8. /// The type of the reference.
  9. type Mut<'a, R: ?Sized + 'static>: DerefMut<Target = R>;
  10. /// Map the reference to a new type.
  11. fn map_mut<I: ?Sized, U: ?Sized, F: FnOnce(&mut I) -> &mut U>(
  12. ref_: Self::Mut<'_, I>,
  13. f: F,
  14. ) -> Self::Mut<'_, U>;
  15. /// Try to map the reference to a new type.
  16. fn try_map_mut<I: ?Sized, U: ?Sized, F: FnOnce(&mut I) -> Option<&mut U>>(
  17. ref_: Self::Mut<'_, I>,
  18. f: F,
  19. ) -> Option<Self::Mut<'_, U>>;
  20. /// Downcast a mutable reference in a RefMut to a more specific lifetime
  21. ///
  22. /// This function enforces the variance of the lifetime parameter `'a` in Ref.
  23. fn downcast_lifetime_mut<'a: 'b, 'b, T: ?Sized + 'static>(
  24. mut_: Self::Mut<'a, T>,
  25. ) -> Self::Mut<'b, T>;
  26. /// Get a mutable reference to the value. If the value has been dropped, this will panic.
  27. #[track_caller]
  28. fn write(&mut self) -> WritableRef<'_, Self> {
  29. self.try_write().unwrap()
  30. }
  31. /// Try to get a mutable reference to the value.
  32. #[track_caller]
  33. fn try_write(&mut self) -> Result<WritableRef<'_, Self>, generational_box::BorrowMutError> {
  34. self.try_write_unchecked().map(Self::downcast_lifetime_mut)
  35. }
  36. /// Try to get a mutable reference to the value without checking the lifetime. This will update any subscribers.
  37. ///
  38. /// NOTE: This method is completely safe because borrow checking is done at runtime.
  39. fn try_write_unchecked(
  40. &self,
  41. ) -> Result<WritableRef<'static, Self>, generational_box::BorrowMutError>;
  42. /// Get a mutable reference to the value without checking the lifetime. This will update any subscribers.
  43. ///
  44. /// NOTE: This method is completely safe because borrow checking is done at runtime.
  45. fn write_unchecked(&self) -> WritableRef<'static, Self> {
  46. self.try_write_unchecked().unwrap()
  47. }
  48. /// Run a function with a mutable reference to the value. If the value has been dropped, this will panic.
  49. #[track_caller]
  50. fn with_mut<O>(&mut self, f: impl FnOnce(&mut Self::Target) -> O) -> O {
  51. f(&mut *self.write())
  52. }
  53. /// Set the value of the signal. This will trigger an update on all subscribers.
  54. #[track_caller]
  55. fn set(&mut self, value: Self::Target)
  56. where
  57. Self::Target: Sized,
  58. {
  59. *self.write() = value;
  60. }
  61. /// Invert the boolean value of the signal. This will trigger an update on all subscribers.
  62. #[track_caller]
  63. fn toggle(&mut self)
  64. where
  65. Self::Target: std::ops::Not<Output = Self::Target> + Clone,
  66. {
  67. self.set(!self.cloned());
  68. }
  69. /// Index into the inner value and return a reference to the result.
  70. #[track_caller]
  71. fn index_mut<I>(
  72. &mut self,
  73. index: I,
  74. ) -> WritableRef<'_, Self, <Self::Target as std::ops::Index<I>>::Output>
  75. where
  76. Self::Target: std::ops::IndexMut<I>,
  77. {
  78. Self::map_mut(self.write(), |v| v.index_mut(index))
  79. }
  80. /// Takes the value out of the Signal, leaving a Default in its place.
  81. #[track_caller]
  82. fn take(&mut self) -> Self::Target
  83. where
  84. Self::Target: Default,
  85. {
  86. self.with_mut(std::mem::take)
  87. }
  88. /// Replace the value in the Signal, returning the old value.
  89. #[track_caller]
  90. fn replace(&mut self, value: Self::Target) -> Self::Target
  91. where
  92. Self::Target: Sized,
  93. {
  94. self.with_mut(|v| std::mem::replace(v, value))
  95. }
  96. }
  97. /// An extension trait for Writable<Option<T>> that provides some convenience methods.
  98. pub trait WritableOptionExt<T: 'static>: Writable<Target = Option<T>> {
  99. /// Gets the value out of the Option, or inserts the given value if the Option is empty.
  100. fn get_or_insert(&mut self, default: T) -> WritableRef<'_, Self, T> {
  101. self.get_or_insert_with(|| default)
  102. }
  103. /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
  104. fn get_or_insert_with(&mut self, default: impl FnOnce() -> T) -> WritableRef<'_, Self, T> {
  105. let is_none = self.read().is_none();
  106. if is_none {
  107. self.with_mut(|v| *v = Some(default()));
  108. Self::map_mut(self.write(), |v| v.as_mut().unwrap())
  109. } else {
  110. Self::map_mut(self.write(), |v| v.as_mut().unwrap())
  111. }
  112. }
  113. /// Attempts to write the inner value of the Option.
  114. #[track_caller]
  115. fn as_mut(&mut self) -> Option<WritableRef<'_, Self, T>> {
  116. Self::try_map_mut(self.write(), |v: &mut Option<T>| v.as_mut())
  117. }
  118. }
  119. impl<T, W> WritableOptionExt<T> for W
  120. where
  121. T: 'static,
  122. W: Writable<Target = Option<T>>,
  123. {
  124. }
  125. /// An extension trait for Writable<Vec<T>> that provides some convenience methods.
  126. pub trait WritableVecExt<T: 'static>: Writable<Target = Vec<T>> {
  127. /// Pushes a new value to the end of the vector.
  128. #[track_caller]
  129. fn push(&mut self, value: T) {
  130. self.with_mut(|v| v.push(value))
  131. }
  132. /// Pops the last value from the vector.
  133. #[track_caller]
  134. fn pop(&mut self) -> Option<T> {
  135. self.with_mut(|v| v.pop())
  136. }
  137. /// Inserts a new value at the given index.
  138. #[track_caller]
  139. fn insert(&mut self, index: usize, value: T) {
  140. self.with_mut(|v| v.insert(index, value))
  141. }
  142. /// Removes the value at the given index.
  143. #[track_caller]
  144. fn remove(&mut self, index: usize) -> T {
  145. self.with_mut(|v| v.remove(index))
  146. }
  147. /// Clears the vector, removing all values.
  148. #[track_caller]
  149. fn clear(&mut self) {
  150. self.with_mut(|v| v.clear())
  151. }
  152. /// Extends the vector with the given iterator.
  153. #[track_caller]
  154. fn extend(&mut self, iter: impl IntoIterator<Item = T>) {
  155. self.with_mut(|v| v.extend(iter))
  156. }
  157. /// Truncates the vector to the given length.
  158. #[track_caller]
  159. fn truncate(&mut self, len: usize) {
  160. self.with_mut(|v| v.truncate(len))
  161. }
  162. /// Swaps two values in the vector.
  163. #[track_caller]
  164. fn swap_remove(&mut self, index: usize) -> T {
  165. self.with_mut(|v| v.swap_remove(index))
  166. }
  167. /// Retains only the values that match the given predicate.
  168. #[track_caller]
  169. fn retain(&mut self, f: impl FnMut(&T) -> bool) {
  170. self.with_mut(|v| v.retain(f))
  171. }
  172. /// Splits the vector into two at the given index.
  173. #[track_caller]
  174. fn split_off(&mut self, at: usize) -> Vec<T> {
  175. self.with_mut(|v| v.split_off(at))
  176. }
  177. /// Try to mutably get an element from the vector.
  178. #[track_caller]
  179. fn get_mut(&mut self, index: usize) -> Option<WritableRef<'_, Self, T>> {
  180. Self::try_map_mut(self.write(), |v: &mut Vec<T>| v.get_mut(index))
  181. }
  182. /// Gets an iterator over the values of the vector.
  183. #[track_caller]
  184. fn iter_mut(&mut self) -> WritableValueIterator<'_, Self>
  185. where
  186. Self: Sized + Clone,
  187. {
  188. WritableValueIterator {
  189. index: 0,
  190. value: self,
  191. }
  192. }
  193. }
  194. /// An iterator over the values of a `Writable<Vec<T>>`.
  195. pub struct WritableValueIterator<'a, R> {
  196. index: usize,
  197. value: &'a mut R,
  198. }
  199. impl<'a, T: 'static, R: Writable<Target = Vec<T>>> Iterator for WritableValueIterator<'a, R> {
  200. type Item = WritableRef<'a, R, T>;
  201. fn next(&mut self) -> Option<Self::Item> {
  202. let index = self.index;
  203. self.index += 1;
  204. R::try_map_mut(
  205. self.value.try_write_unchecked().unwrap(),
  206. |v: &mut Vec<T>| v.get_mut(index),
  207. )
  208. .map(R::downcast_lifetime_mut)
  209. }
  210. }
  211. impl<T, W> WritableVecExt<T> for W
  212. where
  213. T: 'static,
  214. W: Writable<Target = Vec<T>>,
  215. {
  216. }