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
//! Functional Reactive Programming library for Rust

#[cfg(feature="either")]
pub extern crate either;

use std::rc::Rc;
use std::cell::Cell;
use std::borrow::Cow;
use std::ptr;
use std::sync::{mpsc, Arc, RwLock};
use std::any::Any;
use std::ops::Deref;
use std::fmt;

mod types;
use types::Callbacks;
pub use types::SumType2;

mod helpers;
use helpers::{rc_and_weak, with_weak};

#[cfg(feature="either")]
use either::Either;

/// A source of events that feeds the streams connected to it.
#[derive(Debug, Clone)]
pub struct Sink<T: Clone>
{
    cbs: Rc<Callbacks<T>>,
}

impl<T: Clone> Sink<T>
{
    /// Creates a new sink.
    pub fn new() -> Self
    {
        Sink{ cbs: Rc::new(Callbacks::new()) }
    }

    /// Creates a stream that receives the events sent to this sink.
    pub fn stream(&self) -> Stream<T>
    {
        Stream{ cbs: self.cbs.clone(), source: None }
    }

    /// Sends a value into the sink.
    pub fn send(&self, val: T)
    {
        self.cbs.call(val)
    }

    /// Sends values from an Iterator into the sink.
    pub fn feed<I>(&self, iter: I)
        where I: IntoIterator<Item=T>
    {
        for val in iter
        {
            self.cbs.call(val)
        }
    }
}

/// A stream of discrete events sent over time.
///
/// All the streams returned by the methods below contain an internal reference to it's parent,
/// so dropping intermediate streams won't break the chain.
#[derive(Debug, Clone)]
pub struct Stream<T: Clone>
{
    cbs: Rc<Callbacks<T>>,
    source: Option<Rc<Any>>,  // strong reference to a parent Stream
}

impl<T: Clone + 'static> Stream<T>
{
    /// Maps this stream into another stream using the provided function.
    pub fn map<F, R>(&self, f: F) -> Stream<R>
        where F: Fn(Cow<T>) -> R + 'static,
        R: Clone + 'static
    {
        self.filter_map(move |arg| Some(f(arg)))
    }

    /// Creates a new stream that only contains the values where the predicate is `true`.
    pub fn filter<F>(&self, pred: F) -> Self
        where F: Fn(&T) -> bool + 'static
    {
        let (new_cbs, weak) = rc_and_weak(Callbacks::new());
        self.cbs.push(move |arg| {
            with_weak(&weak, |cb| if pred(&arg) { cb.call_cow(arg) })
        });
        Stream{ cbs: new_cbs, source: Some(Rc::new(self.clone())) }
    }

    /// Filter and map a stream simultaneously.
    pub fn filter_map<F, R>(&self, f: F) -> Stream<R>
        where F: Fn(Cow<T>) -> Option<R> + 'static,
        R: Clone + 'static
    {
        let (new_cbs, weak) = rc_and_weak(Callbacks::new());
        self.cbs.push(move |arg| {
            with_weak(&weak, |cb| if let Some(val) = f(arg) { cb.call(val) })
        });
        Stream{ cbs: new_cbs, source: Some(Rc::new(self.clone())) }
    }

    /// Creates a new stream that fires with the events from both streams.
    pub fn merge(&self, other: &Stream<T>) -> Self
    {
        let (new_cbs, weak1) = rc_and_weak(Callbacks::new());
        let weak2 = weak1.clone();
        self.cbs.push(move |arg| {
            with_weak(&weak1, |cb| cb.call_cow(arg))
        });
        other.cbs.push(move |arg| {
            with_weak(&weak2, |cb| cb.call_cow(arg))
        });
        Stream{ cbs: new_cbs, source: Some(Rc::new((self.clone(), other.clone()))) }
    }

    /// Merges two streams of different types using the provided function.
    #[cfg(feature="either")]
    pub fn merge_with<U, F, R>(&self, other: &Stream<U>, f: F) -> Stream<R>
        where F: Fn(Either<Cow<T>, Cow<U>>) -> R + 'static,
        U: Clone + 'static, R: Clone + 'static
    {
        let (new_cbs, weak1) = rc_and_weak(Callbacks::new());
        let weak2 = weak1.clone();
        let f1 = Rc::new(f);
        let f2 = f1.clone();
        self.cbs.push(move |arg| {
            with_weak(&weak1, |cb| cb.call(f1(Either::Left(arg))))
        });
        other.cbs.push(move |arg| {
            with_weak(&weak2, |cb| cb.call(f2(Either::Right(arg))))
        });
        Stream{ cbs: new_cbs, source: Some(Rc::new((self.clone(), other.clone()))) }
    }

    /// Reads the values without modifying them.
    ///
    /// This is meant to be used as a debugging tool and not to cause side effects.
    pub fn inspect<F>(self, f: F) -> Self
        where F: Fn(Cow<T>) + 'static
    {
        self.cbs.push(move |arg| { f(arg); true });
        self
    }

    /// Creates a channel and sends the stream events through it.
    pub fn channel(&self) -> mpsc::Receiver<T>
    {
        let (tx, rx) = mpsc::channel();
        self.cbs.push(move |arg| {
            tx.send(arg.into_owned()).is_ok()
        });
        rx
    }

    /// Creates a Signal that holds the last value sent to this stream.
    pub fn hold(&self, initial: T) -> SignalShared<T>
    {
        self.hold_if(initial, |_| true)
    }

    /// Holds the last value in this stream where the predicate is `true`.
    pub fn hold_if<F>(&self, initial: T, pred: F) -> SignalShared<T>
        where F: Fn(&T) -> bool + 'static
    {
        let storage = Arc::new(RwLock::new(initial));
        let weak = Arc::downgrade(&storage);
        self.cbs.push(move |arg| {
            weak.upgrade()
                .map(|st| if pred(&arg) { *st.write().unwrap() = arg.into_owned() })
                .is_some()
        });

        SignalShared(storage, Some(Rc::new(self.clone())))
    }

    /// Accumulates the values sent over this stream.
    pub fn fold<A, F>(&self, initial: A, f: F) -> SignalShared<A>
        where F: Fn(A, Cow<T>) -> A + 'static,
        A: Clone + 'static
    {
        let storage = Arc::new(RwLock::new(initial));
        let weak = Arc::downgrade(&storage);
        self.cbs.push(move |arg| {
            weak.upgrade()
                .map(|st| unsafe {
                    let acc = &mut *st.write().unwrap();
                    let old = ptr::read(acc);
                    let new = f(old, arg);
                    ptr::write(acc, new);
                })
                .is_some()
        });

        SignalShared(storage, Some(Rc::new(self.clone())))
    }

    /// Maps each stream event to `0..N` output values.
    ///
    /// The closure must return it's value by sending it through the provided sink.
    /// Multiple values (or none) can be sent to the output stream this way.
    ///
    /// This primitive is useful to construct asynchronous operations, since you can
    /// store the sink for later usage.
    pub fn map_n<F, R>(&self, f: F) -> Stream<R>
        where F: Fn(Cow<T>, Sink<R>) + 'static,
        R: Clone + 'static
    {
        let (new_cbs, weak) = rc_and_weak(Callbacks::new());
        self.cbs.push(move |arg| {
            with_weak(&weak, |cb| f(arg, Sink{ cbs: cb }))
        });
        Stream{ cbs: new_cbs, source: Some(Rc::new(self.clone())) }
    }
}

impl<T: Clone + 'static> Stream<Option<T>>
{
    /// Filters a stream of `Option`, returning the unwrapped `Some` values
    pub fn filter_some(&self) -> Stream<T>
    {
        self.filter_first()
    }
}

impl<T: SumType2 + Clone + 'static> Stream<T>
    where T::Type1: Clone + 'static, T::Type2: Clone + 'static
{
    /// Creates a stream with only the first element of a sum type
    pub fn filter_first(&self) -> Stream<T::Type1>
    {
        self.filter_map(|res| if res.is_type1() { res.into_owned().into_type1() } else { None })
    }

    /// Creates a stream with only the second element of a sum type
    pub fn filter_second(&self) -> Stream<T::Type2>
    {
        self.filter_map(|res| if res.is_type2() { res.into_owned().into_type2() } else { None })
    }

    /// Splits a two element sum type stream into two streams with the unwrapped values
    pub fn split(&self) -> (Stream<T::Type1>, Stream<T::Type2>)
    {
        let (cbs_1, weak_1) = rc_and_weak(Callbacks::new());
        let (cbs_2, weak_2) = rc_and_weak(Callbacks::new());
        self.cbs.push(move |result| {
            match (result.is_type1(), weak_1.upgrade(), weak_2.upgrade()) {
                (true, Some(cb), _) => { cb.call(result.into_owned().into_type1().unwrap()); true },
                (false, _, Some(cb)) => { cb.call(result.into_owned().into_type2().unwrap()); true },
                (_, None, None) => return false,  // both output steams dropped, drop this callback
                _ => true,  // sent to a dropped stream, but the other is still alive. keep this callback
            }
        });
        let source_rc = Rc::new(self.clone());
        let stream_1 = Stream{ cbs: cbs_1, source: Some(source_rc.clone()) };
        let stream_2 = Stream{ cbs: cbs_2, source: Some(source_rc) };
        (stream_1, stream_2)
    }
}

impl<T: Clone + 'static> Stream<Stream<T>>
{
    /// Listens to the events from the last stream sent to a nested stream
    pub fn switch(&self) -> Stream<T>
    {
        let (new_cbs, weak) = rc_and_weak(Callbacks::new());
        let id = Rc::new(Cell::new(0u64));  // id of each stream sent
        self.cbs.push(move |stream| {
            if weak.upgrade().is_none() { return false }
            let cbs_w = weak.clone();
            let cur_id = id.clone();
            // increment the id so it will only send to the last stream
            let my_id = id.get() + 1;
            id.set(my_id);
            // redirect the inner stream to the output stream
            stream.cbs.push(move |arg| {
                if my_id != cur_id.get() { return false }
                with_weak(&cbs_w, |cb| cb.call_cow(arg))
            });
            true
        });
        Stream{ cbs: new_cbs, source: Some(Rc::new(self.clone())) }
    }
}

/// Represents a continuous value that changes over time.
pub trait Signal<T>: Clone + 'static
{
    /// Sample by value.
    ///
    /// This will clone the content of the signal.
    fn sample(&self) -> T;

    /// Sample by reference.
    ///
    /// This is meant to be the most efficient way when cloning is undesirable,
    /// but it requires a callback to prevent outliving internal `RwLock` borrows.
    fn sample_with<F, R>(&self, cb: F) -> R
        where F: FnOnce(Cow<T>) -> R;

    /// Maps a signal with the provided function.
    fn map<F, R>(&self, f: F) -> SignalFn<R>
        where F: Fn(Cow<T>) -> R + 'static,
        R: Clone, T: Clone + 'static
    {
        let this = self.clone();
        SignalFn::new(move || this.sample_with(|val| f(val)))
    }

    /// Samples the value of this signal every time the trigger stream fires.
    fn snapshot<S, F, R>(&self, trigger: &Stream<S>, f: F) -> Stream<R>
        where F: Fn(Cow<T>, Cow<S>) -> R + 'static,
        S: Clone + 'static, R: Clone + 'static, T: Clone + 'static
    {
        let this = self.clone();
        trigger.map(move |b| this.sample_with(|a| f(a, b)))
    }

    /// Creates a new signal that samples the inner value of a nested signal.
    fn switch<U>(&self) -> SignalNested<U>
        where T: Signal<U> + Into<SignalAny<U>>, U: Clone
    {
        let this = self.clone();
        SignalNested(Rc::new(move || this.sample().into()))
    }
}

/// A signal with constant value.
#[derive(Debug, Clone)]
pub struct SignalConst<T>(pub T);

impl<T: Clone + 'static> Signal<T> for SignalConst<T>
{
    fn sample(&self) -> T
    {
        self.0.clone()
    }

    fn sample_with<F, R>(&self, cb: F) -> R
        where F: FnOnce(Cow<T>) -> R
    {
        cb(Cow::Borrowed(&self.0))
    }
}

impl<T> From<T> for SignalConst<T>
{
    fn from(val: T) -> Self
    {
        SignalConst(val)
    }
}

impl<T> Deref for SignalConst<T>
{
    type Target = T;

    fn deref(&self) -> &Self::Target
    {
        &self.0
    }
}

/// A signal that reads from shared data.
///
/// This is produced by stream methods that create a signal.
/// It also contains a reference to it's parent stream to avoid it's deletion.
#[derive(Debug, Clone)]
pub struct SignalShared<T>(Arc<RwLock<T>>, Option<Rc<Any>>);

impl<T> SignalShared<T>
{
    /// Returns the internal shared value.
    ///
    /// The returned value can be moved across threads and converted back into a `SignalShared`.
    /// This also drops the reference to it's parent signal, so it can delete the signal
    /// chain as a side effect.
    pub fn into_inner(self) -> Arc<RwLock<T>>
    {
        self.0
    }
}

impl<T: Clone + 'static> Signal<T> for SignalShared<T>
{
    fn sample(&self) -> T
    {
        self.0.read().unwrap().clone()
    }

    fn sample_with<F, R>(&self, cb: F) -> R
        where F: FnOnce(Cow<T>) -> R
    {
        cb(Cow::Borrowed(&self.0.read().unwrap()))
    }
}

impl<T> From<Arc<RwLock<T>>> for SignalShared<T>
{
    fn from(val: Arc<RwLock<T>>) -> Self
    {
        SignalShared(val, None)
    }
}

/// A signal that generates it's values from a function.
///
/// This is produced by `Signal::map`
#[derive(Clone)]
pub struct SignalFn<T>(Rc<Fn() -> T>);

impl<T> SignalFn<T>
{
    /// Creates a signal that samples it's values from the supplied function.
    pub fn new<F>(f: F) -> Self
        where F: Fn() -> T + 'static
    {
        SignalFn(Rc::new(f))
    }
}

impl<T: Clone + 'static> Signal<T> for SignalFn<T>
{
    fn sample(&self) -> T
    {
        (self.0)()
    }

    fn sample_with<F, R>(&self, cb: F) -> R
        where F: FnOnce(Cow<T>) -> R
    {
        cb(Cow::Owned((self.0)()))
    }
}

impl<T> fmt::Debug for SignalFn<T>
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
    {
        write!(f, "SignalFn(Fn)")
    }
}

/// A signal that contains a signal, and allows sampling the inner signal directly.
///
/// This is produced by `Signal::switch`
#[derive(Clone)]
pub struct SignalNested<T>(Rc<Fn() -> SignalAny<T>>);

impl<T: Clone + 'static> Signal<T> for SignalNested<T>
{
    fn sample(&self) -> T
    {
        (self.0)().sample()
    }

    fn sample_with<F, R>(&self, cb: F) -> R
        where F: FnOnce(Cow<T>) -> R
    {
        (self.0)().sample_with(cb)
    }
}

impl<T> fmt::Debug for SignalNested<T>
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result
    {
        write!(f, "SignalNested(Fn)")
    }
}

/// Generalized signal type.
///
/// All other signal subtypes convert to this, so you can say `S: Into<SignalAny<T>>` on your
/// functions that accept signals. It's also the recommended way of storing signals on structs.
#[derive(Debug, Clone)]
pub enum SignalAny<T>
{
    Constant(SignalConst<T>),
    Shared(SignalShared<T>),
    Dynamic(SignalFn<T>),
    Nested(SignalNested<T>),
}

impl<T> SignalAny<T>
{
    pub fn constant(val: T) -> Self
    {
        SignalAny::Constant(SignalConst(val))
    }

    pub fn from_fn<F>(f: F) -> Self
        where F: Fn() -> T + 'static
    {
        SignalAny::Dynamic(SignalFn::new(f))
    }
}

impl<T: Clone + 'static> Signal<T> for SignalAny<T>
{
    fn sample(&self) -> T
    {
        match *self
        {
            SignalAny::Constant(ref s) => s.sample(),
            SignalAny::Shared(ref s) => s.sample(),
            SignalAny::Dynamic(ref s) => s.sample(),
            SignalAny::Nested(ref s) => s.sample(),
        }
    }

    fn sample_with<F, R>(&self, cb: F) -> R
        where F: FnOnce(Cow<T>) -> R
    {
        match *self
        {
            SignalAny::Constant(ref s) => s.sample_with(cb),
            SignalAny::Shared(ref s) => s.sample_with(cb),
            SignalAny::Dynamic(ref s) => s.sample_with(cb),
            SignalAny::Nested(ref s) => s.sample_with(cb),
        }
    }
}

impl<T> From<T> for SignalAny<T>
{
    fn from(val: T) -> Self
    {
        SignalAny::constant(val)
    }
}

impl<T> From<SignalConst<T>> for SignalAny<T>
{
    fn from(sig: SignalConst<T>) -> Self
    {
        SignalAny::Constant(sig)
    }
}

impl<T> From<SignalShared<T>> for SignalAny<T>
{
    fn from(sig: SignalShared<T>) -> Self
    {
        SignalAny::Shared(sig)
    }
}

impl<T> From<SignalFn<T>> for SignalAny<T>
{
    fn from(sig: SignalFn<T>) -> Self
    {
        SignalAny::Dynamic(sig)
    }
}

impl<T> From<SignalNested<T>> for SignalAny<T>
{
    fn from(sig: SignalNested<T>) -> Self
    {
        SignalAny::Nested(sig)
    }
}

#[cfg(test)]
mod tests;