impls.rs 5.4 KB

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