-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathsignal.rs
1127 lines (954 loc) · 31.8 KB
/
signal.rs
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use internal::Map2;
use std::pin::{Pin, Unpin};
use futures_core::task::LocalWaker;
use futures_core::Poll;
use futures_core::future::Future;
use futures_core::stream::Stream;
use futures_util::stream;
use futures_util::stream::StreamExt;
use signal_vec::{VecDiff, SignalVec};
use pin_utils::{unsafe_pinned, unsafe_unpinned};
// TODO impl for AssertUnwindSafe and Pin ?
pub trait Signal {
type Item;
fn poll_change(self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>>;
}
// Copied from Future in the Rust stdlib
impl<'a, A: ?Sized + Signal + Unpin> Signal for &'a mut A {
type Item = A::Item;
#[inline]
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
A::poll_change(Pin::new(&mut **self), waker)
}
}
// Copied from Future in the Rust stdlib
impl<A: ?Sized + Signal + Unpin> Signal for Box<A> {
type Item = A::Item;
#[inline]
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
A::poll_change(Pin::new(&mut *self), waker)
}
}
pub trait SignalExt: Signal {
/// Creates a `Stream` which contains the values of `self`.
///
/// When the output `Stream` is spawned:
///
/// 1. It immediately outputs the current value of `self`.
///
/// 2. Whenever `self` changes it outputs the new value of `self`.
///
/// Like *all* of the `Signal` methods, `to_stream` might skip intermediate changes.
/// So you ***cannot*** rely upon it containing every intermediate change.
/// But you ***can*** rely upon it always containing the most recent change.
///
/// # Performance
///
/// This is ***extremely*** efficient: it is *guaranteed* constant time, and it does not do
/// any heap allocation.
#[inline]
fn to_stream(self) -> SignalStream<Self>
where Self: Sized {
SignalStream {
signal: self,
}
}
// TODO maybe remove this ?
#[inline]
fn to_future(self) -> SignalFuture<Self>
where Self: Sized {
SignalFuture {
signal: self,
value: None,
}
}
/// Creates a `Signal` which uses a closure to transform the value.
///
/// When the output `Signal` is spawned:
///
/// 1. It calls the closure with the current value of `self`.
///
/// 2. Then it puts the return value of the closure into the output `Signal`.
///
/// 3. Whenever `self` changes it repeats the above steps.
///
/// This happens automatically and efficiently.
///
/// It will call the closure at most once for each change in `self`.
///
/// Like *all* of the `Signal` methods, `map` might skip intermediate changes.
/// So you ***cannot*** rely upon the closure being called for every intermediate change.
/// But you ***can*** rely upon it always being called with the most recent change.
///
/// # Examples
///
/// Add `1` to the value:
///
/// ```rust
/// # use futures_signals::signal::{always, SignalExt};
/// # let input = always(1);
/// let mapped = input.map(|value| value + 1);
/// ```
///
/// `mapped` will always contain the current value of `input`, except with `1` added to it.
///
/// If `input` has the value `10`, then `mapped` will have the value `11`.
///
/// If `input` has the value `5`, then `mapped` will have the value `6`, etc.
///
/// ----
///
/// Formatting to a `String`:
///
/// ```rust
/// # use futures_signals::signal::{always, SignalExt};
/// # let input = always(1);
/// let mapped = input.map(|value| format!("{}", value));
/// ```
///
/// `mapped` will always contain the current value of `input`, except formatted as a `String`.
///
/// If `input` has the value `10`, then `mapped` will have the value `"10"`.
///
/// If `input` has the value `5`, then `mapped` will have the value `"5"`, etc.
///
/// # Performance
///
/// This is ***extremely*** efficient: it is *guaranteed* constant time, and it does not do
/// any heap allocation.
#[inline]
fn map<A, B>(self, callback: B) -> Map<Self, B>
where B: FnMut(Self::Item) -> A,
Self: Sized {
Map {
signal: self,
callback,
}
}
#[inline]
fn inspect<A, B>(self, callback: B) -> Inspect<Self, B>
where B: FnMut(&Self::Item),
Self: Sized {
Inspect {
signal: self,
callback,
}
}
/// Creates a `Signal` which uses a closure to transform the value.
///
/// This is exactly the same as `map`, except:
///
/// 1. It calls the closure with a mutable reference to the input value.
///
/// 2. If the new input value is the same as the old input value, it will ***not*** call the closure, instead
/// it will completely ignore the new value, like as if it never happened.
///
/// It uses the `PartialEq` implementation to determine whether the new value is the same as the old value.
///
/// It only keeps track of the most recent value: that means that it ***won't*** call the closure for consecutive
/// duplicates, however it ***will*** call the closure for non-consecutive duplicates.
///
/// Because `dedupe_map` has the same behavior as `map`, it is useful solely as a performance optimization.
///
/// # Performance
///
/// The performance is the same as `map`, except with an additional call to `eq`.
///
/// If the `eq` call is fast, then `dedupe_map` can be faster than `map`, because it doesn't call the closure
/// when the new and old values are the same, and it also doesn't update any child Signals.
///
/// On the other hand, if the `eq` call is slow, then `dedupe_map` is probably slower than `map`.
#[inline]
fn dedupe_map<A, B>(self, callback: B) -> DedupeMap<Self, B>
// TODO should this use & instead of &mut ?
where B: FnMut(&mut Self::Item) -> A,
Self: Sized {
DedupeMap {
old_value: None,
signal: self,
callback,
}
}
#[inline]
fn dedupe(self) -> Dedupe<Self> where Self: Sized {
Dedupe {
old_value: None,
signal: self,
}
}
#[inline]
fn dedupe_cloned(self) -> DedupeCloned<Self> where Self: Sized {
DedupeCloned {
old_value: None,
signal: self,
}
}
/// Creates a `Signal` which uses a closure to asynchronously transform the value.
///
/// When the output `Signal` is spawned:
///
/// 1. It calls the closure with the current value of `self`.
///
/// 2. The closure returns a `Future`. It waits for that `Future` to finish, and then
/// it puts the return value of the `Future` into the output `Signal`.
///
/// 3. Whenever `self` changes it repeats the above steps.
///
/// It will call the closure at most once for each change in `self`.
///
/// Because Signals must always have a current value, if the `Future` is not ready yet, then the
/// output `Signal` will start with the value `None`. When the `Future` finishes it then changes
/// to `Some`. This can be used to detect whether the `Future` has finished or not.
///
/// If `self` changes before the old `Future` is finished, it will cancel the old `Future`.
/// That means if `self` changes faster than the `Future`, then it will never output any values.
///
/// Like *all* of the `Signal` methods, `map_future` might skip intermediate changes.
/// So you ***cannot*** rely upon the closure being called for every intermediate change.
/// But you ***can*** rely upon it always being called with the most recent change.
///
/// # Examples
///
/// Call an asynchronous network API whenever the input changes:
///
/// ```rust
/// # extern crate futures_core;
/// # extern crate futures_util;
/// # extern crate futures_signals;
/// # use futures_signals::signal::{always, SignalExt};
/// # use futures_util::future::{ready, Ready};
/// # fn call_network_api(value: u32) -> Ready<()> { ready(()) }
/// # fn main() {
/// # let input = always(1);
/// #
/// let mapped = input.map_future(|value| call_network_api(value));
/// # }
/// ```
///
/// # Performance
///
/// This is ***extremely*** efficient: it does not do any heap allocation, and it has *very* little overhead.
///
/// Of course the performance will also depend upon the `Future` which is returned from the closure.
#[inline]
fn map_future<A, B>(self, callback: B) -> MapFuture<Self, A, B>
where A: Future,
B: FnMut(Self::Item) -> A,
Self: Sized {
MapFuture {
signal: Some(self),
future: None,
callback,
first: true,
}
}
/// Creates a `Signal` which uses a closure to filter and transform the value.
///
/// When the output `Signal` is spawned:
///
/// 1. The output `Signal` starts with the value `None`.
///
/// 2. It calls the closure with the current value of `self`.
///
/// 3. If the closure returns `Some`, then it puts the return value of the closure into the output `Signal`.
///
/// 4. If the closure returns `None`, then it does nothing.
///
/// 5. Whenever `self` changes it repeats steps 2 - 4.
///
/// The output `Signal` will only be `None` for the initial value. After that it will always be `Some`.
///
/// If the closure returns `Some` for the initial value, then the output `Signal` will never be `None`.
///
/// It will call the closure at most once for each change in `self`.
///
/// Like *all* of the `Signal` methods, `filter_map` might skip intermediate changes.
/// So you ***cannot*** rely upon the closure being called for every intermediate change.
/// But you ***can*** rely upon it always being called with the most recent change.
///
/// # Examples
///
/// Add `1` to the value, but only if the value is less than `5`:
///
/// ```rust
/// # use futures_signals::signal::{always, SignalExt};
/// # let input = always(1);
/// let mapped = input.filter_map(|value| {
/// if value < 5 {
/// Some(value + 1)
///
/// } else {
/// None
/// }
/// });
/// ```
///
/// If the initial value of `input` is `5` or greater then `mapped` will be `None`.
///
/// If the current value of `input` is `5` or greater then `mapped` will keep its old value.
///
/// Otherwise `mapped` will be `Some(input + 1)`.
///
/// # Performance
///
/// This is ***extremely*** efficient: it does not do any heap allocation, and it has *very* little overhead.
#[inline]
fn filter_map<A, B>(self, callback: B) -> FilterMap<Self, B>
where B: FnMut(Self::Item) -> Option<A>,
Self: Sized {
FilterMap {
signal: self,
callback,
first: true,
}
}
/// Creates a `Signal` which flattens `self`.
///
/// When the output `Signal` is spawned:
///
/// 1. It retrieves the current value of `self` (this value is also a `Signal`).
///
/// 2. Then it puts the current value of the inner `Signal` into the output `Signal`.
///
/// 3. Whenever the inner `Signal` changes it puts the new value into the output `Signal`.
///
/// 4. Whenever `self` changes it repeats the above steps.
///
/// This happens automatically and efficiently.
///
/// Like *all* of the `Signal` methods, `flatten` might skip intermediate changes.
/// So you ***cannot*** rely upon it containing every intermediate change.
/// But you ***can*** rely upon it always containing the most recent change.
///
/// # Performance
///
/// This is very efficient: it is *guaranteed* constant time, and it does not do
/// any heap allocation.
#[inline]
fn flatten(self) -> Flatten<Self>
where Self::Item: Signal,
Self: Sized {
Flatten {
signal: Some(self),
inner: None,
}
}
#[inline]
fn switch<A, B>(self, callback: B) -> Switch<Self, A, B>
where A: Signal,
B: FnMut(Self::Item) -> A,
Self: Sized {
Switch {
inner: self.map(callback).flatten()
}
}
#[inline]
// TODO file Rust bug about bad error message when `callback` isn't marked as `mut`
fn for_each<U, F>(self, callback: F) -> ForEach<Self, U, F>
where U: Future<Output = ()>,
F: FnMut(Self::Item) -> U,
Self: Sized {
// TODO a bit hacky
ForEach {
inner: SignalStream {
signal: self,
}.for_each(callback)
}
}
#[inline]
fn to_signal_vec(self) -> SignalSignalVec<Self>
where Self: Sized {
SignalSignalVec {
signal: self
}
}
#[inline]
fn wait_for(self, value: Self::Item) -> WaitFor<Self>
where Self::Item: PartialEq,
Self: Sized {
WaitFor {
signal: self,
value: value,
}
}
#[inline]
fn first(self) -> First<Self> where Self: Sized {
First {
signal: Some(self),
}
}
/// A convenience for calling `Signal::poll_change` on `Unpin` types.
#[inline]
fn poll_change_unpin(&mut self, waker: &LocalWaker) -> Poll<Option<Self::Item>> where Self: Unpin + Sized {
Pin::new(self).poll_change(waker)
}
}
// TODO why is this ?Sized
impl<T: ?Sized> SignalExt for T where T: Signal {}
// TODO make this into a method later
#[inline]
pub fn not<A>(signal: A) -> impl Signal<Item = bool>
where A: Signal<Item = bool> {
signal.map(|x| !x)
}
// TODO make this into a method later
// TODO use short-circuiting if the left signal returns false ?
#[inline]
pub fn and<A, B>(left: A, right: B) -> impl Signal<Item = bool>
where A: Signal<Item = bool>,
B: Signal<Item = bool> {
Map2::new(left, right, |a, b| *a && *b)
}
// TODO make this into a method later
// TODO use short-circuiting if the left signal returns true ?
#[inline]
pub fn or<A, B>(left: A, right: B) -> impl Signal<Item = bool>
where A: Signal<Item = bool>,
B: Signal<Item = bool> {
Map2::new(left, right, |a, b| *a || *b)
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct FromFuture<A> {
// TODO is this valid with pinned types ?
future: Option<A>,
first: bool,
}
impl<A> FromFuture<A> where A: Future {
unsafe_pinned!(future: Option<A>);
unsafe_unpinned!(first: bool);
}
impl<A> Unpin for FromFuture<A> where A: Unpin {}
impl<A> Signal for FromFuture<A> where A: Future {
type Item = Option<A::Output>;
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
// TODO is this valid with pinned types ?
match self.future().as_pin_mut().map(|future| future.poll(waker)) {
None => {
Poll::Ready(None)
},
Some(Poll::Ready(value)) => {
Pin::set(self.future(), None);
Poll::Ready(Some(Some(value)))
},
Some(Poll::Pending) => {
let first = self.first();
if *first {
*first = false;
Poll::Ready(Some(None))
} else {
Poll::Pending
}
},
}
}
}
#[inline]
pub fn from_future<A>(future: A) -> FromFuture<A> where A: Future {
FromFuture { future: Some(future), first: true }
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct FromStream<A> {
stream: A,
first: bool,
}
impl<A> FromStream<A> {
unsafe_pinned!(stream: A);
unsafe_unpinned!(first: bool);
}
impl<A> Unpin for FromStream<A> where A: Unpin {}
impl<A> Signal for FromStream<A> where A: Stream {
type Item = Option<A::Item>;
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
match self.stream().poll_next(waker) {
Poll::Ready(None) => {
Poll::Ready(None)
},
Poll::Ready(Some(value)) => {
Poll::Ready(Some(Some(value)))
},
Poll::Pending => {
let first = self.first();
if *first {
*first = false;
Poll::Ready(Some(None))
} else {
Poll::Pending
}
},
}
}
}
#[inline]
pub fn from_stream<A>(stream: A) -> FromStream<A> where A: Stream {
FromStream { stream, first: true }
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct Always<A> {
value: Option<A>,
}
impl<A> Unpin for Always<A> {}
impl<A> Signal for Always<A> {
type Item = A;
#[inline]
fn poll_change(mut self: Pin<&mut Self>, _: &LocalWaker) -> Poll<Option<Self::Item>> {
Poll::Ready(self.value.take())
}
}
#[inline]
pub fn always<A>(value: A) -> Always<A> {
Always {
value: Some(value),
}
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct First<A> {
signal: Option<A>,
}
impl<A> First<A> {
unsafe_pinned!(signal: Option<A>);
}
impl<A> Unpin for First<A> where A: Unpin {}
impl<A> Signal for First<A> where A: Signal {
type Item = A::Item;
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
// TODO maybe it's safe to replace this with take ?
if let Some(poll) = self.signal().as_pin_mut().map(|signal| signal.poll_change(waker)) {
Pin::set(self.signal(), None);
poll
} else {
Poll::Ready(None)
}
}
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct Switch<A, B, C> where A: Signal, C: FnMut(A::Item) -> B {
inner: Flatten<Map<A, C>>,
}
impl<A, B, C> Switch<A, B, C> where A: Signal, C: FnMut(A::Item) -> B {
unsafe_pinned!(inner: Flatten<Map<A, C>>);
}
// TODO is this correct ?
impl<A, B, C> Unpin for Switch<A, B, C> where A: Signal + Unpin, B: Unpin, C: FnMut(A::Item) -> B {}
impl<A, B, C> Signal for Switch<A, B, C>
where A: Signal,
B: Signal,
C: FnMut(A::Item) -> B {
type Item = B::Item;
#[inline]
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
self.inner().poll_change(waker)
}
}
#[derive(Debug)]
#[must_use = "Futures do nothing unless polled"]
pub struct ForEach<A, B, C> {
inner: stream::ForEach<SignalStream<A>, B, C>,
}
impl<A, B, C> ForEach<A, B, C> {
unsafe_pinned!(inner: stream::ForEach<SignalStream<A>, B, C>);
}
impl<A, B, C> Unpin for ForEach<A, B, C> where A: Signal + Unpin, B: Future<Output = ()> + Unpin, C: FnMut(A::Item) -> B {}
impl<A, B, C> Future for ForEach<A, B, C>
where A: Signal,
B: Future<Output = ()>,
C: FnMut(A::Item) -> B {
type Output = ();
#[inline]
fn poll(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Self::Output> {
self.inner().poll(waker)
}
}
#[derive(Debug)]
#[must_use = "Streams do nothing unless polled"]
pub struct SignalStream<A> {
signal: A,
}
impl<A> SignalStream<A> {
unsafe_pinned!(signal: A);
}
impl<A> Unpin for SignalStream<A> where A: Unpin {}
impl<A: Signal> Stream for SignalStream<A> {
type Item = A::Item;
#[inline]
fn poll_next(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
self.signal().poll_change(waker)
}
}
// TODO maybe remove this ?
#[derive(Debug)]
#[must_use = "Futures do nothing unless polled"]
pub struct SignalFuture<A> where A: Signal {
signal: A,
value: Option<A::Item>,
}
impl<A> SignalFuture<A> where A: Signal {
unsafe_pinned!(signal: A);
unsafe_unpinned!(value: Option<A::Item>);
}
impl<A> Unpin for SignalFuture<A> where A: Unpin + Signal {}
impl<A> Future for SignalFuture<A> where A: Signal {
type Output = A::Item;
#[inline]
fn poll(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Self::Output> {
loop {
return match self.signal().poll_change(waker) {
Poll::Ready(None) => {
Poll::Ready(self.value().take().unwrap())
},
Poll::Ready(Some(value)) => {
*self.value() = Some(value);
continue;
},
Poll::Pending => {
Poll::Pending
},
}
}
}
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct Map<A, B> {
signal: A,
callback: B,
}
impl<A, B> Map<A, B> {
unsafe_pinned!(signal: A);
unsafe_unpinned!(callback: B);
}
impl<A, B> Unpin for Map<A, B> where A: Unpin {}
impl<A, B, C> Signal for Map<A, B>
where A: Signal,
B: FnMut(A::Item) -> C {
type Item = C;
#[inline]
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
self.signal().poll_change(waker).map(|opt| opt.map(|value| self.callback()(value)))
}
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct Inspect<A, B> {
signal: A,
callback: B,
}
impl<A, B> Inspect<A, B> {
unsafe_pinned!(signal: A);
unsafe_unpinned!(callback: B);
}
impl<A, B> Unpin for Inspect<A, B> where A: Unpin {}
impl<A, B> Signal for Inspect<A, B>
where A: Signal,
B: FnMut(&A::Item) {
type Item = A::Item;
#[inline]
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
let poll = self.signal().poll_change(waker);
if let Poll::Ready(Some(ref value)) = poll {
self.callback()(value);
}
poll
}
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct MapFuture<A, B, C> {
signal: Option<A>,
future: Option<B>,
callback: C,
first: bool,
}
impl<A, B, C> MapFuture<A, B, C> {
unsafe_pinned!(signal: Option<A>);
unsafe_pinned!(future: Option<B>);
unsafe_unpinned!(callback: C);
unsafe_unpinned!(first: bool);
}
impl<A, B, C> Unpin for MapFuture<A, B, C> where A: Unpin, B: Unpin {}
impl<A, B, C> Signal for MapFuture<A, B, C>
where A: Signal,
B: Future,
C: FnMut(A::Item) -> B {
type Item = Option<B::Output>;
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
let mut done = false;
loop {
match self.signal().as_pin_mut().map(|signal| signal.poll_change(waker)) {
None => {
done = true;
},
Some(Poll::Ready(None)) => {
Pin::set(self.signal(), None);
done = true;
},
Some(Poll::Ready(Some(value))) => {
let value = Some(self.callback()(value));
Pin::set(self.future(), value);
continue;
},
Some(Poll::Pending) => {},
}
break;
}
match self.future().as_pin_mut().map(|future| future.poll(waker)) {
None => {},
Some(Poll::Ready(value)) => {
Pin::set(self.future(), None);
*self.first() = false;
return Poll::Ready(Some(Some(value)));
},
Some(Poll::Pending) => {
done = false;
},
}
let first = self.first();
if *first {
*first = false;
Poll::Ready(Some(None))
} else if done {
Poll::Ready(None)
} else {
Poll::Pending
}
}
}
#[derive(Debug)]
#[must_use = "Futures do nothing unless polled"]
pub struct WaitFor<A>
where A: Signal,
A::Item: PartialEq {
signal: A,
value: A::Item,
}
impl<A> WaitFor<A> where A: Signal, A::Item: PartialEq {
unsafe_pinned!(signal: A);
}
impl<A> Unpin for WaitFor<A> where A: Unpin + Signal, A::Item: PartialEq {}
impl<A> Future for WaitFor<A>
where A: Signal,
A::Item: PartialEq {
type Output = Option<A::Item>;
fn poll(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Self::Output> {
loop {
let poll = self.signal().poll_change(waker);
if let Poll::Ready(Some(ref value)) = poll {
if *value != self.value {
continue;
}
}
return poll;
}
}
}
#[derive(Debug)]
#[must_use = "SignalVecs do nothing unless polled"]
pub struct SignalSignalVec<A> {
signal: A,
}
impl<A> SignalSignalVec<A> {
unsafe_pinned!(signal: A);
}
impl<A> Unpin for SignalSignalVec<A> where A: Unpin {}
impl<A, B> SignalVec for SignalSignalVec<A>
where A: Signal<Item = Vec<B>> {
type Item = B;
#[inline]
fn poll_vec_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<VecDiff<Self::Item>>> {
self.signal().poll_change(waker).map(|opt| opt.map(|values| VecDiff::Replace { values }))
}
}
macro_rules! dedupe {
($signal:expr, $waker:expr, $value:expr, $pat:pat, $name:ident => $output:expr) => {
loop {
return match $signal.poll_change($waker) {
Poll::Ready(Some($pat)) => {
let has_changed = match $value {
Some(ref old_value) => *old_value != $name,
None => true,
};
if has_changed {
let output = $output;
*$value = Some($name);
Poll::Ready(Some(output))
} else {
continue;
}
},
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
}
};
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct DedupeMap<A, B> where A: Signal {
old_value: Option<A::Item>,
signal: A,
callback: B,
}
impl<A, B> DedupeMap<A, B> where A: Signal {
unsafe_unpinned!(old_value: Option<A::Item>);
unsafe_pinned!(signal: A);
unsafe_unpinned!(callback: B);
}
impl<A, B> Unpin for DedupeMap<A, B> where A: Unpin + Signal {}
impl<A, B, C> Signal for DedupeMap<A, B>
where A: Signal,
A::Item: PartialEq,
// TODO should this use & instead of &mut ?
// TODO should this use Fn instead ?
B: FnMut(&mut A::Item) -> C {
type Item = C;
// TODO should this use #[inline] ?
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
dedupe!(self.signal(), waker, self.old_value(), mut value, value => self.callback()(&mut value))
}
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct Dedupe<A> where A: Signal {
old_value: Option<A::Item>,
signal: A,
}
impl<A> Dedupe<A> where A: Signal {
unsafe_unpinned!(old_value: Option<A::Item>);
unsafe_pinned!(signal: A);
}
impl<A> Unpin for Dedupe<A> where A: Unpin + Signal {}
impl<A> Signal for Dedupe<A>
where A: Signal,
A::Item: PartialEq + Copy {
type Item = A::Item;
// TODO should this use #[inline] ?
fn poll_change(mut self: Pin<&mut Self>, waker: &LocalWaker) -> Poll<Option<Self::Item>> {
dedupe!(self.signal(), waker, self.old_value(), value, value => value)
}
}
#[derive(Debug)]
#[must_use = "Signals do nothing unless polled"]
pub struct DedupeCloned<A> where A: Signal {
old_value: Option<A::Item>,
signal: A,
}
impl<A> DedupeCloned<A> where A: Signal {
unsafe_unpinned!(old_value: Option<A::Item>);
unsafe_pinned!(signal: A);
}
impl<A> Unpin for DedupeCloned<A> where A: Unpin + Signal {}
impl<A> Signal for DedupeCloned<A>
where A: Signal,
A::Item: PartialEq + Clone {