Skip to main content

ReactiveCollection

Struct ReactiveCollection 

Source
pub struct ReactiveCollection<T, K, IdFn, Cmp, Flt = AcceptAll>
where T: Clone + PartialEq + 'static, K: Ord + Clone + 'static, IdFn: Fn(&T) -> K + 'static, Cmp: Fn(&T, &T) -> Ordering + 'static, Flt: ItemFilter<T> + 'static,
{ /* private fields */ }
Expand description

Reactive bridge wrapping zwire_collections::Collection with zwire_core::Scope.

Exposes Self::top_k_signal and Self::top_k_filtered_signal as first-class F_EXTERNAL reactive Computed<Vec<T>> signals with O(1) Top-K boundary pruning, batched single-pass materialization via core flush hooks, O(1) per-key Self::get_signal/Self::has_signal, and STM Draft compatibility.

Implementations§

Source§

impl<T, K, IdFn, Cmp> ReactiveCollection<T, K, IdFn, Cmp, AcceptAll>
where T: Clone + PartialEq + 'static, K: Ord + Clone + 'static, IdFn: Fn(&T) -> K + 'static, Cmp: Fn(&T, &T) -> Ordering + 'static,

Source

pub fn new(scope: &Scope, get_id: IdFn, compare: Cmp) -> Self

Creates an unfiltered reactive incremental collection bound to scope.

Source§

impl<T, K, IdFn, Cmp, Flt> ReactiveCollection<T, K, IdFn, Cmp, Flt>
where T: Clone + PartialEq + 'static, K: Ord + Clone + 'static, IdFn: Fn(&T) -> K + 'static, Cmp: Fn(&T, &T) -> Ordering + 'static, Flt: Fn(&T) -> bool + 'static,

Source

pub fn with_filter( scope: &Scope, get_id: IdFn, compare: Cmp, filter: Flt, ) -> Self

Creates a filtered reactive incremental collection bound to scope.

Examples found in repository?
examples/orderbook_optimistic.rs (lines 22-27)
18fn main() {
19    let scope = Scope::new();
20
21    // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22    let bids = ReactiveCollection::with_filter(
23        &scope,
24        |b: &Bid| b.id,
25        |a, b| b.price_cents.cmp(&a.price_cents),
26        |b| b.active && b.qty > 0,
27    );
28
29    bids.upsert_many((1..=10_000u64).map(|id| Bid {
30        id,
31        price_cents: i64::try_from(id).unwrap() * 10,
32        qty: 5,
33        active: true,
34    }));
35
36    let top5 = bids.top_k_signal(5);
37    let best_bid =
38        scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40    let renders = Rc::new(Cell::new(0usize));
41    let renders_c = Rc::clone(&renders);
42    let _sub = scope.subscribe(best_bid, move |price| {
43        renders_c.set(renders_c.get() + 1);
44        println!(
45            "[book] best bid updated -> ${}.{:02}",
46            price / 100,
47            price.abs() % 100
48        );
49    });
50
51    let init_price = scope.get(best_bid);
52    println!(
53        "Initial best bid across {} active bids: ${}.{:02}",
54        bids.indexed_len(),
55        init_price / 100,
56        init_price.abs() % 100
57    );
58
59    // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60    for id in 1..=100u64 {
61        bids.mutate(&id, |b| b.price_cents += 1);
62    }
63    assert_eq!(renders.get(), 0);
64    println!(
65        "After 100 off-top updates: {} UI renders (O(1) pruned)",
66        renders.get()
67    );
68
69    // New aggressive bid beats top-of-book -> triggers exactly 1 render
70    bids.upsert(Bid {
71        id: 99_999,
72        price_cents: 105_000,
73        qty: 12,
74        active: true,
75    });
76    assert_eq!(renders.get(), 1);
77
78    // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79    let available_usd = scope.atom(10_000i64);
80
81    let mut order_a = OptimisticLayer::new(&scope);
82    order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84    let mut order_b = OptimisticLayer::new(&scope);
85    order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86    println!(
87        "After 2 optimistic orders -> displayed balance: ${}",
88        scope.get(available_usd)
89    );
90
91    // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92    scope.set(available_usd, 15_000);
93    assert_eq!(scope.get(available_usd), 13_000);
94    println!(
95        "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96        scope.get(available_usd)
97    );
98
99    // Order A rejected by venue -> surgical revert preserves Order B
100    order_a.revert();
101    assert_eq!(scope.get(available_usd), 14_200);
102    println!(
103        "After Order A rejected -> balance: ${}",
104        scope.get(available_usd)
105    );
106
107    // Order B confirmed by venue -> atomic zero-flicker settle
108    order_b.settle_with(|| scope.set(available_usd, 14_200));
109    assert_eq!(scope.get(available_usd), 14_200);
110    println!(
111        "After Order B settled -> final committed balance: ${}",
112        scope.get(available_usd)
113    );
114}
Source§

impl<T, K, IdFn, Cmp, Flt> ReactiveCollection<T, K, IdFn, Cmp, Flt>
where T: Clone + PartialEq + 'static, K: Ord + Clone + 'static, IdFn: Fn(&T) -> K + 'static, Cmp: Fn(&T, &T) -> Ordering + 'static, Flt: ItemFilter<T> + 'static,

Source

pub fn from_parts( scope: &Scope, get_id: IdFn, compare: Cmp, filter: Flt, ) -> Self

Creates a reactive incremental collection with any ItemFilter<T> implementation.

Source

pub fn len(&self) -> usize

Total number of stored items.

Source

pub fn is_empty(&self) -> bool

Returns true if the collection stores no items.

Source

pub fn indexed_len(&self) -> usize

Number of items currently passing the filter and present in the sorted block index.

Examples found in repository?
examples/orderbook_optimistic.rs (line 54)
18fn main() {
19    let scope = Scope::new();
20
21    // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22    let bids = ReactiveCollection::with_filter(
23        &scope,
24        |b: &Bid| b.id,
25        |a, b| b.price_cents.cmp(&a.price_cents),
26        |b| b.active && b.qty > 0,
27    );
28
29    bids.upsert_many((1..=10_000u64).map(|id| Bid {
30        id,
31        price_cents: i64::try_from(id).unwrap() * 10,
32        qty: 5,
33        active: true,
34    }));
35
36    let top5 = bids.top_k_signal(5);
37    let best_bid =
38        scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40    let renders = Rc::new(Cell::new(0usize));
41    let renders_c = Rc::clone(&renders);
42    let _sub = scope.subscribe(best_bid, move |price| {
43        renders_c.set(renders_c.get() + 1);
44        println!(
45            "[book] best bid updated -> ${}.{:02}",
46            price / 100,
47            price.abs() % 100
48        );
49    });
50
51    let init_price = scope.get(best_bid);
52    println!(
53        "Initial best bid across {} active bids: ${}.{:02}",
54        bids.indexed_len(),
55        init_price / 100,
56        init_price.abs() % 100
57    );
58
59    // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60    for id in 1..=100u64 {
61        bids.mutate(&id, |b| b.price_cents += 1);
62    }
63    assert_eq!(renders.get(), 0);
64    println!(
65        "After 100 off-top updates: {} UI renders (O(1) pruned)",
66        renders.get()
67    );
68
69    // New aggressive bid beats top-of-book -> triggers exactly 1 render
70    bids.upsert(Bid {
71        id: 99_999,
72        price_cents: 105_000,
73        qty: 12,
74        active: true,
75    });
76    assert_eq!(renders.get(), 1);
77
78    // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79    let available_usd = scope.atom(10_000i64);
80
81    let mut order_a = OptimisticLayer::new(&scope);
82    order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84    let mut order_b = OptimisticLayer::new(&scope);
85    order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86    println!(
87        "After 2 optimistic orders -> displayed balance: ${}",
88        scope.get(available_usd)
89    );
90
91    // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92    scope.set(available_usd, 15_000);
93    assert_eq!(scope.get(available_usd), 13_000);
94    println!(
95        "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96        scope.get(available_usd)
97    );
98
99    // Order A rejected by venue -> surgical revert preserves Order B
100    order_a.revert();
101    assert_eq!(scope.get(available_usd), 14_200);
102    println!(
103        "After Order A rejected -> balance: ${}",
104        scope.get(available_usd)
105    );
106
107    // Order B confirmed by venue -> atomic zero-flicker settle
108    order_b.settle_with(|| scope.set(available_usd, 14_200));
109    assert_eq!(scope.get(available_usd), 14_200);
110    println!(
111        "After Order B settled -> final committed balance: ${}",
112        scope.get(available_usd)
113    );
114}
Source

pub fn get(&self, id: &K) -> Option<T>

Clones the stored item for id, if present.

Source

pub fn contains_key(&self, id: &K) -> bool

Returns true if id exists in the collection.

Source

pub fn get_top_k(&self, k: usize) -> Vec<T>

Returns a cloned snapshot of the current top k indexed items.

Source

pub fn get_range(&self, offset: usize, limit: usize) -> Vec<T>

Returns a sorted window, measured after filtering.

Source

pub fn iter_sorted(&self) -> IntoIter<T> ⓘ

Iterates over a detached snapshot in exact index order.

Source

pub fn range_signal(&self, offset: usize, limit: usize) -> Computed<Vec<T>>

Tracks a sorted window and suppresses equal snapshots.

Source

pub fn len_signal(&self) -> Computed<usize>

Tracks the number of stored rows.

Source

pub fn indexed_len_signal(&self) -> Computed<usize>

Tracks the number of indexed rows.

Source

pub fn get_signal(&self, id: K) -> Computed<Option<T>>

Tracks one row in O(1) per write, including insertions and deletions.

Source

pub fn has_signal(&self, id: K) -> Computed<bool>

Tracks membership in O(1) per write independently of the current index filter.

Source

pub fn set_ordering( &self, compare: impl Fn(&T, &T) -> Ordering + 'static, filter: impl Fn(&T) -> bool + 'static, ) -> bool

Rebuilds under a new comparator and filter. Returns false when a callback changed the collection while building the replacement.

Source

pub fn top_k_signal(&self, k: usize) -> Computed<Vec<T>>

Creates (or returns a cached live) first-class reactive Computed<Vec<T>> tracking the exact top k items.

Mutations strictly outside the K-th boundary skip materialization and propagation in O(1).

Examples found in repository?
examples/orderbook_optimistic.rs (line 36)
18fn main() {
19    let scope = Scope::new();
20
21    // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22    let bids = ReactiveCollection::with_filter(
23        &scope,
24        |b: &Bid| b.id,
25        |a, b| b.price_cents.cmp(&a.price_cents),
26        |b| b.active && b.qty > 0,
27    );
28
29    bids.upsert_many((1..=10_000u64).map(|id| Bid {
30        id,
31        price_cents: i64::try_from(id).unwrap() * 10,
32        qty: 5,
33        active: true,
34    }));
35
36    let top5 = bids.top_k_signal(5);
37    let best_bid =
38        scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40    let renders = Rc::new(Cell::new(0usize));
41    let renders_c = Rc::clone(&renders);
42    let _sub = scope.subscribe(best_bid, move |price| {
43        renders_c.set(renders_c.get() + 1);
44        println!(
45            "[book] best bid updated -> ${}.{:02}",
46            price / 100,
47            price.abs() % 100
48        );
49    });
50
51    let init_price = scope.get(best_bid);
52    println!(
53        "Initial best bid across {} active bids: ${}.{:02}",
54        bids.indexed_len(),
55        init_price / 100,
56        init_price.abs() % 100
57    );
58
59    // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60    for id in 1..=100u64 {
61        bids.mutate(&id, |b| b.price_cents += 1);
62    }
63    assert_eq!(renders.get(), 0);
64    println!(
65        "After 100 off-top updates: {} UI renders (O(1) pruned)",
66        renders.get()
67    );
68
69    // New aggressive bid beats top-of-book -> triggers exactly 1 render
70    bids.upsert(Bid {
71        id: 99_999,
72        price_cents: 105_000,
73        qty: 12,
74        active: true,
75    });
76    assert_eq!(renders.get(), 1);
77
78    // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79    let available_usd = scope.atom(10_000i64);
80
81    let mut order_a = OptimisticLayer::new(&scope);
82    order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84    let mut order_b = OptimisticLayer::new(&scope);
85    order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86    println!(
87        "After 2 optimistic orders -> displayed balance: ${}",
88        scope.get(available_usd)
89    );
90
91    // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92    scope.set(available_usd, 15_000);
93    assert_eq!(scope.get(available_usd), 13_000);
94    println!(
95        "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96        scope.get(available_usd)
97    );
98
99    // Order A rejected by venue -> surgical revert preserves Order B
100    order_a.revert();
101    assert_eq!(scope.get(available_usd), 14_200);
102    println!(
103        "After Order A rejected -> balance: ${}",
104        scope.get(available_usd)
105    );
106
107    // Order B confirmed by venue -> atomic zero-flicker settle
108    order_b.settle_with(|| scope.set(available_usd, 14_200));
109    assert_eq!(scope.get(available_usd), 14_200);
110    println!(
111        "After Order B settled -> final committed balance: ${}",
112        scope.get(available_usd)
113    );
114}
Source

pub fn top_k_filtered_signal<P>( &self, k: usize, predicate: P, ) -> Computed<Vec<T>>
where P: Fn(&T) -> bool + 'static,

Creates a first-class reactive Computed<Vec<T>> with an additional per-view predicate and re-entrancy-safe O(1) Top-K boundary pruning.

Source

pub fn dispose_top_k_signal(&self, sig: Computed<Vec<T>>) -> bool

Unregisters a previously created Top-K signal view and disposes its reactive node in the owning Scope.

Source

pub fn upsert(&self, item: T)

Inserts or replaces an item and incrementally updates any affected watched Top-K signals.

Examples found in repository?
examples/orderbook_optimistic.rs (lines 70-75)
18fn main() {
19    let scope = Scope::new();
20
21    // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22    let bids = ReactiveCollection::with_filter(
23        &scope,
24        |b: &Bid| b.id,
25        |a, b| b.price_cents.cmp(&a.price_cents),
26        |b| b.active && b.qty > 0,
27    );
28
29    bids.upsert_many((1..=10_000u64).map(|id| Bid {
30        id,
31        price_cents: i64::try_from(id).unwrap() * 10,
32        qty: 5,
33        active: true,
34    }));
35
36    let top5 = bids.top_k_signal(5);
37    let best_bid =
38        scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40    let renders = Rc::new(Cell::new(0usize));
41    let renders_c = Rc::clone(&renders);
42    let _sub = scope.subscribe(best_bid, move |price| {
43        renders_c.set(renders_c.get() + 1);
44        println!(
45            "[book] best bid updated -> ${}.{:02}",
46            price / 100,
47            price.abs() % 100
48        );
49    });
50
51    let init_price = scope.get(best_bid);
52    println!(
53        "Initial best bid across {} active bids: ${}.{:02}",
54        bids.indexed_len(),
55        init_price / 100,
56        init_price.abs() % 100
57    );
58
59    // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60    for id in 1..=100u64 {
61        bids.mutate(&id, |b| b.price_cents += 1);
62    }
63    assert_eq!(renders.get(), 0);
64    println!(
65        "After 100 off-top updates: {} UI renders (O(1) pruned)",
66        renders.get()
67    );
68
69    // New aggressive bid beats top-of-book -> triggers exactly 1 render
70    bids.upsert(Bid {
71        id: 99_999,
72        price_cents: 105_000,
73        qty: 12,
74        active: true,
75    });
76    assert_eq!(renders.get(), 1);
77
78    // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79    let available_usd = scope.atom(10_000i64);
80
81    let mut order_a = OptimisticLayer::new(&scope);
82    order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84    let mut order_b = OptimisticLayer::new(&scope);
85    order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86    println!(
87        "After 2 optimistic orders -> displayed balance: ${}",
88        scope.get(available_usd)
89    );
90
91    // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92    scope.set(available_usd, 15_000);
93    assert_eq!(scope.get(available_usd), 13_000);
94    println!(
95        "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96        scope.get(available_usd)
97    );
98
99    // Order A rejected by venue -> surgical revert preserves Order B
100    order_a.revert();
101    assert_eq!(scope.get(available_usd), 14_200);
102    println!(
103        "After Order A rejected -> balance: ${}",
104        scope.get(available_usd)
105    );
106
107    // Order B confirmed by venue -> atomic zero-flicker settle
108    order_b.settle_with(|| scope.set(available_usd, 14_200));
109    assert_eq!(scope.get(available_usd), 14_200);
110    println!(
111        "After Order B settled -> final committed balance: ${}",
112        scope.get(available_usd)
113    );
114}
Source

pub fn try_upsert(&self, item: T) -> bool

Returns false if a reentrant policy callback changed the index during this edit.

Source

pub fn upsert_many<I>(&self, items: I)
where I: IntoIterator<Item = T>,

Inserts or replaces a batch of items.

Examples found in repository?
examples/orderbook_optimistic.rs (lines 29-34)
18fn main() {
19    let scope = Scope::new();
20
21    // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22    let bids = ReactiveCollection::with_filter(
23        &scope,
24        |b: &Bid| b.id,
25        |a, b| b.price_cents.cmp(&a.price_cents),
26        |b| b.active && b.qty > 0,
27    );
28
29    bids.upsert_many((1..=10_000u64).map(|id| Bid {
30        id,
31        price_cents: i64::try_from(id).unwrap() * 10,
32        qty: 5,
33        active: true,
34    }));
35
36    let top5 = bids.top_k_signal(5);
37    let best_bid =
38        scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40    let renders = Rc::new(Cell::new(0usize));
41    let renders_c = Rc::clone(&renders);
42    let _sub = scope.subscribe(best_bid, move |price| {
43        renders_c.set(renders_c.get() + 1);
44        println!(
45            "[book] best bid updated -> ${}.{:02}",
46            price / 100,
47            price.abs() % 100
48        );
49    });
50
51    let init_price = scope.get(best_bid);
52    println!(
53        "Initial best bid across {} active bids: ${}.{:02}",
54        bids.indexed_len(),
55        init_price / 100,
56        init_price.abs() % 100
57    );
58
59    // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60    for id in 1..=100u64 {
61        bids.mutate(&id, |b| b.price_cents += 1);
62    }
63    assert_eq!(renders.get(), 0);
64    println!(
65        "After 100 off-top updates: {} UI renders (O(1) pruned)",
66        renders.get()
67    );
68
69    // New aggressive bid beats top-of-book -> triggers exactly 1 render
70    bids.upsert(Bid {
71        id: 99_999,
72        price_cents: 105_000,
73        qty: 12,
74        active: true,
75    });
76    assert_eq!(renders.get(), 1);
77
78    // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79    let available_usd = scope.atom(10_000i64);
80
81    let mut order_a = OptimisticLayer::new(&scope);
82    order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84    let mut order_b = OptimisticLayer::new(&scope);
85    order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86    println!(
87        "After 2 optimistic orders -> displayed balance: ${}",
88        scope.get(available_usd)
89    );
90
91    // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92    scope.set(available_usd, 15_000);
93    assert_eq!(scope.get(available_usd), 13_000);
94    println!(
95        "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96        scope.get(available_usd)
97    );
98
99    // Order A rejected by venue -> surgical revert preserves Order B
100    order_a.revert();
101    assert_eq!(scope.get(available_usd), 14_200);
102    println!(
103        "After Order A rejected -> balance: ${}",
104        scope.get(available_usd)
105    );
106
107    // Order B confirmed by venue -> atomic zero-flicker settle
108    order_b.settle_with(|| scope.set(available_usd, 14_200));
109    assert_eq!(scope.get(available_usd), 14_200);
110    println!(
111        "After Order B settled -> final committed balance: ${}",
112        scope.get(available_usd)
113    );
114}
Source

pub fn try_upsert_many(&self, items: impl IntoIterator<Item = T>) -> bool

Atomically applies all rows. A competing reentrant edit supersedes this batch.

Source

pub fn update(&self, id: &K, updater: impl FnOnce(&T) -> T) -> bool

Updates a snapshot of the item. Returns false if the key was absent or the callback replaced or deleted it before the edit could be applied.

Source

pub fn mutate(&self, id: &K, mutator: impl FnOnce(&mut T)) -> bool

Mutates a detached copy with the same conflict semantics as Self::update.

Examples found in repository?
examples/orderbook_optimistic.rs (line 61)
18fn main() {
19    let scope = Scope::new();
20
21    // 1. Live 10,000-row orderbook with exact reactive Top-5 highest bids
22    let bids = ReactiveCollection::with_filter(
23        &scope,
24        |b: &Bid| b.id,
25        |a, b| b.price_cents.cmp(&a.price_cents),
26        |b| b.active && b.qty > 0,
27    );
28
29    bids.upsert_many((1..=10_000u64).map(|id| Bid {
30        id,
31        price_cents: i64::try_from(id).unwrap() * 10,
32        qty: 5,
33        active: true,
34    }));
35
36    let top5 = bids.top_k_signal(5);
37    let best_bid =
38        scope.computed(move |cx| cx.with(top5, |rows| rows.first().map_or(0, |b| b.price_cents)));
39
40    let renders = Rc::new(Cell::new(0usize));
41    let renders_c = Rc::clone(&renders);
42    let _sub = scope.subscribe(best_bid, move |price| {
43        renders_c.set(renders_c.get() + 1);
44        println!(
45            "[book] best bid updated -> ${}.{:02}",
46            price / 100,
47            price.abs() % 100
48        );
49    });
50
51    let init_price = scope.get(best_bid);
52    println!(
53        "Initial best bid across {} active bids: ${}.{:02}",
54        bids.indexed_len(),
55        init_price / 100,
56        init_price.abs() % 100
57    );
58
59    // 100 updates strictly outside Top-5 -> O(1) boundary pruned, 0 UI renders!
60    for id in 1..=100u64 {
61        bids.mutate(&id, |b| b.price_cents += 1);
62    }
63    assert_eq!(renders.get(), 0);
64    println!(
65        "After 100 off-top updates: {} UI renders (O(1) pruned)",
66        renders.get()
67    );
68
69    // New aggressive bid beats top-of-book -> triggers exactly 1 render
70    bids.upsert(Bid {
71        id: 99_999,
72        price_cents: 105_000,
73        qty: 12,
74        active: true,
75    });
76    assert_eq!(renders.get(), 1);
77
78    // 2. Overlapping optimistic user orders auto-rebasing over concurrent exchange fills
79    let available_usd = scope.atom(10_000i64);
80
81    let mut order_a = OptimisticLayer::new(&scope);
82    order_a.update(available_usd, |bal| bal - 1_200); // UI: $8,800
83
84    let mut order_b = OptimisticLayer::new(&scope);
85    order_b.update(available_usd, |bal| bal - 800); // UI: $8,000
86    println!(
87        "After 2 optimistic orders -> displayed balance: ${}",
88        scope.get(available_usd)
89    );
90
91    // Concurrent exchange deposit push arrives (+$5,000) -> both active intents auto-rebase!
92    scope.set(available_usd, 15_000);
93    assert_eq!(scope.get(available_usd), 13_000);
94    println!(
95        "After concurrent +$5,000 deposit -> auto-rebased balance: ${}",
96        scope.get(available_usd)
97    );
98
99    // Order A rejected by venue -> surgical revert preserves Order B
100    order_a.revert();
101    assert_eq!(scope.get(available_usd), 14_200);
102    println!(
103        "After Order A rejected -> balance: ${}",
104        scope.get(available_usd)
105    );
106
107    // Order B confirmed by venue -> atomic zero-flicker settle
108    order_b.settle_with(|| scope.set(available_usd, 14_200));
109    assert_eq!(scope.get(available_usd), 14_200);
110    println!(
111        "After Order B settled -> final committed balance: ${}",
112        scope.get(available_usd)
113    );
114}
Source

pub fn delete(&self, id: &K) -> Option<T>

Deletes an item by key.

Source

pub fn clear(&self)

Removes all items from the collection.

Trait Implementations§

Source§

impl<T, K, IdFn, Cmp, Flt> Debug for ReactiveCollection<T, K, IdFn, Cmp, Flt>
where T: Clone + PartialEq + 'static, K: Ord + Clone + 'static, IdFn: Fn(&T) -> K + 'static, Cmp: Fn(&T, &T) -> Ordering + 'static, Flt: ItemFilter<T> + 'static,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, K, IdFn, Cmp, Flt> Drop for ReactiveCollection<T, K, IdFn, Cmp, Flt>
where T: Clone + PartialEq + 'static, K: Ord + Clone + 'static, IdFn: Fn(&T) -> K + 'static, Cmp: Fn(&T, &T) -> Ordering + 'static, Flt: ItemFilter<T> + 'static,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<T, K, IdFn, Cmp, Flt = AcceptAll> !Freeze for ReactiveCollection<T, K, IdFn, Cmp, Flt>

§

impl<T, K, IdFn, Cmp, Flt = AcceptAll> !RefUnwindSafe for ReactiveCollection<T, K, IdFn, Cmp, Flt>

§

impl<T, K, IdFn, Cmp, Flt = AcceptAll> !Send for ReactiveCollection<T, K, IdFn, Cmp, Flt>

§

impl<T, K, IdFn, Cmp, Flt = AcceptAll> !Sync for ReactiveCollection<T, K, IdFn, Cmp, Flt>

§

impl<T, K, IdFn, Cmp, Flt = AcceptAll> !UnwindSafe for ReactiveCollection<T, K, IdFn, Cmp, Flt>

§

impl<T, K, IdFn, Cmp, Flt> Forget for ReactiveCollection<T, K, IdFn, Cmp, Flt>

§

impl<T, K, IdFn, Cmp, Flt> Move for ReactiveCollection<T, K, IdFn, Cmp, Flt>

§

impl<T, K, IdFn, Cmp, Flt> Unpin for ReactiveCollection<T, K, IdFn, Cmp, Flt>
where Flt: Unpin, Cmp: Unpin, IdFn: Unpin, Rc<RefCell<ScopeInner>>: Unpin, Rc<Index<T, K>>: Unpin, Rc<RefCell<Vec<WatchedTopK>>>: Unpin, Rc<RefCell<Vec<WatchedFilteredTopK<T, K>>>>: Unpin, *const LeafNode<usize, Computed<Vec<T>>> is !null: Unpin, Box<(usize, Computed<Vec<T>>)>: Unpin, *const LeafNode<K, Atom<u64>> is !null: Unpin, Box<(K, Atom<u64>)>: Unpin, Rc<Cell<bool>>: Unpin,

§

impl<T, K, IdFn, Cmp, Flt> UnsafeUnpin for ReactiveCollection<T, K, IdFn, Cmp, Flt>
where *const RcInner<RefCell<ScopeInner>> is !null: UnsafeUnpin, PhantomData<RcInner<RefCell<ScopeInner>>>: UnsafeUnpin, *const RcInner<Index<T, K>> is !null: UnsafeUnpin, PhantomData<RcInner<Index<T, K>>>: UnsafeUnpin, PhantomData<(IdFn, Cmp, Flt)>: UnsafeUnpin, *const RcInner<RefCell<Vec<WatchedTopK>>> is !null: UnsafeUnpin, PhantomData<RcInner<RefCell<Vec<WatchedTopK>>>>: UnsafeUnpin, *const RcInner<RefCell<Vec<WatchedFilteredTopK<T, K>>>> is !null: UnsafeUnpin, PhantomData<RcInner<RefCell<Vec<WatchedFilteredTopK<T, K>>>>>: UnsafeUnpin, *const LeafNode<usize, Computed<Vec<T>>> is !null: UnsafeUnpin, PhantomData<(Owned, LeafOrInternal)>: UnsafeUnpin, PhantomData<Box<(usize, Computed<Vec<T>>)>>: UnsafeUnpin, PhantomData<fn() -> u64>: UnsafeUnpin, *const LeafNode<K, Atom<u64>> is !null: UnsafeUnpin, PhantomData<Box<(K, Atom<u64>)>>: UnsafeUnpin, *const RcInner<Cell<bool>> is !null: UnsafeUnpin, PhantomData<RcInner<Cell<bool>>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.