1
0

impls.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. use crate::rt::CopyValue;
  2. use crate::signal::{ReadOnlySignal, Signal, Write};
  3. use crate::SignalData;
  4. use generational_box::Mappable;
  5. use generational_box::{MappableMut, Storage};
  6. use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
  7. use std::{
  8. fmt::{Debug, Display},
  9. ops::{Add, Div, Mul, Sub},
  10. };
  11. macro_rules! read_impls {
  12. ($ty:ident) => {
  13. impl<T: Default + 'static, S: Storage<T>> Default for $ty<T, S> {
  14. fn default() -> Self {
  15. Self::new(Default::default())
  16. }
  17. }
  18. impl<T, S: Storage<T>> std::clone::Clone for $ty<T, S> {
  19. fn clone(&self) -> Self {
  20. *self
  21. }
  22. }
  23. impl<T, S: Storage<T> + Copy> Copy for $ty<T, S> {}
  24. impl<T: Display + 'static, S: Storage<T>> Display for $ty<T, S> {
  25. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  26. self.with(|v| Display::fmt(v, f))
  27. }
  28. }
  29. impl<T: Debug + 'static, S: Storage<T>> Debug for $ty<T, S> {
  30. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  31. self.with(|v| Debug::fmt(v, f))
  32. }
  33. }
  34. impl<T: 'static, S: Storage<T>> $ty<Vec<T>, S> {
  35. /// Read a value from the inner vector.
  36. pub fn get(&self, index: usize) -> Option<MappedRwLockReadGuard<'static, T>> {
  37. MappedRwLockReadGuard::try_map(self.read(), |v| v.get(index)).ok()
  38. }
  39. }
  40. impl<T: 'static, S: Storage<T>> $ty<Option<T>, S> {
  41. /// Unwraps the inner value and clones it.
  42. pub fn unwrap(&self) -> T
  43. where
  44. T: Clone,
  45. {
  46. self.with(|v| v.clone()).unwrap()
  47. }
  48. /// Attemps to read the inner value of the Option.
  49. pub fn as_ref(&self) -> Option<MappedRwLockReadGuard<'static, T>> {
  50. MappedRwLockReadGuard::try_map(self.read(), |v| v.as_ref()).ok()
  51. }
  52. }
  53. };
  54. }
  55. macro_rules! write_impls {
  56. ($ty:ident) => {
  57. impl<T: Add<Output = T> + Copy + 'static, S: Storage<T>> std::ops::Add<T> for $ty<T, S> {
  58. type Output = T;
  59. fn add(self, rhs: T) -> Self::Output {
  60. self.with(|v| *v + rhs)
  61. }
  62. }
  63. impl<T: Add<Output = T> + Copy + 'static, S: Storage<T>> std::ops::AddAssign<T>
  64. for $ty<T, S>
  65. {
  66. fn add_assign(&mut self, rhs: T) {
  67. self.with_mut(|v| *v = *v + rhs)
  68. }
  69. }
  70. impl<T: Sub<Output = T> + Copy + 'static, S: Storage<T>> std::ops::SubAssign<T>
  71. for $ty<T, S>
  72. {
  73. fn sub_assign(&mut self, rhs: T) {
  74. self.with_mut(|v| *v = *v - rhs)
  75. }
  76. }
  77. impl<T: Sub<Output = T> + Copy + 'static, S: Storage<T>> std::ops::Sub<T> for $ty<T, S> {
  78. type Output = T;
  79. fn sub(self, rhs: T) -> Self::Output {
  80. self.with(|v| *v - rhs)
  81. }
  82. }
  83. impl<T: Mul<Output = T> + Copy + 'static, S: Storage<T>> std::ops::MulAssign<T>
  84. for $ty<T, S>
  85. {
  86. fn mul_assign(&mut self, rhs: T) {
  87. self.with_mut(|v| *v = *v * rhs)
  88. }
  89. }
  90. impl<T: Mul<Output = T> + Copy + 'static, S: Storage<T>> std::ops::Mul<T> for $ty<T, S> {
  91. type Output = T;
  92. fn mul(self, rhs: T) -> Self::Output {
  93. self.with(|v| *v * rhs)
  94. }
  95. }
  96. impl<T: Div<Output = T> + Copy + 'static, S: Storage<T>> std::ops::DivAssign<T>
  97. for $ty<T, S>
  98. {
  99. fn div_assign(&mut self, rhs: T) {
  100. self.with_mut(|v| *v = *v / rhs)
  101. }
  102. }
  103. impl<T: Div<Output = T> + Copy + 'static, S: Storage<T>> std::ops::Div<T> for $ty<T, S> {
  104. type Output = T;
  105. fn div(self, rhs: T) -> Self::Output {
  106. self.with(|v| *v / rhs)
  107. }
  108. }
  109. impl<T: 'static, S: Storage<T>> $ty<Vec<T>, S> {
  110. /// Pushes a new value to the end of the vector.
  111. pub fn push(&self, value: T) {
  112. self.with_mut(|v| v.push(value))
  113. }
  114. /// Pops the last value from the vector.
  115. pub fn pop(&self) -> Option<T> {
  116. self.with_mut(|v| v.pop())
  117. }
  118. /// Inserts a new value at the given index.
  119. pub fn insert(&self, index: usize, value: T) {
  120. self.with_mut(|v| v.insert(index, value))
  121. }
  122. /// Removes the value at the given index.
  123. pub fn remove(&self, index: usize) -> T {
  124. self.with_mut(|v| v.remove(index))
  125. }
  126. /// Clears the vector, removing all values.
  127. pub fn clear(&self) {
  128. self.with_mut(|v| v.clear())
  129. }
  130. /// Extends the vector with the given iterator.
  131. pub fn extend(&self, iter: impl IntoIterator<Item = T>) {
  132. self.with_mut(|v| v.extend(iter))
  133. }
  134. /// Truncates the vector to the given length.
  135. pub fn truncate(&self, len: usize) {
  136. self.with_mut(|v| v.truncate(len))
  137. }
  138. /// Swaps two values in the vector.
  139. pub fn swap_remove(&self, index: usize) -> T {
  140. self.with_mut(|v| v.swap_remove(index))
  141. }
  142. /// Retains only the values that match the given predicate.
  143. pub fn retain(&self, f: impl FnMut(&T) -> bool) {
  144. self.with_mut(|v| v.retain(f))
  145. }
  146. /// Splits the vector into two at the given index.
  147. pub fn split_off(&self, at: usize) -> Vec<T> {
  148. self.with_mut(|v| v.split_off(at))
  149. }
  150. }
  151. impl<T: 'static, S: Storage<T>> $ty<Option<T>, S> {
  152. /// Takes the value out of the Option.
  153. pub fn take(&self) -> Option<T> {
  154. self.with_mut(|v| v.take())
  155. }
  156. /// Replace the value in the Option.
  157. pub fn replace(&self, value: T) -> Option<T> {
  158. self.with_mut(|v| v.replace(value))
  159. }
  160. /// Gets the value out of the Option, or inserts the given value if the Option is empty.
  161. pub fn get_or_insert(&self, default: T) -> S::Ref {
  162. self.get_or_insert_with(|| default)
  163. }
  164. /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
  165. pub fn get_or_insert_with(
  166. &self,
  167. default: impl FnOnce() -> T,
  168. ) -> <S::Ref as Mappable<T>>::Mapped<T> {
  169. let borrow = self.read();
  170. if borrow.is_none() {
  171. drop(borrow);
  172. self.with_mut(|v| *v = Some(default()));
  173. S::Ref::map(self.read(), |v| v.as_ref().unwrap())
  174. } else {
  175. S::Ref::map(borrow, |v| v.as_ref().unwrap())
  176. }
  177. }
  178. }
  179. };
  180. }
  181. read_impls!(CopyValue);
  182. write_impls!(CopyValue);
  183. read_impls!(Signal);
  184. write_impls!(Signal);
  185. read_impls!(ReadOnlySignal);
  186. /// An iterator over the values of a `CopyValue<Vec<T>>`.
  187. pub struct CopyValueIterator<T: 'static, S: Storage<T>> {
  188. index: usize,
  189. value: CopyValue<Vec<T>, S>,
  190. }
  191. impl<T: Clone, S: Storage<T>> Iterator for CopyValueIterator<T, S> {
  192. type Item = T;
  193. fn next(&mut self) -> Option<Self::Item> {
  194. let index = self.index;
  195. self.index += 1;
  196. self.value.get(index).map(|v| v.clone())
  197. }
  198. }
  199. impl<T: Clone + 'static, S: Storage<T>> IntoIterator for CopyValue<Vec<T>, S> {
  200. type IntoIter = CopyValueIterator<T, S>;
  201. type Item = T;
  202. fn into_iter(self) -> Self::IntoIter {
  203. CopyValueIterator {
  204. index: 0,
  205. value: self,
  206. }
  207. }
  208. }
  209. impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
  210. /// Write to an element in the inner vector.
  211. pub fn get_mut(&self, index: usize) -> Option<<S::Mut as MappableMut<Vec<T>>>::Mapped<T>> {
  212. S::Mut::try_map(self.write(), |v: &mut Vec<T>| v.get_mut(index))
  213. }
  214. }
  215. impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
  216. /// Deref the inner value mutably.
  217. pub fn as_mut(
  218. &self,
  219. ) -> Option<<<S as Storage<Option<T>>>::Mut as MappableMut<Option<T>>>::Mapped<T>> {
  220. S::Mut::try_map(self.write(), |v: &mut Option<T>| v.as_mut())
  221. }
  222. }
  223. /// An iterator over items in a `Signal<Vec<T>>`.
  224. pub struct SignalIterator<T: 'static, S: Storage<T>> {
  225. index: usize,
  226. value: Signal<Vec<T>, S>,
  227. }
  228. impl<T: Clone, S: Storage<T>> Iterator for SignalIterator<T, S> {
  229. type Item = T;
  230. fn next(&mut self) -> Option<Self::Item> {
  231. let index = self.index;
  232. self.index += 1;
  233. self.value.get(index).map(|v| v.clone())
  234. }
  235. }
  236. impl<T: Clone + 'static, S: Storage<T>> IntoIterator for Signal<Vec<T>, S> {
  237. type IntoIter = SignalIterator<T, S>;
  238. type Item = T;
  239. fn into_iter(self) -> Self::IntoIter {
  240. SignalIterator {
  241. index: 0,
  242. value: self,
  243. }
  244. }
  245. }
  246. impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S>
  247. where
  248. <<S as Storage<SignalData<std::vec::Vec<T>>>>::Mut as MappableMut<
  249. SignalData<std::vec::Vec<T>>,
  250. >>::Mapped<std::vec::Vec<T>>: MappableMut<std::vec::Vec<T>>,
  251. {
  252. /// Returns a reference to an element or `None` if out of bounds.
  253. pub fn get_mut(
  254. &self,
  255. index: usize,
  256. ) -> Option<
  257. Write<
  258. T,
  259. <<<S as Storage<SignalData<Vec<T>>>>::Mut as MappableMut<SignalData<Vec<T>>>>::Mapped<
  260. Vec<T>,
  261. > as MappableMut<Vec<T>>>::Mapped<T>,
  262. S,
  263. Vec<T>,
  264. >,
  265. > {
  266. Write::filter_map(self.write(), |v| v.get_mut(index))
  267. }
  268. }
  269. impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
  270. /// Returns a reference to an element or `None` if out of bounds.
  271. pub fn as_mut(&self) -> Option<Write<T, <<<S as Storage<SignalData<Option<T>>>>::Mut as MappableMut<SignalData<Option<T>>>>::Mapped<Option<T>> as MappableMut<Option<T>>>::Mapped<T>, S, Option<T>>>{
  272. Write::filter_map(self.write(), |v| v.as_mut())
  273. }
  274. }