flo_binding 3.0.0

Declarative binding library for Rust
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
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
use crate::*;

use flo_rope::*;

use futures::executor;
use futures::prelude::*;

use std::sync::*;

#[test]
fn mutable_rope_sends_changes_to_stream() {
    // Create a rope that copies changes from a mutable rope
    let mutable_rope        = RopeBindingMut::<usize, ()>::new();
    let mut rope_stream     = mutable_rope.follow_changes();

    // Write some data to the mutable rope
    mutable_rope.replace(0..0, vec![1, 2, 3, 4]);

    // Should get sent to the stream
    executor::block_on(async move {
        let next = rope_stream.next().await;

        assert!(next == Some(RopeAction::Replace(0..0, vec![1,2,3,4])));
    });
}

#[test]
fn watch_mutable_rope() {
    // Create a rope that copies changes from a mutable rope
    let mutable_rope        = RopeBindingMut::<usize, ()>::new();

    let change_count        = bind(0);
    let change_counter      = change_count.clone();
    let watcher             = mutable_rope.watch(notify(move || change_counter.set(change_counter.get() + 1)));
    let mut rope_stream     = mutable_rope.follow_changes();

    // Write some data to the mutable rope
    mutable_rope.replace(0..0, vec![1, 2, 3, 4]);

    // Watcher hasn't been read, so no changes
    executor::block_on(async { rope_stream.next().await });
    assert!(change_count.get() == 0);

    // Read from the watcher and replace again: should have some changes
    watcher.get();
    mutable_rope.replace(0..4, vec![1, 2, 3, 4, 5]);

    executor::block_on(async { rope_stream.next().await });
    assert!(change_count.get() == 1);

    // But we don't notify on another change until the watcher is read from again
    mutable_rope.replace(0..5, vec![1, 2, 3, 4]);

    executor::block_on(async { rope_stream.next().await });
    assert!(change_count.get() == 1);
}

#[test]
fn pull_from_mutable_binding() {
    // Create a rope that copies changes from a mutable rope
    let mutable_rope        = RopeBindingMut::<usize, ()>::new();
    let rope_copy           = RopeBinding::from_stream(mutable_rope.follow_changes());
    let mut rope_stream     = rope_copy.follow_changes();

    // Write some data to the mutable rope
    mutable_rope.replace(0..0, vec![1, 2, 3, 4]);

    // Wait for the change to arrive at the copy
    executor::block_on(async move {
        let next = rope_stream.next().await;
        assert!(next == Some(RopeAction::Replace(0..0, vec![1,2,3,4])))
    });

    // Read from the copy
    assert!(rope_copy.len() == 4);
    assert!(rope_copy.read_cells(0..4).collect::<Vec<_>>() == vec![1, 2, 3, 4]);
}

#[test]
fn concatenate_ropes() {
    // Create a LHS and RHS rope and a concatenation of both
    let lhs                 = RopeBindingMut::<usize, ()>::new();
    let rhs                 = RopeBindingMut::<usize, ()>::new();
    let chain               = lhs.chain(&rhs);

    // We need to wait for the changes to arrive on the concatenated rope to avoid racing when reading back
    let mut follow_chain    = chain.follow_changes();

    // Add to LHS
    lhs.replace(0..0, vec![1, 2, 3]);
    executor::block_on(async { follow_chain.next().await });
    println!("{:?}", chain.read_cells(0..3).collect::<Vec<_>>());
    assert!(chain.read_cells(0..3).collect::<Vec<_>>() == vec![1, 2, 3]);

    // Add to RHS
    rhs.replace(0..0, vec![10, 11, 12]);
    executor::block_on(async { follow_chain.next().await });
    println!("{:?}", chain.read_cells(0..6).collect::<Vec<_>>());
    assert!(chain.read_cells(0..6).collect::<Vec<_>>() == vec![1, 2, 3, 10, 11, 12]);

    // Edit LHS
    lhs.replace(1..2, vec![4, 5, 6]);
    executor::block_on(async { follow_chain.next().await });
    println!("{:?}", chain.read_cells(0..8).collect::<Vec<_>>());
    assert!(chain.read_cells(0..8).collect::<Vec<_>>() == vec![1, 4, 5, 6, 3, 10, 11, 12]);

    // Edit RHS
    rhs.replace(1..2, vec![20, 21, 22]);
    executor::block_on(async { follow_chain.next().await });
    println!("{:?}", chain.read_cells(0..10).collect::<Vec<_>>());
    assert!(chain.read_cells(0..10).collect::<Vec<_>>() == vec![1, 4, 5, 6, 3, 10, 20, 21, 22, 12]);
}

#[test]
fn map_ropes() {
    // Create a rope with some numbers in it
    let rope            = RopeBindingMut::<usize, ()>::new();
    rope.replace(0..0, vec![1, 2, 3]);

    // Create a mapped rope that adds one to the numbers
    let add_one         = rope.map(|val| val+1);

    // Check that it changes as the numbers change
    let mut follow_add  = add_one.follow_changes();

    executor::block_on(async { follow_add.next().await });
    assert!(add_one.read_cells(0..3).collect::<Vec<_>>() == vec![2, 3, 4]);

    rope.replace(1..1, vec![8, 9, 10]);
    executor::block_on(async { follow_add.next().await });
    assert!(add_one.read_cells(0..6).collect::<Vec<_>>() == vec![2, 9, 10, 11, 3, 4]);
}

#[test]
fn watch_computed_rope() {
    // Create a length binding and compute a rope from it
    let length          = bind(0);
    let length_copy     = length.clone();
    let rope            = RopeBinding::<_, ()>::computed(move || (0..length_copy.get()).into_iter().map(|idx| idx));

    // Follow a the rope changes so we can sync up with the changes
    let change_count    = bind(0);
    let change_counter  = change_count.clone();
    let watcher         = rope.watch(notify(move || change_counter.set(change_counter.get() + 1)));
    let mut follow_rope = rope.follow_changes();

    // Mess with the length
    length.set(1);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 1);
    assert!(change_count.get() == 0);

    watcher.get();
    length.set(3);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 3);
    assert!(change_count.get() == 1);

    length.set(2);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 2);
    assert!(change_count.get() == 1);

    watcher.get();
    length.set(10);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 10);
    assert!(change_count.get() == 2);
}

#[test]
fn computed_rope() {
    // Create a length binding and compute a rope from it
    let length          = bind(0);
    let length_copy     = length.clone();
    let rope            = RopeBinding::<_, ()>::computed(move || (0..length_copy.get()).into_iter().map(|idx| idx));

    // Follow a the rope changes so we can sync up with the changes
    let mut follow_rope = rope.follow_changes();

    // Mess with the length
    length.set(1);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 1);
    assert!(rope.read_cells(0..1).collect::<Vec<_>>() == vec![0]);

    length.set(3);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 3);
    assert!(rope.read_cells(0..3).collect::<Vec<_>>() == vec![0, 1, 2]);

    length.set(2);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 2);
    assert!(rope.read_cells(0..2).collect::<Vec<_>>() == vec![0, 1]);

    length.set(10);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 10);
    assert!(rope.read_cells(0..10).collect::<Vec<_>>() == vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}

#[test]
fn computed_rope_using_diffs_1() {
    // Create a length binding and compute a rope from it
    let length          = bind(0);
    let length_copy     = length.clone();
    let rope            = RopeBinding::<_, ()>::computed_difference(move || (0..length_copy.get()).into_iter().map(|idx| idx));

    // Follow a the rope changes so we can sync up with the changes
    let mut follow_rope = rope.follow_changes();

    // Mess with the length
    length.set(1);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 1);
    assert!(rope.read_cells(0..1).collect::<Vec<_>>() == vec![0]);

    length.set(3);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 3);
    assert!(rope.read_cells(0..3).collect::<Vec<_>>() == vec![0, 1, 2]);

    length.set(2);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 2);
    assert!(rope.read_cells(0..2).collect::<Vec<_>>() == vec![0, 1]);

    length.set(10);
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == 10);
    assert!(rope.read_cells(0..10).collect::<Vec<_>>() == vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
}

#[test]
fn computed_rope_using_diffs_2() {
    // Create a length binding and compute a rope from it
    let length          = bind(0);
    let length_copy     = length.clone();
    let rope            = RopeBinding::<_, ()>::computed_difference(move || (0..length_copy.get()).into_iter().map(|idx| idx));

    // Follow a the rope changes so we can sync up with the changes
    let mut follow_rope = rope.follow_changes();

    // Mess with the length
    length.set(1);
    let diff = executor::block_on(async { follow_rope.next().await });
    assert!(diff == Some(RopeAction::Replace(0..0, vec![0])));

    length.set(3);
    let diff = executor::block_on(async { follow_rope.next().await });
    assert!(diff == Some(RopeAction::Replace(1..1, vec![1, 2])));

    length.set(2);
    let diff = executor::block_on(async { follow_rope.next().await });
    assert!(diff == Some(RopeAction::Replace(2..3, vec![])));

    length.set(10);
    let diff = executor::block_on(async { follow_rope.next().await });
    assert!(diff == Some(RopeAction::Replace(2..2, vec![2, 3, 4, 5, 6, 7, 8, 9])));
}

#[test]
fn computed_rope_using_diffs_3() {
    // Bind a list of items
    let items           = bind(vec![]);
    let items_copy      = items.clone();
    let rope            = RopeBinding::<_, ()>::computed_difference(move || items_copy.get());

    // Follow a the rope changes so we can sync up with the changes
    let mut follow_rope = rope.follow_changes();

    // Update the items (as we're using diffs these will test various ways of sending them across)
    let val = vec![1, 1, 1, 1];
    items.set(val.clone());
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == val.len());
    assert!(rope.read_cells(0..val.len()).collect::<Vec<_>>() == val);

    let val = vec![1, 1, 2, 2, 1, 1, 3, 3];
    items.set(val.clone());
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == val.len());
    assert!(rope.read_cells(0..val.len()).collect::<Vec<_>>() == val);

    let val = vec![1, 1, 1, 1];
    items.set(val.clone());
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == val.len());
    assert!(rope.read_cells(0..val.len()).collect::<Vec<_>>() == val);

    let val = vec![1, 2, 1, 3, 3, 1, 4, 4, 4, 1, 5, 5, 5, 5];
    items.set(val.clone());
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == val.len());
    assert!(rope.read_cells(0..val.len()).collect::<Vec<_>>() == val);

    let val = vec![1, 1, 1, 1];
    items.set(val.clone());
    executor::block_on(async { follow_rope.next().await });
    assert!(rope.len() == val.len());
    assert!(rope.read_cells(0..val.len()).collect::<Vec<_>>() == val);
}

#[test]
fn bind_rope_length_to_computed() {
    // Create a rope
    let items           = bind(vec![]);
    let items_copy      = items.clone();
    let rope            = RopeBinding::<_, ()>::computed_difference(move || items_copy.get());

    // Computed binding containing the rope length
    let rope_copy       = rope.clone();
    let rope_length     = computed(move || rope_copy.len());

    // Follow the rope so we know when it's updated
    let mut follow_rope = rope.follow_changes();

    // Initial length is 0
    assert!(rope_length.get() == 0);

    let is_changed      = Arc::new(Mutex::new(false));
    let is_changed_copy = is_changed.clone();
    rope_length.when_changed(notify(move || *is_changed_copy.lock().unwrap() = true)).keep_alive();

    // Rope length should update to 4
    let val = vec![1, 1, 1, 1];
    items.set(val.clone());
    executor::block_on(async { follow_rope.next().await });

    // Reading the rope length forces synchronisation with the binding (otherwise there's a race condition here)
    assert!(rope.len() == 4);

    assert!(rope_length.get() == 4);
    assert!(*is_changed.lock().unwrap() == true);
}

#[test]
fn bind_rope_mut_length_to_computed() {
    // Create a rope
    let rope            = RopeBindingMut::<_, ()>::new();

    // Computed binding containing the rope length
    let rope_copy       = rope.clone();
    let rope_length     = computed(move || rope_copy.len());

    // Follow the rope so we know when it's updated
    let mut follow_rope = rope.follow_changes();

    // Initial length is 0
    assert!(rope_length.get() == 0);

    let is_changed      = Arc::new(Mutex::new(false));
    let is_changed_copy = is_changed.clone();
    rope_length.when_changed(notify(move || *is_changed_copy.lock().unwrap() = true)).keep_alive();

    // Rope length should update to 4
    let val = vec![1, 1, 1, 1];
    rope.replace(0..0, val);
    executor::block_on(async { follow_rope.next().await });

    rope.len();

    assert!(rope_length.get() == 4);
    assert!(*is_changed.lock().unwrap() == true);
}

#[test]
fn bind_rope_read_cells_to_computed() {
    // Create a rope
    let items           = bind(vec![]);
    let items_copy      = items.clone();
    let rope            = RopeBinding::<_, ()>::computed_difference(move || items_copy.get());

    // Computed binding containing the rope length
    let rope_copy       = rope.clone();
    let rope_cells      = computed(move || rope_copy.read_cells(0..2).collect::<Vec<_>>());

    // Follow the rope so we know when it's updated
    let mut follow_rope = rope.follow_changes();

    // Initial length is 0
    assert!(rope_cells.get() == vec![]);

    let is_changed      = Arc::new(Mutex::new(false));
    let is_changed_copy = is_changed.clone();
    rope_cells.when_changed(notify(move || *is_changed_copy.lock().unwrap() = true)).keep_alive();

    // Update the cells
    let val = vec![1, 1, 1, 1];
    items.set(val.clone());
    executor::block_on(async { follow_rope.next().await });

    rope.len();

    assert!(rope_cells.get() == vec![1,1]);
    assert!(*is_changed.lock().unwrap() == true);
}

#[test]
fn bind_rope_mut_read_cells_to_computed() {
    // Create a rope
    let rope            = RopeBindingMut::<_, ()>::new();

    // Computed binding containing the rope length
    let rope_copy       = rope.clone();
    let rope_cells      = computed(move || rope_copy.read_cells(0..2).collect::<Vec<_>>());

    // Follow the rope so we know when it's updated
    let mut follow_rope = rope.follow_changes();

    // Initial length is 0
    assert!(rope_cells.get() == vec![]);

    let is_changed      = Arc::new(Mutex::new(false));
    let is_changed_copy = is_changed.clone();
    rope_cells.when_changed(notify(move || *is_changed_copy.lock().unwrap() = true)).keep_alive();

    // Update the cells
    let val = vec![1, 1, 1, 1];
    rope.replace(0..0, val);
    executor::block_on(async { follow_rope.next().await });

    rope.len();

    assert!(rope_cells.get() == vec![1,1]);
    assert!(*is_changed.lock().unwrap() == true);
}

#[test]
fn following_rope_generates_when_changed() {
    // Create a rope
    let rope            = RopeBindingMut::<_, ()>::new();
    let following_rope  = RopeBinding::from_stream(rope.follow_changes());

    // Computed binding containing the rope length
    let rope_copy       = following_rope.clone();
    let rope_cells      = computed(move || rope_copy.read_cells(0..2).collect::<Vec<_>>());

    // Follow the rope so we know when it's updated
    let mut follow_rope = following_rope.follow_changes();

    // Initial length is 0
    assert!(rope_cells.get() == vec![]);

    let is_changed      = Arc::new(Mutex::new(false));
    let is_changed_copy = is_changed.clone();
    rope_cells.when_changed(notify(move || *is_changed_copy.lock().unwrap() = true)).keep_alive();

    // Update the cells
    let val = vec![1, 1, 1, 1];
    rope.replace(0..0, val);
    executor::block_on(async { follow_rope.next().await });

    rope.len();
    following_rope.len();

    assert!(*is_changed.lock().unwrap() == true);
    assert!(rope_cells.get() == vec![1,1]);
}

#[test]
fn retain_cells_1() {
    // Create a rope
    let rope = RopeBindingMut::<_, ()>::new();

    // Load with numbers from 0 to 10
    rope.replace(0..0, 0..10);

    // Retain the even numbers
    rope.retain_cells(|x| (x%2) == 0);

    // Rope should contain 5 even numbers
    assert!(rope.len() == 5);
    assert!(rope.read_cells(0..5).collect::<Vec<_>>() == vec![0, 2, 4, 6, 8]);
}

#[test]
fn retain_cells_2() {
    // Create a rope
    let rope = RopeBindingMut::<_, ()>::new();

    // Load with numbers from 0 to 10
    rope.replace(0..0, 0..10);

    // Retain the odd numbers
    rope.retain_cells(|x| (x%2) == 1);

    // Rope should contain 5 odd numbers
    assert!(rope.len() == 5);
    assert!(rope.read_cells(0..5).collect::<Vec<_>>() == vec![1, 3, 5, 7, 9]);
}

#[test]
fn retain_cells_3() {
    // Create a rope
    let rope = RopeBindingMut::<_, ()>::new();

    // Load with numbers from 0 to 10
    rope.replace(0..0, 0..10);

    // Retain the first and last numbers
    rope.retain_cells(|x| *x < 1 || *x > 8);

    // Rope should contain the first and the last numbers
    assert!(rope.len() == 2);
    assert!(rope.read_cells(0..5).collect::<Vec<_>>() == vec![0, 9]);
}

#[test]
fn retain_cells_4() {
    // Create a rope
    let rope = RopeBindingMut::<_, ()>::new();

    // Load with numbers from 0 to 10
    rope.replace(0..0, 0..10);

    // Retain the first numbers
    rope.retain_cells(|x| *x < 5);

    // Rope should contain the first 5 numbers
    assert!(rope.len() == 5);
    assert!(rope.read_cells(0..5).collect::<Vec<_>>() == vec![0, 1, 2, 3, 4]);
}

#[test]
fn retain_cells_5() {
    // Create a rope
    let rope = RopeBindingMut::<_, ()>::new();

    // Load with numbers from 0 to 10
    rope.replace(0..0, 0..10);

    // Retain the last numbers
    rope.retain_cells(|x| *x >= 5);

    // Rope should contain the last 5 numbers
    assert!(rope.len() == 5);
    assert!(rope.read_cells(0..5).collect::<Vec<_>>() == vec![5, 6, 7, 8, 9]);
}