impls.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. use crate::read::Readable;
  2. use crate::read::ReadableVecExt;
  3. use crate::rt::CopyValue;
  4. use crate::signal::Signal;
  5. use crate::write::Writable;
  6. use crate::{GlobalMemo, GlobalSignal, ReadOnlySignal, ReadableValueIterator, SignalData};
  7. use generational_box::UnsyncStorage;
  8. use generational_box::{AnyStorage, Storage};
  9. use std::{
  10. fmt::{Debug, Display},
  11. ops::{Add, Div, Mul, Sub},
  12. };
  13. macro_rules! read_impls {
  14. ($ty:ident $(: $extra_bounds:path)? $(, $bound_ty:ident : $bound:path, $vec_bound_ty:ident : $vec_bound:path)?) => {
  15. $(
  16. impl<T: Default + 'static, $bound_ty: $bound> Default for $ty<T, $bound_ty> {
  17. #[track_caller]
  18. fn default() -> Self {
  19. Self::new_maybe_sync(Default::default())
  20. }
  21. }
  22. )?
  23. impl<T $(: $extra_bounds)? $(,$bound_ty: $bound)?> std::clone::Clone for $ty<T $(, $bound_ty)?> {
  24. #[track_caller]
  25. fn clone(&self) -> Self {
  26. *self
  27. }
  28. }
  29. impl<T $(: $extra_bounds)? $(,$bound_ty: $bound)?> Copy for $ty<T $(, $bound_ty)?> {}
  30. impl<T: $($extra_bounds + )? Display + 'static $(,$bound_ty: $bound)?> Display for $ty<T $(, $bound_ty)?> {
  31. #[track_caller]
  32. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  33. self.with(|v| Display::fmt(v, f))
  34. }
  35. }
  36. impl<T: $($extra_bounds + )? Debug + 'static $(,$bound_ty: $bound)?> Debug for $ty<T $(, $bound_ty)?> {
  37. #[track_caller]
  38. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  39. self.with(|v| Debug::fmt(v, f))
  40. }
  41. }
  42. impl<T: $($extra_bounds + )? PartialEq + 'static $(,$bound_ty: $bound)?> PartialEq<T> for $ty<T $(, $bound_ty)?> {
  43. #[track_caller]
  44. fn eq(&self, other: &T) -> bool {
  45. self.with(|v| *v == *other)
  46. }
  47. }
  48. impl<T: $($extra_bounds + )? 'static $(,$vec_bound_ty: $vec_bound)?> $ty<Vec<T>, $($vec_bound_ty)?> {
  49. /// Returns the length of the inner vector.
  50. #[track_caller]
  51. pub fn len(&self) -> usize {
  52. self.with(|v| v.len())
  53. }
  54. /// Returns true if the inner vector is empty.
  55. #[track_caller]
  56. pub fn is_empty(&self) -> bool {
  57. self.with(|v| v.is_empty())
  58. }
  59. }
  60. };
  61. }
  62. macro_rules! write_impls {
  63. ($ty:ident, $bound:path, $vec_bound:path) => {
  64. impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::Add<T> for $ty<T, S> {
  65. type Output = T;
  66. #[track_caller]
  67. fn add(self, rhs: T) -> Self::Output {
  68. self.with(|v| *v + rhs)
  69. }
  70. }
  71. impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::AddAssign<T> for $ty<T, S> {
  72. #[track_caller]
  73. fn add_assign(&mut self, rhs: T) {
  74. self.with_mut(|v| *v = *v + rhs)
  75. }
  76. }
  77. impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::SubAssign<T> for $ty<T, S> {
  78. #[track_caller]
  79. fn sub_assign(&mut self, rhs: T) {
  80. self.with_mut(|v| *v = *v - rhs)
  81. }
  82. }
  83. impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::Sub<T> for $ty<T, S> {
  84. type Output = T;
  85. #[track_caller]
  86. fn sub(self, rhs: T) -> Self::Output {
  87. self.with(|v| *v - rhs)
  88. }
  89. }
  90. impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::MulAssign<T> for $ty<T, S> {
  91. #[track_caller]
  92. fn mul_assign(&mut self, rhs: T) {
  93. self.with_mut(|v| *v = *v * rhs)
  94. }
  95. }
  96. impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::Mul<T> for $ty<T, S> {
  97. type Output = T;
  98. #[track_caller]
  99. fn mul(self, rhs: T) -> Self::Output {
  100. self.with(|v| *v * rhs)
  101. }
  102. }
  103. impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::DivAssign<T> for $ty<T, S> {
  104. #[track_caller]
  105. fn div_assign(&mut self, rhs: T) {
  106. self.with_mut(|v| *v = *v / rhs)
  107. }
  108. }
  109. impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::Div<T> for $ty<T, S> {
  110. type Output = T;
  111. #[track_caller]
  112. fn div(self, rhs: T) -> Self::Output {
  113. self.with(|v| *v / rhs)
  114. }
  115. }
  116. };
  117. }
  118. read_impls!(CopyValue, S: Storage<T>, S: Storage<Vec<T>>);
  119. write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
  120. impl<T: 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> {
  121. type IntoIter = ReadableValueIterator<T, Self>;
  122. type Item = S::Ref<T>;
  123. fn into_iter(self) -> Self::IntoIter {
  124. self.iter()
  125. }
  126. }
  127. read_impls!(Signal, S: Storage<SignalData<T>>, S: Storage<SignalData<Vec<T>>>);
  128. write_impls!(Signal, Storage<SignalData<T>>, Storage<SignalData<Vec<T>>>);
  129. impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>, S> {
  130. type IntoIter = ReadableValueIterator<T, Self>;
  131. type Item = S::Ref<T>;
  132. fn into_iter(self) -> Self::IntoIter {
  133. self.iter()
  134. }
  135. }
  136. read_impls!(
  137. ReadOnlySignal,
  138. S: Storage<SignalData<T>>,
  139. S: Storage<SignalData<Vec<T>>>
  140. );
  141. impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for ReadOnlySignal<Vec<T>, S> {
  142. type IntoIter = ReadableValueIterator<T, Self>;
  143. type Item = S::Ref<T>;
  144. fn into_iter(self) -> Self::IntoIter {
  145. self.iter()
  146. }
  147. }
  148. read_impls!(GlobalSignal);
  149. impl<T: 'static> IntoIterator for GlobalSignal<Vec<T>> {
  150. type IntoIter = ReadableValueIterator<T, Self>;
  151. type Item = <UnsyncStorage as AnyStorage>::Ref<T>;
  152. fn into_iter(self) -> Self::IntoIter {
  153. self.iter()
  154. }
  155. }
  156. read_impls!(GlobalMemo: PartialEq);
  157. impl<T: PartialEq + 'static> IntoIterator for GlobalMemo<Vec<T>> {
  158. type IntoIter = ReadableValueIterator<T, Self>;
  159. type Item = <UnsyncStorage as AnyStorage>::Ref<T>;
  160. fn into_iter(self) -> Self::IntoIter {
  161. self.iter()
  162. }
  163. }