|
| 1 | +pub use self::render_value::{ |
| 2 | + RenderValueMut, RenderValueOnce, RenderValueWithFn, RenderValueWithFnMutAndData, |
| 3 | + RenderValueWithFnOnceAndData, |
| 4 | +}; |
| 5 | + |
| 6 | +use std::{pin::Pin, task::Poll}; |
| 7 | + |
| 8 | +use crate::csr::StateUnmount; |
| 9 | + |
| 10 | +mod render_value; |
| 11 | + |
| 12 | +mod option; |
| 13 | +pub mod str; |
| 14 | + |
| 15 | +pub trait ReactiveValueKind: 'static { |
| 16 | + type Value<'a>; |
| 17 | +} |
| 18 | + |
| 19 | +impl ReactiveValueKind for str { |
| 20 | + type Value<'a> = &'a str; |
| 21 | +} |
| 22 | + |
| 23 | +pub trait ReactiveValueState: StateUnmount { |
| 24 | + type ReactiveValueKind: ?Sized + ReactiveValueKind; |
| 25 | + |
| 26 | + fn reactive_value_state_poll_render( |
| 27 | + self: Pin<&mut Self>, |
| 28 | + renderer: &mut impl RenderValueMut<Self::ReactiveValueKind, RenderOutput = ()>, |
| 29 | + cx: &mut std::task::Context<'_>, |
| 30 | + ) -> Poll<()>; |
| 31 | +} |
| 32 | + |
| 33 | +impl<T: ReactiveValueState> ReactiveValueState for Option<T> { |
| 34 | + type ReactiveValueKind = T::ReactiveValueKind; |
| 35 | + |
| 36 | + fn reactive_value_state_poll_render( |
| 37 | + self: Pin<&mut Self>, |
| 38 | + renderer: &mut impl RenderValueMut<Self::ReactiveValueKind, RenderOutput = ()>, |
| 39 | + cx: &mut std::task::Context<'_>, |
| 40 | + ) -> Poll<()> { |
| 41 | + if let Some(this) = self.as_pin_mut() { |
| 42 | + this.reactive_value_state_poll_render(renderer, cx) |
| 43 | + } else { |
| 44 | + Poll::Ready(()) |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +mod sealed { |
| 50 | + pub trait AsOptionMut<T: ?Sized> {} |
| 51 | +} |
| 52 | + |
| 53 | +/// This trait is sealed to ensure correct implementation: |
| 54 | +/// - `as_option_mut()` returning `None` indicates [`<Self as StateUnmount>::state_unmount()`](StateUnmount::state_unmount) is noop. |
| 55 | +/// - `as_option_mut()` returning `None` indicates the renderer has been called with `renderer.*_remove()` |
| 56 | +/// |
| 57 | +/// (Search the code for *Correct AsOptionMut Implementation* to find the code relying on this behavior.) |
| 58 | +pub trait AsOptionMut<T: ?Sized>: sealed::AsOptionMut<T> { |
| 59 | + // Not using the receiver `&mut self` so that calling this method must be qualified. |
| 60 | + fn as_option_mut(this: &mut Self) -> Option<&mut T>; |
| 61 | + |
| 62 | + fn as_option_pin_mut(this: Pin<&mut Self>) -> Option<Pin<&mut T>>; |
| 63 | +} |
| 64 | + |
| 65 | +impl<T: ?Sized> sealed::AsOptionMut<T> for T {} |
| 66 | +impl<T: ?Sized> AsOptionMut<T> for T { |
| 67 | + fn as_option_mut(this: &mut Self) -> Option<&mut T> { |
| 68 | + Some(this) |
| 69 | + } |
| 70 | + |
| 71 | + fn as_option_pin_mut(this: Pin<&mut Self>) -> Option<Pin<&mut T>> { |
| 72 | + Some(this) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl<T> sealed::AsOptionMut<T> for Option<T> {} |
| 77 | +impl<T> AsOptionMut<T> for Option<T> { |
| 78 | + fn as_option_mut(this: &mut Self) -> Option<&mut T> { |
| 79 | + this.as_mut() |
| 80 | + } |
| 81 | + |
| 82 | + fn as_option_pin_mut(this: Pin<&mut Self>) -> Option<Pin<&mut T>> { |
| 83 | + this.as_pin_mut() |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +/// The value is optional. That's why [`RenderValueOnce`] and [`RenderValueMut`] have a `remove*` method. |
| 88 | +pub trait ReactiveValue<VK: ?Sized + ReactiveValueKind> { |
| 89 | + type PinnedState; |
| 90 | + |
| 91 | + /// Requires [`Default`] so that it can be initialized and pinned safely. |
| 92 | + type PinnedStateDefault: Default |
| 93 | + + From<Self::PinnedState> |
| 94 | + + AsOptionMut<Self::PinnedState> |
| 95 | + + ReactiveValueState<ReactiveValueKind = VK>; |
| 96 | + |
| 97 | + /// Requires [`Unpin`] so that [`CsrReactiveStrStateUnmount`] can be reused without defining another trait taking `&mut self`. |
| 98 | + type UnpinnedState: Unpin + ReactiveValueState<ReactiveValueKind = VK>; |
| 99 | + |
| 100 | + /// Requires `Unpin + ReactiveValueState` so that implementations for `Option` like types doesn't need to |
| 101 | + /// introduce a wrapper type to implement `type StateUnpinned`. |
| 102 | + /// `Unpin + ReactiveValueState` is usually already implemented because `Option<_>` derives them. |
| 103 | + type UnpinnedStateDefault: Default |
| 104 | + + From<Self::UnpinnedState> |
| 105 | + + AsOptionMut<Self::UnpinnedState> |
| 106 | + + Unpin |
| 107 | + + ReactiveValueState<ReactiveValueKind = VK>; |
| 108 | + |
| 109 | + /// `state` is `Default::default()` at a pinned place. |
| 110 | + fn reactive_value_render_init_pinned<Out>( |
| 111 | + self, |
| 112 | + renderer: impl RenderValueOnce<VK, RenderOutput = Out>, |
| 113 | + state: Pin<&mut Self::PinnedStateDefault>, |
| 114 | + ) -> Out; |
| 115 | + |
| 116 | + /// `old_state` has been [unmounted](ReactiveStrStateUnmount::reactive_value_state_unmount) but not necessarily set to `Default::default()`. |
| 117 | + fn reactive_value_render_init_with_old_state_pinned<Out>( |
| 118 | + self, |
| 119 | + renderer: impl RenderValueOnce<VK, RenderOutput = Out>, |
| 120 | + old_state: Pin<&mut Self::PinnedStateDefault>, |
| 121 | + ) -> Out; |
| 122 | + |
| 123 | + /// `renderer` doesn't need to be updated if unnecessary. |
| 124 | + fn reactive_value_render_update_pinned<Out>( |
| 125 | + self, |
| 126 | + renderer: impl RenderValueOnce<VK, RenderOutput = Out>, |
| 127 | + state: Pin<&mut Self::PinnedStateDefault>, |
| 128 | + ) -> Option<Out>; |
| 129 | + |
| 130 | + fn reactive_value_render_init_unpinned<Out>( |
| 131 | + self, |
| 132 | + renderer: impl RenderValueOnce<VK, RenderOutput = Out>, |
| 133 | + ) -> (Self::UnpinnedState, Out); |
| 134 | + |
| 135 | + fn reactive_value_render_init_with_old_state_unpinned<Out>( |
| 136 | + self, |
| 137 | + renderer: impl RenderValueOnce<VK, RenderOutput = Out>, |
| 138 | + old_state: &mut Self::UnpinnedState, |
| 139 | + ) -> Out; |
| 140 | + |
| 141 | + /// `renderer` doesn't need to be updated if unnecessary. |
| 142 | + fn reactive_value_render_update_unpinned<Out>( |
| 143 | + self, |
| 144 | + renderer: impl RenderValueOnce<VK, RenderOutput = Out>, |
| 145 | + state: &mut Self::UnpinnedState, |
| 146 | + ) -> Option<Out>; |
| 147 | +} |
| 148 | + |
| 149 | +pub trait ReactiveValueExt<VK: ?Sized + ReactiveValueKind>: ReactiveValue<VK> + Sized { |
| 150 | + fn reactive_value_render_init_unpinned_default<Out>( |
| 151 | + self, |
| 152 | + renderer: impl RenderValueOnce<VK, RenderOutput = Out>, |
| 153 | + ) -> (Self::UnpinnedStateDefault, Out) { |
| 154 | + let (state, out) = self.reactive_value_render_init_unpinned(renderer); |
| 155 | + (state.into(), out) |
| 156 | + } |
| 157 | + |
| 158 | + fn reactive_value_render_init_with_old_state_unpinned_default<Out>( |
| 159 | + self, |
| 160 | + renderer: impl RenderValueOnce<VK, RenderOutput = Out>, |
| 161 | + old_state: &mut Self::UnpinnedStateDefault, |
| 162 | + ) -> Out { |
| 163 | + if let Some(old_state) = AsOptionMut::<Self::UnpinnedState>::as_option_mut(old_state) { |
| 164 | + self.reactive_value_render_init_with_old_state_unpinned(renderer, old_state) |
| 165 | + } else { |
| 166 | + // I pray for the compiler to optimize out this branch when StateUnpinnedDefault::as_option_mut() always returns Some(_) |
| 167 | + let out; |
| 168 | + (*old_state, out) = self.reactive_value_render_init_unpinned_default(renderer); |
| 169 | + out |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /// Note that this method requires `state` to be Some or will panic. |
| 174 | + /// |
| 175 | + /// `renderer` doesn't need to be updated if unnecessary. |
| 176 | + fn reactive_value_render_update_unpinned_default<Out>( |
| 177 | + self, |
| 178 | + renderer: impl RenderValueOnce<VK, RenderOutput = Out>, |
| 179 | + state: &mut Self::UnpinnedStateDefault, |
| 180 | + ) -> Option<Out> { |
| 181 | + if let Some(state) = AsOptionMut::<Self::UnpinnedState>::as_option_mut(state) { |
| 182 | + self.reactive_value_render_update_unpinned(renderer, state) |
| 183 | + } else { |
| 184 | + ::core::panic!("state should have been initialized before render_update") |
| 185 | + } |
| 186 | + } |
| 187 | +} |
| 188 | +impl<T: ReactiveValue<VK>, VK: ?Sized + ReactiveValueKind> ReactiveValueExt<VK> for T {} |
| 189 | + |
| 190 | +macro_rules! impl_reactive_value_pinned_with_unpinned { |
| 191 | + ( |
| 192 | + type ReactiveValueKind = $ReactiveValueKind:ty; |
| 193 | + ) => { |
| 194 | + type PinnedState = Self::UnpinnedState; |
| 195 | + type PinnedStateDefault = Self::UnpinnedStateDefault; |
| 196 | + |
| 197 | + fn reactive_value_render_init_pinned<Out>( |
| 198 | + self, |
| 199 | + renderer: impl $crate::reactive_value::RenderValueOnce< |
| 200 | + $ReactiveValueKind, |
| 201 | + RenderOutput = Out, |
| 202 | + >, |
| 203 | + default_state: ::core::pin::Pin<&mut Self::PinnedStateDefault>, |
| 204 | + ) -> Out { |
| 205 | + let default_state = default_state.get_mut(); |
| 206 | + |
| 207 | + ::core::debug_assert!( |
| 208 | + <Self::PinnedStateDefault as $crate::reactive_value::AsOptionMut< |
| 209 | + Self::PinnedState, |
| 210 | + >>::as_option_mut(default_state) |
| 211 | + .is_none(), |
| 212 | + "state must be set to default before render_init" |
| 213 | + ); |
| 214 | + |
| 215 | + let out; |
| 216 | + (*default_state, out) = <Self as $crate::reactive_value::ReactiveValueExt< |
| 217 | + $ReactiveValueKind, |
| 218 | + >>::reactive_value_render_init_unpinned_default( |
| 219 | + self, renderer |
| 220 | + ); |
| 221 | + |
| 222 | + out |
| 223 | + } |
| 224 | + |
| 225 | + fn reactive_value_render_init_with_old_state_pinned<Out>( |
| 226 | + self, |
| 227 | + renderer: impl $crate::reactive_value::RenderValueOnce< |
| 228 | + $ReactiveValueKind, |
| 229 | + RenderOutput = Out, |
| 230 | + >, |
| 231 | + old_state: ::core::pin::Pin<&mut Self::PinnedStateDefault>, |
| 232 | + ) -> Out { |
| 233 | + #[rustfmt::skip] |
| 234 | + let out = <Self as $crate::reactive_value::ReactiveValueExt< |
| 235 | + $ReactiveValueKind, |
| 236 | + >>::reactive_value_render_init_with_old_state_unpinned_default( |
| 237 | + self, renderer, old_state.get_mut() |
| 238 | + ); |
| 239 | + out |
| 240 | + } |
| 241 | + |
| 242 | + fn reactive_value_render_update_pinned<Out>( |
| 243 | + self, |
| 244 | + renderer: impl $crate::reactive_value::RenderValueOnce<str, RenderOutput = Out>, |
| 245 | + state: ::core::pin::Pin<&mut Self::PinnedStateDefault>, |
| 246 | + ) -> ::core::option::Option<Out> { |
| 247 | + #[rustfmt::skip] |
| 248 | + let out = <Self as $crate::reactive_value::ReactiveValueExt< |
| 249 | + $ReactiveValueKind, |
| 250 | + >>::reactive_value_render_update_unpinned_default( |
| 251 | + self, renderer, state.get_mut() |
| 252 | + ); |
| 253 | + out |
| 254 | + } |
| 255 | + }; |
| 256 | +} |
| 257 | + |
| 258 | +use impl_reactive_value_pinned_with_unpinned; |
0 commit comments