1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
mod map_state;
mod prior_op;
mod splitted_state;
mod stateful;
pub use prior_op::*;

use std::{
  cell::{Cell, Ref, RefCell, RefMut, UnsafeCell},
  convert::Infallible,
  mem::MaybeUninit,
  ops::{Deref, DerefMut},
  time::Instant,
};

use crate::prelude::*;
pub use map_state::*;
use rxrust::ops::box_it::BoxOp;
pub use splitted_state::*;
pub use stateful::*;

/// The `StateReader` trait allows for reading, clone and map the state.
pub trait StateReader: 'static {
  /// The value type of this state.
  type Value: ?Sized;
  /// The origin state type that this state map or split from . Otherwise
  /// return itself.
  type OriginReader: StateReader;
  type Reader: StateReader<Value = Self::Value>;

  /// Return a reference of this state.
  fn read(&self) -> ReadRef<Self::Value>;
  /// get a clone of this state that only can read.
  fn clone_reader(&self) -> Self::Reader;
  /// Maps an reader to another by applying a function to a contained
  /// value. The return reader is just a shortcut to access part of the origin
  /// reader.
  ///
  /// ## Undefined Behavior
  ///
  /// As `MapReader` is a shortcut to access part of the origin writer, it's
  /// assume its part of data is always valid. If the data is invalid, and you
  /// use `MapReader` to access it, it's undefined behavior.
  #[inline]
  fn map_reader<U, F>(&self, map: F) -> MapReader<Self::Reader, F>
  where
    U: ?Sized,
    F: Fn(&Self::Value) -> &U + Clone,
  {
    MapReader { origin: self.clone_reader(), map }
  }
  /// Return the origin reader that this state map or split from . Otherwise
  /// return itself.
  fn origin_reader(&self) -> &Self::OriginReader;

  /// Return the time stamp of the last modifies of this state.
  fn time_stamp(&self) -> Instant;

  /// Return a modifies `Rx` stream of the state, user can subscribe it to
  /// response the state changes.
  fn modifies(&self) -> BoxOp<'static, ModifyScope, Infallible> {
    self
      .raw_modifies()
      .filter(|s| s.contains(ModifyScope::DATA))
      .box_it()
  }

  /// Return a modifies `Rx` stream of the state, including all modifies. Use
  /// `modifies` instead if you only want to response the data changes.
  fn raw_modifies(&self) -> BoxOp<'static, ModifyScope, Infallible>;
  /// try convert this state into the value, if there is no other share this
  /// state, otherwise return an error with self.
  fn try_into_value(self) -> Result<Self::Value, Self>
  where
    Self: Sized,
    Self::Value: Sized;
}

pub trait StateWriter: StateReader {
  type Writer: StateWriter<Value = Self::Value>;
  type OriginWriter: StateWriter;

  /// Return a write reference of this state.
  fn write(&self) -> WriteRef<Self::Value>;
  /// Return a silent write reference which notifies will be ignored by the
  /// framework.
  fn silent(&self) -> WriteRef<Self::Value>;
  /// Return a shallow write reference. Modify across this reference will notify
  /// framework only. That means the modifies on shallow reference should only
  /// effect framework but not effect on data. eg. temporary to modify the
  /// state and then modifies it back to trigger the view update. Use it only
  /// if you know how a shallow reference works.
  fn shallow(&self) -> WriteRef<Self::Value>;
  /// Clone this state writer.
  fn clone_writer(&self) -> Self::Writer;
  /// Return the origin writer that this state map or split from.
  fn origin_writer(&self) -> &Self::OriginWriter;
  /// Return a new writer that be part of the origin writer by applying a
  /// function to the contained value.
  ///
  /// The return writer share the same data with the origin writer. But modify
  /// the data through the return writer will not trigger the views depend on
  /// the origin writer to update.
  ///
  /// If you want a new writer that has same notifier with the origin writer,
  /// you can use `map_writer(...)`.
  ///
  /// ##Notice
  ///
  /// Your `mut_map` function will accept a mutable reference, but you should
  /// not modify it, just return a mutable reference to a part of it. Because
  /// Ribir assume you will not modify the origin data in it. If you modify the
  /// origin data in it, no downstream will be notified.
  ///
  /// When you remove/invalid some data across the return writer, you should be
  /// careful. Because that part may be in another state reader or writer, if
  /// you invalid it, other state reader or writer will be invalid too.
  ///
  /// ## Panic
  ///
  /// 1. When the origin writer modifies, the return writer will be invalid,
  ///    access its value will panic.
  /// 2. If modifies the split part data causing any data to be invalid, it may
  ///    panic.

  #[inline]
  fn split_writer<V, R, W>(&self, map: R, mut_map: W) -> SplittedWriter<Self::Writer, R, W>
  where
    V: ?Sized,
    R: Fn(&Self::Value) -> &V + Clone + 'static,
    W: Fn(&mut Self::Value) -> &mut V + Clone + 'static,
  {
    SplittedWriter::new(self.clone_writer(), map, mut_map)
  }

  /// Return a new writer by applying a function to the contained value. The
  /// return writer is just a shortcut to access part of the origin writer.
  ///
  /// ## Notice
  ///
  /// Your `mut_map` function will accept a mutable reference, but you should
  /// not modify it, just return a mutable reference to a part of it. Because
  /// Ribir assume you will not modify the origin data in it. If you modify the
  /// origin data in it, no downstream will be notified.
  ///
  /// ## Undefined Behavior
  ///
  /// As `MapWriter` is a shortcut to access part of the origin writer, it's
  /// assume its part of data is always valid. If the data is invalid, and you
  /// use `MapWriter` to access it, it's undefined behavior.
  #[inline]
  fn map_writer<V, R, W>(&self, map: R, mut_map: W) -> MapWriter<Self::Writer, R, W>
  where
    V: ?Sized,
    R: Fn(&Self::Value) -> &V + Clone,
    W: Fn(&mut Self::Value) -> &mut V + Clone,
  {
    let origin = self.clone_writer();
    MapWriter { origin, map, mut_map }
  }
}

/// Wraps a borrowed reference to a value in a state.
/// A wrapper type for an immutably borrowed value from a `StateReader`.
pub struct ReadRef<'a, V: ?Sized>(Ref<'a, V>);

pub struct WriteRef<'a, V: ?Sized> {
  value: Option<RefMut<'a, V>>,
  control: &'a dyn WriterControl,
  modify_scope: ModifyScope,
  modified: bool,
}

/// Enum to store both stateless and stateful object.
pub struct State<W>(pub(crate) UnsafeCell<InnerState<W>>);

pub(crate) enum InnerState<W> {
  Data(RefCell<W>),
  Stateful(Stateful<W>),
}

trait WriterControl {
  fn last_modified_stamp(&self) -> &Cell<Instant>;
  fn batched_modifies(&self) -> &Cell<ModifyScope>;
  fn notifier(&self) -> &Notifier;
  fn dyn_clone(&self) -> Box<dyn WriterControl>;
}

impl<T: 'static> StateReader for State<T> {
  type Value = T;
  type OriginReader = Self;
  type Reader = Reader<T>;

  fn read(&self) -> ReadRef<T> {
    match self.inner_ref() {
      InnerState::Data(w) => ReadRef::new(w.borrow()),
      InnerState::Stateful(w) => w.read(),
    }
  }

  #[inline]
  fn clone_reader(&self) -> Self::Reader { self.as_stateful().clone_reader() }

  #[inline]
  fn origin_reader(&self) -> &Self::OriginReader { self }

  #[inline]
  fn time_stamp(&self) -> Instant { self.as_stateful().time_stamp() }

  #[inline]
  fn raw_modifies(&self) -> BoxOp<'static, ModifyScope, Infallible> {
    self.as_stateful().raw_modifies()
  }

  fn try_into_value(self) -> Result<Self::Value, Self> {
    match self.0.into_inner() {
      InnerState::Data(w) => Ok(w.into_inner()),
      InnerState::Stateful(w) => w.try_into_value().map_err(State::stateful),
    }
  }
}

impl<T: 'static> StateWriter for State<T> {
  type Writer = Writer<T>;
  type OriginWriter = Self;

  #[inline]
  fn write(&self) -> WriteRef<T> { self.as_stateful().write() }

  #[inline]
  fn silent(&self) -> WriteRef<T> { self.as_stateful().silent() }

  #[inline]
  fn shallow(&self) -> WriteRef<T> { self.as_stateful().shallow() }

  #[inline]
  fn clone_writer(&self) -> Self::Writer { self.as_stateful().clone_writer() }

  #[inline]
  fn origin_writer(&self) -> &Self::OriginWriter { self }
}

impl<W> State<W> {
  pub fn stateful(stateful: Stateful<W>) -> Self {
    State(UnsafeCell::new(InnerState::Stateful(stateful)))
  }

  pub fn value(value: W) -> Self { State(UnsafeCell::new(InnerState::Data(RefCell::new(value)))) }

  pub fn as_stateful(&self) -> &Stateful<W> {
    match self.inner_ref() {
      InnerState::Data(w) => {
        assert!(w.try_borrow_mut().is_ok());

        let mut uninit: MaybeUninit<_> = MaybeUninit::uninit();
        // Safety: we already check there is no other reference to the state data.
        unsafe {
          std::ptr::copy(w, uninit.as_mut_ptr(), 1);
          let value = uninit.assume_init().into_inner();
          let stateful = InnerState::Stateful(Stateful::new(value));
          let copy = std::mem::replace(&mut *self.0.get(), stateful);
          // this is a copy of the inner data so we need forget it.
          std::mem::forget(copy);
        };

        match self.inner_ref() {
          InnerState::Stateful(w) => w,
          _ => unreachable!(),
        }
      }
      InnerState::Stateful(w) => w,
    }
  }

  fn inner_ref(&self) -> &InnerState<W> {
    // Safety: we only use this method to get the inner state, and no way to get the
    // mutable reference of the inner state except the `as_stateful` method and the
    // `as_stateful` will check the inner borrow state.
    unsafe { &*self.0.get() }
  }
}

impl<'a, V: ?Sized> ReadRef<'a, V> {
  pub(crate) fn new(r: Ref<'a, V>) -> ReadRef<'a, V> { ReadRef(r) }

  pub(crate) fn map<U: ?Sized>(r: ReadRef<'a, V>, f: impl FnOnce(&V) -> &U) -> ReadRef<'a, U> {
    ReadRef(Ref::map(r.0, f))
  }
}

impl<'a, V: ?Sized> WriteRef<'a, V> {
  /// Forget all modifies of this reference. So all the modifies occurred on
  /// this reference before this call will not be notified. Return true if there
  /// is any modifies on this reference.
  #[inline]
  pub fn forget_modifies(&mut self) -> bool { std::mem::replace(&mut self.modified, false) }
}

impl<'a, W: ?Sized> Deref for ReadRef<'a, W> {
  type Target = W;

  #[track_caller]
  #[inline]
  fn deref(&self) -> &Self::Target { &self.0 }
}

impl<'a, W: ?Sized> Deref for WriteRef<'a, W> {
  type Target = W;
  #[track_caller]
  #[inline]
  fn deref(&self) -> &Self::Target {
    // Safety: value always exists except in drop method.
    unsafe { self.value.as_ref().unwrap_unchecked().deref() }
  }
}

impl<'a, W: ?Sized> DerefMut for WriteRef<'a, W> {
  #[track_caller]
  #[inline]
  fn deref_mut(&mut self) -> &mut Self::Target {
    self.modified = true;
    self.control.last_modified_stamp().set(Instant::now());
    // Safety: value always exists except in drop method.
    unsafe { self.value.as_mut().unwrap_unchecked().deref_mut() }
  }
}

impl<'a, W: ?Sized> Drop for WriteRef<'a, W> {
  fn drop(&mut self) {
    let Self { control, modify_scope, modified, .. } = self;
    if !*modified {
      return;
    }

    let batched_modifies = control.batched_modifies();
    if batched_modifies.get().is_empty() && !modify_scope.is_empty() {
      batched_modifies.set(*modify_scope);

      let control = control.dyn_clone();
      AppCtx::spawn_local(async move {
        let scope = control.batched_modifies().replace(ModifyScope::empty());
        control.notifier().next(scope);
      })
      .unwrap();
    } else {
      batched_modifies.set(*modify_scope | batched_modifies.get());
    }
  }
}

// todo: We should use `BoxPipe<T>` to replace `State<T>` as the dynamic child.
// remove it after no widget use `State<T>` Child.
pub(crate) trait StateFrom<V> {
  fn state_from(value: V) -> Self;
}

impl<W> StateFrom<W> for State<W> {
  #[inline]
  fn state_from(value: W) -> State<W> { State::value(value) }
}

impl<W> StateFrom<Stateful<W>> for State<W> {
  #[inline]
  fn state_from(value: Stateful<W>) -> State<W> { State::stateful(value) }
}

impl<W, T> From<T> for State<W>
where
  Self: StateFrom<T>,
{
  fn from(value: T) -> Self { StateFrom::state_from(value) }
}

impl<C: Compose + 'static> ComposeBuilder for State<C> {
  #[inline]
  fn build(self, ctx: &BuildCtx) -> Widget { Compose::compose(self).build(ctx) }
}

impl<P: ComposeChild<Child = Option<C>> + 'static, C> ComposeChildBuilder for State<P> {
  #[inline]
  fn build(self, ctx: &BuildCtx) -> Widget { ComposeChild::compose_child(self, None).build(ctx) }
}

impl<W: SingleChild> SingleChild for State<W> {}
impl<W: MultiChild> MultiChild for State<W> {}

impl<R: Render> RenderBuilder for State<R> {
  #[inline]
  fn build(self, ctx: &BuildCtx) -> Widget {
    match self.0.into_inner() {
      InnerState::Data(w) => w.into_inner().build(ctx),
      InnerState::Stateful(w) => w.build(ctx),
    }
  }
}

impl<W: SingleChild + Render> SingleParent for State<W> {
  fn compose_child(self, child: Widget, ctx: &BuildCtx) -> Widget {
    match self.0.into_inner() {
      InnerState::Data(w) => w.into_inner().compose_child(child, ctx),
      InnerState::Stateful(w) => w.compose_child(child, ctx),
    }
  }
}

impl<W: MultiChild + Render> MultiParent for State<W> {
  fn compose_children(self, children: impl Iterator<Item = Widget>, ctx: &BuildCtx) -> Widget {
    match self.0.into_inner() {
      InnerState::Data(w) => w.into_inner().compose_children(children, ctx),
      InnerState::Stateful(w) => w.compose_children(children, ctx),
    }
  }
}

impl<T: StateReader + 'static> Query for T
where
  T::Value: 'static + Sized,
{
  #[inline]
  fn query_inside_first(
    &self,
    type_id: TypeId,
    callback: &mut dyn FnMut(&dyn Any) -> bool,
  ) -> bool {
    self.query_outside_first(type_id, callback)
  }

  fn query_outside_first(
    &self,
    type_id: TypeId,
    callback: &mut dyn FnMut(&dyn Any) -> bool,
  ) -> bool {
    let any: &T::Value = &self.read();
    if type_id == any.type_id() {
      callback(any)
    } else {
      true
    }
  }
}

macro_rules! impl_compose_builder {
  ($name: ident) => {
    impl<V, W, RM, WM> ComposeBuilder for $name<W, RM, WM>
    where
      W: StateWriter,
      RM: Fn(&W::Value) -> &V + Clone + 'static,
      WM: Fn(&mut W::Value) -> &mut V + Clone + 'static,
      V: Compose + 'static,
    {
      fn build(self, ctx: &crate::context::BuildCtx) -> Widget { Compose::compose(self).build(ctx) }
    }

    impl<V, W, RM, WM, Child> ComposeChildBuilder for $name<W, RM, WM>
    where
      W: StateWriter,
      RM: Fn(&W::Value) -> &V + Clone + 'static,
      WM: Fn(&mut W::Value) -> &mut V + Clone + 'static,
      V: ComposeChild<Child = Option<Child>> + 'static,
    {
      #[inline]
      fn build(self, ctx: &BuildCtx) -> Widget {
        ComposeChild::compose_child(self, None).build(ctx)
      }
    }
  };
}

impl_compose_builder!(MapWriter);
impl_compose_builder!(SplittedWriter);

#[cfg(test)]
mod tests {
  use std::cell::Cell;

  use ribir_algo::Sc;

  use super::*;
  use crate::{context::AppCtx, reset_test_env, timer::Timer};

  struct Origin {
    a: i32,
    b: i32,
  }

  #[test]
  fn map_same_with_origin() {
    reset_test_env!();

    let origin = State::value(Origin { a: 0, b: 0 });
    let map_state = map_writer!($origin.b);

    let track_origin = Sc::new(Cell::new(0));
    let track_map = Sc::new(Cell::new(0));

    let c_origin = track_origin.clone();
    origin.modifies().subscribe(move |_| {
      c_origin.set(c_origin.get() + 1);
    });

    let c_map = track_map.clone();
    map_state.modifies().subscribe(move |_| {
      c_map.set(c_map.get() + 1);
    });

    origin.write().a = 1;
    Timer::wake_timeout_futures();
    AppCtx::run_until_stalled();

    assert_eq!(track_origin.get(), 1);
    assert_eq!(track_map.get(), 1);

    *map_state.write() = 1;
    Timer::wake_timeout_futures();
    AppCtx::run_until_stalled();

    assert_eq!(track_origin.get(), 2);
    assert_eq!(track_map.get(), 2);
  }

  #[test]
  fn split_not_notify_origin() {
    reset_test_env!();

    let origin = State::value(Origin { a: 0, b: 0 });
    let split = split_writer!($origin.b);

    let track_origin = Sc::new(Cell::new(0));
    let track_split = Sc::new(Cell::new(0));

    let c_origin = track_origin.clone();
    origin.modifies().subscribe(move |s| {
      c_origin.set(c_origin.get() + s.bits());
    });

    let c_split = track_split.clone();
    split.modifies().subscribe(move |s| {
      c_split.set(c_split.get() + s.bits());
    });

    *split.write() = 0;
    Timer::wake_timeout_futures();
    AppCtx::run_until_stalled();

    assert_eq!(track_origin.get(), ModifyScope::DATA.bits());
    assert_eq!(track_split.get(), ModifyScope::BOTH.bits());

    origin.write().b = 0;
    Timer::wake_timeout_futures();
    AppCtx::run_until_stalled();
    assert_eq!(
      track_origin.get(),
      ModifyScope::DATA.bits() + ModifyScope::BOTH.bits()
    );
    // splitted downstream will not be notified.
    assert_eq!(track_split.get(), ModifyScope::BOTH.bits());
  }

  #[test]
  #[should_panic]
  fn invalid_split_after_origin_modify() {
    reset_test_env!();

    let origin = State::value(Origin { a: 0, b: 0 });
    let split = split_writer!($origin.b);

    origin.write().b = 1;
    // invalid split state
    *split.write() = 1;
  }

  struct C;

  impl Compose for C {
    fn compose(_: impl StateWriter<Value = Self>) -> impl WidgetBuilder {
      fn_widget! { Void }
    }
  }

  #[test]
  fn state_writer_compose_builder() {
    let _state_compose_widget = fn_widget! {
      State::value(C)
    };

    let _sateful_compose_widget = fn_widget! {
      Stateful::new(C)
    };

    let _writer_compose_widget = fn_widget! {
      Stateful::new(C).clone_writer()
    };

    let _map_writer_compose_widget = fn_widget! {
      let s = Stateful::new((C, 0));
      map_writer!($s.0)
    };
    let _split_writer_compose_widget = fn_widget! {
      let s = Stateful::new((C, 0));
      split_writer!($s.0)
    };
  }

  struct CC;
  impl ComposeChild for CC {
    type Child = Option<Widget>;
    fn compose_child(_: impl StateWriter<Value = Self>, _: Self::Child) -> impl WidgetBuilder {
      fn_widget! { @{ Void } }
    }
  }

  #[test]
  fn state_writer_compose_child_builder() {
    let _state_with_child = fn_widget! {
      let cc = State::value(CC);
      @$cc { @{ Void } }
    };

    let _state_without_child = fn_widget! {
      State::value(CC)
    };

    let _stateful_with_child = fn_widget! {
      let cc = Stateful::new(CC);
      @$cc { @{ Void } }
    };

    let _stateful_without_child = fn_widget! {
      Stateful::new(CC)
    };

    let _writer_with_child = fn_widget! {
      let cc = Stateful::new(CC).clone_writer();
      @$cc { @{ Void } }
    };

    let _writer_without_child = fn_widget! {
      Stateful::new(CC).clone_writer()
    };

    let _map_writer_with_child = fn_widget! {
      let s = Stateful::new((CC, 0));
      let w = map_writer!($s.0);
      @$w { @{ Void } }
    };

    let _map_writer_without_child = fn_widget! {
      let s = Stateful::new((CC, 0));
      map_writer!($s.0)
    };

    let _split_writer_with_child = fn_widget! {
      let s = Stateful::new((CC, 0));
      let w = split_writer!($s.0);
      @$w { @{ Void } }
    };

    let _split_writer_without_child = fn_widget! {
      let s = Stateful::new((CC, 0));
      split_writer!($s.0)
    };
  }

  #[test]
  fn state_reader_builder() {
    let _state_render_widget = fn_widget! {
      State::value(Void)
    };

    let _stateful_render_widget = fn_widget! {
      Stateful::new(Void)
    };

    let _reader_render_widget = fn_widget! {
      Stateful::new(Void).clone_reader()
    };

    let _writer_render_widget = fn_widget! {
      Stateful::new(Void).clone_writer()
    };

    let _map_reader_render_widget = fn_widget! {
      Stateful::new((Void, 0)).map_reader(|v| &v.0)
    };

    let _map_writer_render_widget = fn_widget! {
      let s = Stateful::new((Void, 0));
      map_writer!($s.0)
    };

    let _split_writer_render_widget = fn_widget! {
      let s = Stateful::new((Void, 0));
      split_writer!($s.0)
    };
  }

  use crate::state::{StateReader, StateWriter};

  #[test]
  fn reader_from_trait() {
    reset_test_env!();
    struct A {}
    trait T {
      fn test(&self);
      fn test_mut(&mut self);
    }

    impl T for A {
      fn test(&self) {}
      fn test_mut(&mut self) {}
    }

    let a = State::value(A {});
    let split_writer = a.split_writer(|a| a as &dyn T, |a| a as &mut dyn T);
    split_writer.read().test();
    split_writer.write().test_mut();

    let map_writer = a.map_writer(|a| a as &dyn T, |a| a as &mut dyn T);
    let map_reader = a.map_reader(|a| a as &dyn T);
    map_reader.read().test();
    map_writer.write().test_mut();
    AppCtx::run_until_stalled();
  }
}