impls.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. use crate::read::Readable;
  2. use crate::rt::CopyValue;
  3. use crate::signal::Signal;
  4. use crate::write::Writable;
  5. use crate::{GlobalMemo, GlobalSignal, ReadOnlySignal, SignalData};
  6. use generational_box::Storage;
  7. use std::{
  8. fmt::{Debug, Display},
  9. ops::{Add, Div, Mul, Sub},
  10. };
  11. macro_rules! read_impls {
  12. ($ty:ident $(: $extra_bounds:path)? $(, $bound_ty:ident : $bound:path, $vec_bound_ty:ident : $vec_bound:path)?) => {
  13. $(
  14. impl<T: Default + 'static, $bound_ty: $bound> Default for $ty<T, $bound_ty> {
  15. #[track_caller]
  16. fn default() -> Self {
  17. Self::new_maybe_sync(Default::default())
  18. }
  19. }
  20. )?
  21. impl<T: $($extra_bounds + )? Display + 'static $(,$bound_ty: $bound)?> Display for $ty<T $(, $bound_ty)?> {
  22. #[track_caller]
  23. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  24. self.with(|v| Display::fmt(v, f))
  25. }
  26. }
  27. impl<T: $($extra_bounds + )? Debug + 'static $(,$bound_ty: $bound)?> Debug for $ty<T $(, $bound_ty)?> {
  28. #[track_caller]
  29. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  30. self.with(|v| Debug::fmt(v, f))
  31. }
  32. }
  33. impl<T: $($extra_bounds + )? PartialEq + 'static $(,$bound_ty: $bound)?> PartialEq<T> for $ty<T $(, $bound_ty)?> {
  34. #[track_caller]
  35. fn eq(&self, other: &T) -> bool {
  36. self.with(|v| *v == *other)
  37. }
  38. }
  39. };
  40. }
  41. macro_rules! write_impls {
  42. ($ty:ident, $bound:path, $vec_bound:path) => {
  43. impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::Add<T> for $ty<T, S> {
  44. type Output = T;
  45. #[track_caller]
  46. fn add(self, rhs: T) -> Self::Output {
  47. self.with(|v| *v + rhs)
  48. }
  49. }
  50. impl<T: Add<Output = T> + Copy + 'static, S: $bound> std::ops::AddAssign<T> for $ty<T, S> {
  51. #[track_caller]
  52. fn add_assign(&mut self, rhs: T) {
  53. self.with_mut(|v| *v = *v + rhs)
  54. }
  55. }
  56. impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::SubAssign<T> for $ty<T, S> {
  57. #[track_caller]
  58. fn sub_assign(&mut self, rhs: T) {
  59. self.with_mut(|v| *v = *v - rhs)
  60. }
  61. }
  62. impl<T: Sub<Output = T> + Copy + 'static, S: $bound> std::ops::Sub<T> for $ty<T, S> {
  63. type Output = T;
  64. #[track_caller]
  65. fn sub(self, rhs: T) -> Self::Output {
  66. self.with(|v| *v - rhs)
  67. }
  68. }
  69. impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::MulAssign<T> for $ty<T, S> {
  70. #[track_caller]
  71. fn mul_assign(&mut self, rhs: T) {
  72. self.with_mut(|v| *v = *v * rhs)
  73. }
  74. }
  75. impl<T: Mul<Output = T> + Copy + 'static, S: $bound> std::ops::Mul<T> for $ty<T, S> {
  76. type Output = T;
  77. #[track_caller]
  78. fn mul(self, rhs: T) -> Self::Output {
  79. self.with(|v| *v * rhs)
  80. }
  81. }
  82. impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::DivAssign<T> for $ty<T, S> {
  83. #[track_caller]
  84. fn div_assign(&mut self, rhs: T) {
  85. self.with_mut(|v| *v = *v / rhs)
  86. }
  87. }
  88. impl<T: Div<Output = T> + Copy + 'static, S: $bound> std::ops::Div<T> for $ty<T, S> {
  89. type Output = T;
  90. #[track_caller]
  91. fn div(self, rhs: T) -> Self::Output {
  92. self.with(|v| *v / rhs)
  93. }
  94. }
  95. };
  96. }
  97. read_impls!(CopyValue, S: Storage<T>, S: Storage<Vec<T>>);
  98. write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
  99. impl<T: 'static, S: Storage<T>> Clone for CopyValue<T, S> {
  100. fn clone(&self) -> Self {
  101. *self
  102. }
  103. }
  104. impl<T: 'static, S: Storage<T>> Copy for CopyValue<T, S> {}
  105. read_impls!(Signal, S: Storage<SignalData<T>>, S: Storage<SignalData<Vec<T>>>);
  106. write_impls!(Signal, Storage<SignalData<T>>, Storage<SignalData<Vec<T>>>);
  107. impl<T: 'static, S: Storage<SignalData<T>>> Clone for Signal<T, S> {
  108. fn clone(&self) -> Self {
  109. *self
  110. }
  111. }
  112. impl<T: 'static, S: Storage<SignalData<T>>> Copy for Signal<T, S> {}
  113. read_impls!(
  114. ReadOnlySignal,
  115. S: Storage<SignalData<T>>,
  116. S: Storage<SignalData<Vec<T>>>
  117. );
  118. impl<T: 'static, S: Storage<SignalData<T>>> Clone for ReadOnlySignal<T, S> {
  119. fn clone(&self) -> Self {
  120. *self
  121. }
  122. }
  123. impl<T: 'static, S: Storage<SignalData<T>>> Copy for ReadOnlySignal<T, S> {}
  124. read_impls!(GlobalSignal);
  125. read_impls!(GlobalMemo: PartialEq);