futures-rx 0.1.23

Rx implementations for the futures crate
Documentation
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
# futures-rx: lightweight Rx implementation built upon `futures::Stream`

[![](https://github.com/frankpepermans/rxrs/actions/workflows/main.yml/badge.svg)](https://github.com/frankpepermans/rxrs)
[![](https://docs.rs/futures-rx/badge.svg)](https://docs.rs/futures-rx/latest/futures_rx/stream_ext/trait.RxExt.html)
[![](https://img.shields.io/crates/v/futures-rx.svg)](https://crates.io/crates/futures-rx)
[![](https://img.shields.io/crates/d/futures-rx.svg)](https://crates.io/crates/futures-rx)

## Subjects


Subjects are `Stream` controllers, that allow pushing new events to them, comparable to collections.
You can subscribe to them, which returns an `Observable`, which just implements `Stream`.

This `Observable` can be polled, but all items are wrapped in an `Event` struct,
which internally handles an `Rc` containing a reference to the actual item.

The subjects are:
- `PublishSubject`
- `BehaviorSubject`
- `ReplaySubject`

Subjects are hot observables, meaning you can subscribe to them as much as you like and at any point in time,
but you will miss out on items that have been polled _before_ subscribing.

`PublishSubject` is the default version, acting as explained above.
However, a `BehaviorSubject` will always replay the last emitted item to any new subscription
and `ReplaySubject` will replay _all_ events from the beginning. `ReplaySubject` can also take a buffer size, to avoid memory issues when dealing with massive amounts of events.

```rust
let mut subject = BehaviorSubject::new();

subject.next(1);
subject.next(2);
subject.next(3);
subject.close();

let obs = subject.subscribe();
// You can subscribe multiple times
let another_obs = subject.subscribe();

block_on(async {
    // Since Subjects allow for multiple subscribers, events are
    // wrapped in Event types, which internally manage an Rc to the actual event.
    // Here, we just borrow the underlying value and deref it.
    let res = obs.map(|it| *it.borrow_value()).collect::<Vec<i32>>().await;

    assert_eq!(res, [3]);
});
```

## Combine


Currently there's 2 macro-generated `Stream` builders:
- `CombineLatest2`..`CombineLatest9`
- `Zip2`..`Zip9`

## CombineLatest

`CombineLatest` emits all latest items from n-`Stream`s

```rust
let s1 = stream::iter([1, 2, 3]);
let s2 = stream::iter([6, 7, 8, 9]);
let s3 = stream::iter([0]);
let stream = CombineLatest3::new(s1, s2, s3);

block_on(async {
    let res = stream.collect::<Vec<_>>().await;

    assert_eq!(res, [(1, 6, 0), (2, 7, 0), (3, 8, 0), (3, 9, 0),]);
});
```

## Zip

`Zip` is similar, but instead emits all combined items by sequence:

```rust
let s1 = stream::iter([1, 2, 3]);
let s2 = stream::iter([6, 7, 8, 9]);
let stream = Zip2::new(s1, s2);

block_on(async {
    let res = stream.collect::<Vec<_>>().await;

    assert_eq!(res, [(1, 6), (2, 7), (3, 8),]);
});
```

## Ops


futures-rx also exposes the `RxExt` trait, which, like `StreamExt`, provides typical Rx transformers.

Note that a lot of other Rx operators are already part of the `futures::StreamExt` trait. This crate will only ever contain Rx operators that are missing from `StreamExt`.
Do use both `StreamExt` and `RxExt` to access all.

Currently this crate supports:
- `buffer`
- `debounce`
- `delay`
- `delay_every`
- `dematerialize`
- `distinct`
- `distinct_until_changed`
- `end_with`
- `inspect_done`
- `materialize`
- `pairwise`
- `race`
- `sample`
- `share`
- `share_behavior`
- `share_replay`
- `start_with`
- `switch_map`
- `timing`
- `throttle`
- `throttle_trailing`
- `throttle_all`
- `window`
- `with_latest_from`

## buffer

```rust
futures::executor::block_on(async {
    use futures::stream::{self, StreamExt};
    use futures_rx::RxExt;

    let stream = stream::iter(0..9);
    let stream = stream.window(|_, count| async move { count == 3 }).flat_map(|it| it);

    assert_eq!(vec![0, 1, 2, 3, 4, 5, 6, 7, 8], stream.collect::<Vec<_>>().await);
});
```

## debounce

```rust
futures::executor::block_on(async {
    stream
        .debounce(|_| Duration::from_millis(150).into_future())
        .collect::<Vec<_>>()
        .await;
});
```

## delay

```rust
futures::executor::block_on(async {
    let now = SystemTime::now();
    let all_events = stream::iter(0..=3)
        .delay(|| Duration::from_millis(100).into_future())
        .collect::<Vec<_>>()
        .await;

    assert_eq!(all_events, [0, 1, 2, 3]);
    assert!(now.elapsed().unwrap().as_millis() >= 100);
});    
```

## delay_every

```rust
futures::executor::block_on(async {
    let now = Instant::now();
    let all_events = stream::iter(0..=3)
        .delay_every(|_| Duration::from_millis(50).into_future(), None)
        .collect::<Vec<_>>()
        .await;

    assert_eq!(all_events, [0, 1, 2, 3]);
    assert!(now.elapsed().as_millis() >= 50 * 4);
});    
```

## dematerialize

```rust
futures::executor::block_on(async {
    let stream = stream::iter(1..=2);
    let all_events = stream
        .materialize()
        .dematerialize()
        .collect::<Vec<_>>()
        .await;

    assert_eq!(all_events, [1, 2]);
});    
```

## distinct

```rust
futures::executor::block_on(async {
    let stream = stream::iter([1, 1, 2, 1, 3, 2, 4, 5]);
    let all_events = stream.distinct().collect::<Vec<_>>().await;

    assert_eq!(all_events, [1, 2, 3, 4, 5]);
});    
```

## distinct_until_changed

```rust
futures::executor::block_on(async {
    let stream = stream::iter([1, 1, 2, 3, 3, 3, 4, 5]);
    let all_events = stream.distinct_until_changed().collect::<Vec<_>>().await;

    assert_eq!(all_events, [1, 2, 3, 4, 5]);
});    
```

## end_with

```rust
futures::executor::block_on(async {
    let stream = stream::iter(1..=5);
    let all_events = stream.end_with([0]).collect::<Vec<_>>().await;

    assert_eq!(all_events, [1, 2, 3, 4, 5, 0]);
});    
```

## inspect_done

```rust
futures::executor::block_on(async {
    let mut is_done = false;
    let all_events = stream::iter(0..=3)
        .inspect_done(|| is_done = true)
        .collect::<Vec<_>>()
        .await;

    assert_eq!(all_events, [0, 1, 2, 3]);
    assert!(is_done);
});    
```

## materialize

```rust
futures::executor::block_on(async {
    let stream = stream::iter(1..=2);
    let all_events = stream.materialize().collect::<Vec<_>>().await;

    assert_eq!(
        all_events,
        [
            Notification::Next(1),
            Notification::Next(2),
            Notification::Complete
        ]
    );
});    
```

## pairwise

```rust
futures::executor::block_on(async {
    let stream = stream::iter(0..=5);
    let all_events = stream
        .pairwise()
        .map(|(prev, next)| (prev, *next))
        .collect::<Vec<_>>()
        .await;

    assert_eq!(all_events, [(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]);
});    
```

## race

```rust
futures::executor::block_on(async {
    let mut phase = 0usize;
    let fast_stream = stream::iter(["fast"]);
    let slow_stream = stream::poll_fn(move |_| {
        // let's make it slower by first emitting a Pending state
        phase += 1;

        match phase {
            1 => Poll::Pending,
            2 => Poll::Ready(Some("slow")),
            3 => Poll::Ready(None),
            _ => unreachable!(),
        }
    });
    let all_events = slow_stream.race(fast_stream).collect::<Vec<_>>().await;

    assert_eq!(all_events, ["fast"]);
});    
```

## sample

```rust
futures::executor::block_on(async {
    let stream = create_stream(); // produces over time, interval is 20ms
        .take(6)
        .enumerate()
        .map(|(index, _)| index);
    let sampler = futures_time::stream::interval(Duration::from_millis(50)).take(6);
    let all_events = stream.sample(sampler).collect::<Vec<_>>().await;

    assert_eq!(all_events, [1, 3, 5]);
});    
```

## share

## share_behavior

## share_replay

```rust
futures::executor::block_on(async {
    let stream = stream::iter(1usize..=3usize);
    let s1 = stream.share(); // first subscription
    let s2 = s1.clone(); // second subscription
    let (a, b) = join(s1.collect::<Vec<_>>(), s2.collect::<Vec<_>>()).await;

    // as s1 and s2 produce Events, which wrap an Rc
    // we can call into() on the test values to convert them into Events as well.
    assert_eq!(a, [1.into(), 2.into(), 3.into()]);
    assert_eq!(b, [1.into(), 2.into(), 3.into()]);
});    
```

## start_with

```rust
futures::executor::block_on(async {
    let stream = stream::iter(1..=5);
    let all_events = stream.start_with([0]).collect::<Vec<_>>().await;

    assert_eq!(all_events, [0, 1, 2, 3, 4, 5]);
});    
```

## switch_map

```rust
futures::executor::block_on(async {
    let stream = stream::iter(0usize..=3usize);
    let all_events = stream
        .switch_map(|i| stream::iter([i.pow(2), i.pow(3), i.pow(4)]))
        .collect::<Vec<_>>()
        .await;

    assert_eq!(all_events, [0, 1, 4, 9, 27, 81]);
});    
```

## throttle

```rust
futures::executor::block_on(async {
    let stream = create_stream(); // produces 0..=9 over time, interval is 50ms
    let all_events = stream
        .throttle(|_| Duration::from_millis(175).into_future())
        .collect::<Vec<_>>()
        .await;

    assert_eq!(all_events, [0, 4, 8]);
});    
```

## throttle_trailing

```rust
futures::executor::block_on(async {
    let stream = create_stream(); // produces 0..=9 over time, interval is 50ms
    let all_events = stream
        .throttle_trailing(|_| Duration::from_millis(175).into_future())
        .collect::<Vec<_>>()
        .await;

    assert_eq!(all_events, [3, 7]);
});    
```

## throttle_all

```rust
futures::executor::block_on(async {
    let stream = create_stream(); // produces 0..=9 over time, interval is 50ms
    let all_events = stream
        .throttle_all(|_| Duration::from_millis(175).into_future())
        .collect::<Vec<_>>()
        .await;

    assert_eq!(all_events, [0, 3, 4, 7, 8]);
});    
```

## timing

```rust
futures::executor::block_on(async {
    let stream = create_stream(); // produces 0..=9 over time, interval is 50ms
    let start = Instant::now();
    let all_events = stream.timing().collect::<Vec<_>>().await;
    let timestamps = all_events
        .iter()
        .map(|it| it.timestamp)
        .enumerate()
        .collect::<Vec<_>>();
    let intervals = all_events
        .iter()
        .map(|it| it.interval)
        .enumerate()
        .collect::<Vec<_>>();

    for (index, timestamp) in timestamps {
        assert!(
            timestamp.duration_since(start).as_millis() >= (50 * index).try_into().unwrap()
        );
    }

    for (index, interval) in intervals {
        if index == 0 {
            assert!(interval.is_none());
        } else {
            assert!(interval.expect("interval is None!").as_millis() >= 50);
        }
    }
});    
```

## window

```rust
futures::executor::block_on(async {
    let all_events = stream::iter(0..=8)
        .window(|_, count| async move { count == 3 })
        .enumerate()
        .flat_map(|(index, it)| it.map(move |it| (index, it)))
        .collect::<Vec<_>>()
        .await;

    assert_eq!(
        all_events,
        vec![
            (0, 0),
            (0, 1),
            (0, 2),
            (1, 3),
            (1, 4),
            (1, 5),
            (2, 6),
            (2, 7),
            (2, 8)
        ]
    );
});    
```

## with_latest_from

```rust
futures::executor::block_on(async {
    let stream = stream::iter(0..=3);
    let stream = stream.with_latest_from(stream::iter(0..=3));

    assert_eq!(vec![(0, 0), (1, 1), (2, 2), (3, 3)], stream.collect::<Vec<_>>().await);
});    
```