From 901b11378e5e65efab6c4c605c69fe7f9aea0a86 Mon Sep 17 00:00:00 2001 From: Adoo Date: Sat, 10 Aug 2024 19:15:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor(core):=20=F0=9F=92=A1=20Removed=20unne?= =?UTF-8?q?cessary=20`Writer`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++ core/src/animation/stagger.rs | 8 ++-- core/src/animation/transition.rs | 4 +- core/src/pipe.rs | 2 +- core/src/state.rs | 2 +- core/src/state/stateful.rs | 63 +---------------------------- core/src/widget_tree/layout_info.rs | 2 +- docs/en/get_started/quick_start.md | 4 +- docs/zh/get_started/quick_start.md | 4 +- examples/todos/src/ui.rs | 4 +- tests/rdl_macro_test.rs | 2 +- 11 files changed, 23 insertions(+), 77 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21cc899f9..bd9704452 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,11 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he - **core**: `PartData` now supports `T: ?Sized`, allowing us to separate trait objects from `State`.(#614 @M-Adoo) +### Breaking + +- **core**: Removed unnecessary `Writer` since it has the same capabilities as `Stateful`. (#pr @M-Adoo) + + ## [0.4.0-alpha.4] - 2024-08-07 ### Features diff --git a/core/src/animation/stagger.rs b/core/src/animation/stagger.rs index 66bed84f2..a78beb49f 100644 --- a/core/src/animation/stagger.rs +++ b/core/src/animation/stagger.rs @@ -103,7 +103,7 @@ impl Stagger { { let transition = Box::new(self.transition.clone()); let animate = rdl! { Animate { transition, state, from } }; - self.push_animation_with(stagger, animate.clone_writer().into_inner()); + self.push_animation_with(stagger, animate.clone_writer()); animate } @@ -168,7 +168,7 @@ impl Animation for Stateful> { } fn box_clone(&self) -> Box { - let c = self.clone_writer().into_inner(); + let c = self.clone_writer(); Box::new(c) } } @@ -187,7 +187,7 @@ impl Stateful> { this.forget_modifies(); drop(this); - let this = self.clone_writer().into_inner(); + let this = self.clone_writer(); let h = observable::timer_at((), at, AppCtx::scheduler()).subscribe(move |_| { next.run(); this.trigger_next(); @@ -265,7 +265,7 @@ mod tests { Duration::from_millis(100), EasingTransition { duration: Duration::ZERO, easing: easing::LINEAR }, ); - let c_stagger = stagger.clone_writer().into_inner(); + let c_stagger = stagger.clone_writer(); let w = fn_widget! { let mut mock_box = @MockBox { size: Size::new(100., 100.) }; $stagger.write().push_state( diff --git a/core/src/animation/transition.rs b/core/src/animation/transition.rs index 54bf1bbd0..b54484a60 100644 --- a/core/src/animation/transition.rs +++ b/core/src/animation/transition.rs @@ -26,7 +26,7 @@ pub struct RepeatTransition { /// Trait help to transition the state. pub trait TransitionState: Sized + 'static { /// Use an animate to transition the state after it modified. - fn transition(self, transition: Box, ctx: &BuildCtx) -> Writer> + fn transition(self, transition: Box, ctx: &BuildCtx) -> Stateful> where Self: AnimateState, { @@ -58,7 +58,7 @@ impl TransitionState for S {} pub trait TransitionWithFn: AnimateStateSetter + Sized { fn transition_with( self, transition: Box, lerp_fn: F, ctx: &BuildCtx, - ) -> Writer>> + ) -> Stateful>> where F: FnMut(&Self::Value, &Self::Value, f32) -> Self::Value + 'static, { diff --git a/core/src/pipe.rs b/core/src/pipe.rs index 3691528c2..26b7c3939 100644 --- a/core/src/pipe.rs +++ b/core/src/pipe.rs @@ -1272,7 +1272,7 @@ mod tests { wid: Option, } - fn build(task: Writer) -> impl IntoWidget<'static, FN> { + fn build(task: Stateful) -> impl IntoWidget<'static, FN> { fn_widget! { @TaskWidget { keep_alive: pipe!($task.pin), diff --git a/core/src/state.rs b/core/src/state.rs index 060e8f347..5995cff3c 100644 --- a/core/src/state.rs +++ b/core/src/state.rs @@ -193,7 +193,7 @@ impl StateWatcher for State { } impl StateWriter for State { - type Writer = Writer; + type Writer = Stateful; type OriginWriter = Self; #[inline] diff --git a/core/src/state/stateful.rs b/core/src/state/stateful.rs index dda7c6ceb..bcd806957 100644 --- a/core/src/state/stateful.rs +++ b/core/src/state/stateful.rs @@ -14,8 +14,6 @@ pub struct Stateful { pub struct Reader(Sc>); -pub struct Writer(Stateful); - /// The notifier is a `RxRust` stream that emit notification when the state /// changed. #[derive(Default, Clone)] @@ -80,7 +78,7 @@ impl StateWatcher for Stateful { } impl StateWriter for Stateful { - type Writer = Writer; + type Writer = Self; type OriginWriter = Self; #[inline] @@ -93,7 +91,7 @@ impl StateWriter for Stateful { fn shallow(&self) -> WriteRef { self.write_ref(ModifyScope::FRAMEWORK) } #[inline] - fn clone_writer(&self) -> Self::Writer { Writer(self.clone()) } + fn clone_writer(&self) -> Self::Writer { self.clone() } #[inline] fn origin_writer(&self) -> &Self::OriginWriter { self } @@ -124,51 +122,6 @@ impl StateReader for Reader { } } -impl StateReader for Writer { - type Value = W; - type OriginReader = Self; - type Reader = Reader; - - #[inline] - fn read(&'_ self) -> ReadRef { self.0.read() } - - #[inline] - fn clone_reader(&self) -> Self::Reader { self.0.clone_reader() } - - #[inline] - fn origin_reader(&self) -> &Self::OriginReader { self } - - #[inline] - fn try_into_value(self) -> Result { self.0.try_into_value().map_err(Writer) } -} - -impl StateWatcher for Writer { - #[inline] - fn raw_modifies(&self) -> CloneableBoxOp<'static, ModifyScope, Infallible> { - self.0.raw_modifies() - } -} - -impl StateWriter for Writer { - type Writer = Self; - type OriginWriter = Self; - - #[inline] - fn write(&self) -> WriteRef { self.0.write() } - - #[inline] - fn silent(&self) -> WriteRef { self.0.silent() } - - #[inline] - fn shallow(&self) -> WriteRef { self.0.shallow() } - - #[inline] - fn clone_writer(&self) -> Self { self.0.clone_writer() } - - #[inline] - fn origin_writer(&self) -> &Self::OriginWriter { self } -} - impl Drop for Stateful { fn drop(&mut self) { self.info.dec_writer(); } } @@ -184,11 +137,6 @@ impl Drop for WriterInfo { } } -impl Writer { - #[inline] - pub fn into_inner(self) -> Stateful { self.0 } -} - impl Stateful { pub fn new(data: W) -> Self { Self { data: Sc::new(StateCell::new(data)), info: Sc::new(WriterInfo::new()) } @@ -248,13 +196,6 @@ impl IntoWidgetStrict<'static, RENDER> for Reader { } } -impl<'w, W, const M: usize> IntoWidgetStrict<'w, M> for Writer -where - Stateful: IntoWidget<'w, M>, -{ - fn into_widget_strict(self) -> Widget<'w> { self.0.into_widget() } -} - impl Notifier { pub(crate) fn raw_modifies(&self) -> CloneableBoxOp<'static, ModifyScope, Infallible> { self.0.clone().box_it() diff --git a/core/src/widget_tree/layout_info.rs b/core/src/widget_tree/layout_info.rs index d4d222ff5..ba2c92e16 100644 --- a/core/src/widget_tree/layout_info.rs +++ b/core/src/widget_tree/layout_info.rs @@ -344,7 +344,7 @@ mod tests { @ { pipe!($child_box.size.is_empty()).map(move|b| if b { MockBox { size: Size::new(1., 1.) }.into_widget() } else { - child_box.clone_writer().into_inner().into_widget() + child_box.clone_writer().into_widget() }) } } diff --git a/docs/en/get_started/quick_start.md b/docs/en/get_started/quick_start.md index ea8091ce7..1a04d2ade 100644 --- a/docs/en/get_started/quick_start.md +++ b/docs/en/get_started/quick_start.md @@ -785,8 +785,8 @@ let _: &State = state.origin_reader(); let _: &State = state.origin_writer(); // the sub-state's origin state is where it splits from -let _: &Writer = split_count.origin_reader(); -let _: &Writer = split_count.origin_writer(); +let _: &Stateful = split_count.origin_reader(); +let _: &Stateful = split_count.origin_writer(); ``` ## The next step diff --git a/docs/zh/get_started/quick_start.md b/docs/zh/get_started/quick_start.md index e61b3ddfa..77125460b 100644 --- a/docs/zh/get_started/quick_start.md +++ b/docs/zh/get_started/quick_start.md @@ -784,8 +784,8 @@ let _: &State = state.origin_reader(); let _: &State = state.origin_writer(); // 子状态的源状态是它的父亲 -let _: &Writer = split_count.origin_reader(); -let _: &Writer = split_count.origin_writer(); +let _: &Stateful = split_count.origin_reader(); +let _: &Stateful = split_count.origin_writer(); ``` ## 下一步 diff --git a/examples/todos/src/ui.rs b/examples/todos/src/ui.rs index 880dae49d..28b87e8c8 100644 --- a/examples/todos/src/ui.rs +++ b/examples/todos/src/ui.rs @@ -38,7 +38,7 @@ fn task_lists( fn_widget! { let editing = Stateful::new(None); let stagger = Stagger::new(Duration::from_millis(100), transitions::EASE_IN_OUT.of(ctx!())); - let c_stagger = stagger.clone_writer().into_inner(); + let c_stagger = stagger.clone_writer(); @VScrollBar { on_mounted: move |_| c_stagger.run(), @@ -123,7 +123,7 @@ fn input( } .into_widget() } -fn task_item_widget(task: S, stagger: Writer>>) -> Widget<'static> +fn task_item_widget(task: S, stagger: Stateful>>) -> Widget<'static> where S: StateWriter + 'static, S::OriginWriter: StateWriter, diff --git a/tests/rdl_macro_test.rs b/tests/rdl_macro_test.rs index 231e5cca7..2b8e01705 100644 --- a/tests/rdl_macro_test.rs +++ b/tests/rdl_macro_test.rs @@ -739,7 +739,7 @@ fn fix_subscribe_cancel_after_widget_drop() { reset_test_env!(); let notify_cnt = Stateful::new(0); - let cnt: Writer = notify_cnt.clone_writer(); + let cnt = notify_cnt.clone_writer(); let trigger = Stateful::new(true); let c_trigger = trigger.clone_watcher(); let w = fn_widget! {