write.rs 6.5 KB

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