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
//! `Combine` (`Cache/ObservableCacheEx.Combine.cs`): the private overloads behind `And`, `Or`,
//! `Xor` and `Except`, and the kinds of sources they take.
//!
//! Deviation (DEVIATIONS.md, DynamicData): upstream's static overloads over a collection of
//! sources are methods of extension traits on those collections, as for `MergeChangeSets`:
//! - a sequence with others: provided methods of [`ObservableCacheEx`](crate::cache::ObservableCacheEx)
//! (`and_cache(others)`, `or(others)`, `xor(others)`, `except(others)`);
//! - a collection of sources (`ICollection<IObservable<IChangeSet>>`): [`CacheSourcesCombineEx`],
//! for a slice or a `Vec`;
//! - an observable list of sources (`IObservableList<IObservable<IChangeSet>>`,
//! `IObservableList<IObservableCache>`, `IObservableList<ISourceCache>`):
//! [`CacheSourceListCombineEx`], for a `SourceList` or an `ObservableList`.
//!
//! A source is any [`CacheChangeSetSource`]: a sequence of change sets, or a cache, whose
//! `Connect()` is its sequence.
//!
//! Deviation (DEVIATIONS.md, DynamicData): the combiners keep the item they hold when a source
//! gives the same object again (`ReferenceEquals`). The reference of an item is the object its
//! pointer refers to (`T: Deref`, an `Rc<..>` for the classes of upstream), as
//! `ignore_same_reference_update` does; the combiners of caches require it.
//!
//! Naming (DEVIATIONS.md, DynamicData): `And` is `and_cache` everywhere, as the method of
//! `ObservableCacheEx` would hide the join pattern `ObservableExt::and`.
use crate::cache::internal::{combiner, dynamic_combiner, CombineOperator, SameReference};
use crate::cache::{ChangeSet, IConnectableCache, IObservableCache, ISourceCache, ObservableCache, SourceCache};
use crate::list::{IObservableList, ObservableList, SourceList};
use ferroui_system::IObservable;
use std::hash::Hash;
use std::ops::Deref;
use std::rc::Rc;
/// What `ReferenceEquals` is for an item: the same object behind the pointer.
pub(crate) fn same_reference<T: Deref + 'static>() -> SameReference<T> {
Rc::new(|a: &T, b: &T| std::ptr::addr_eq(Deref::deref(a) as *const T::Target, Deref::deref(b) as *const T::Target))
}
/// `Combine(IObservable<IChangeSet>, CombineOperator, params IObservable<IChangeSet>[])`: the
/// sequence first, then the others.
pub(crate) fn combine_with_others<T: Clone + Deref + 'static, K: Eq + Hash + Clone + 'static>(
source: Rc<dyn IObservable<ChangeSet<T, K>>>,
kind: CombineOperator,
others: Vec<Rc<dyn IObservable<ChangeSet<T, K>>>>,
) -> Rc<dyn IObservable<ChangeSet<T, K>>> {
let mut sources = Vec::with_capacity(others.len() + 1);
sources.push(source);
sources.extend(others);
combiner(sources, kind, same_reference())
}
/// A source of the change sets of a cache: a sequence of change sets, or a cache whose `Connect()`
/// is its sequence (the items of upstream's collections of sources).
pub trait CacheChangeSetSource: 'static {
/// The type of the items.
type Item: Clone + 'static;
/// The type of the keys.
type Key: Eq + Hash + Clone + 'static;
/// The change sets of the source: the sequence itself, or `Connect()` of a cache.
fn source_change_sets(&self) -> Rc<dyn IObservable<ChangeSet<Self::Item, Self::Key>>>;
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static> CacheChangeSetSource for Rc<dyn IObservable<ChangeSet<T, K>>> {
type Item = T;
type Key = K;
fn source_change_sets(&self) -> Rc<dyn IObservable<ChangeSet<T, K>>> {
self.clone()
}
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static> CacheChangeSetSource for SourceCache<T, K> {
type Item = T;
type Key = K;
fn source_change_sets(&self) -> Rc<dyn IObservable<ChangeSet<T, K>>> {
self.connect()
}
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static> CacheChangeSetSource for ObservableCache<T, K> {
type Item = T;
type Key = K;
fn source_change_sets(&self) -> Rc<dyn IObservable<ChangeSet<T, K>>> {
self.connect()
}
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static> CacheChangeSetSource for Rc<dyn IObservableCache<T, K>> {
type Item = T;
type Key = K;
fn source_change_sets(&self) -> Rc<dyn IObservable<ChangeSet<T, K>>> {
self.connect()
}
}
impl<T: Clone + 'static, K: Eq + Hash + Clone + 'static> CacheChangeSetSource for Rc<dyn ISourceCache<T, K>> {
type Item = T;
type Key = K;
fn source_change_sets(&self) -> Rc<dyn IObservable<ChangeSet<T, K>>> {
self.connect()
}
}
/// Caches compare by identity (reference equality), so that they can be the items of a list.
impl<T, K> PartialEq for dyn IObservableCache<T, K> {
fn eq(&self, other: &Self) -> bool {
std::ptr::addr_eq(self as *const Self, other as *const Self)
}
}
/// Caches compare by identity (reference equality), so that they can be the items of a list.
impl<T, K> PartialEq for dyn ISourceCache<T, K> {
fn eq(&self, other: &Self) -> bool {
std::ptr::addr_eq(self as *const Self, other as *const Self)
}
}
/// `And`, `Or`, `Xor` and `Except` of a collection of sources (`And(this
/// ICollection<IObservable<IChangeSet<TObject, TKey>>>)`, ..): the sources are subscribed in
/// order when the result is subscribed.
pub trait CacheSourcesCombineEx {
/// The type of the items.
type Item: Clone + 'static;
/// The type of the keys.
type Key: Eq + Hash + Clone + 'static;
/// The items that are in every source (`And(ICollection<..>)`).
fn and_cache(&self) -> Rc<dyn IObservable<ChangeSet<Self::Item, Self::Key>>>
where
Self::Item: Deref;
/// The items that are in any source (`Or(ICollection<..>)`).
fn or(&self) -> Rc<dyn IObservable<ChangeSet<Self::Item, Self::Key>>>
where
Self::Item: Deref;
/// The items that are in exactly one source (`Xor(ICollection<..>)`).
fn xor(&self) -> Rc<dyn IObservable<ChangeSet<Self::Item, Self::Key>>>
where
Self::Item: Deref;
/// The items of the first source that are in none of the others (`Except(ICollection<..>)`).
fn except(&self) -> Rc<dyn IObservable<ChangeSet<Self::Item, Self::Key>>>
where
Self::Item: Deref;
}
/// `Combine(ICollection<IObservable<IChangeSet>>, CombineOperator)`.
fn combine_collection<S: CacheChangeSetSource>(sources: &[S], kind: CombineOperator) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>>
where
S::Item: Deref,
{
combiner(sources.iter().map(CacheChangeSetSource::source_change_sets).collect(), kind, same_reference())
}
impl<S: CacheChangeSetSource> CacheSourcesCombineEx for [S] {
type Item = S::Item;
type Key = S::Key;
fn and_cache(&self) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>>
where
Self::Item: Deref,
{
combine_collection(self, CombineOperator::And)
}
fn or(&self) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>>
where
Self::Item: Deref,
{
combine_collection(self, CombineOperator::Or)
}
fn xor(&self) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>>
where
Self::Item: Deref,
{
combine_collection(self, CombineOperator::Xor)
}
fn except(&self) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>>
where
Self::Item: Deref,
{
combine_collection(self, CombineOperator::Except)
}
}
/// `And`, `Or`, `Xor` and `Except` of an observable list of sources (`And(this
/// IObservableList<IObservable<IChangeSet<TObject, TKey>>>)`, `And(this
/// IObservableList<IObservableCache<TObject, TKey>>)`, `And(this
/// IObservableList<ISourceCache<TObject, TKey>>)`, ..): the result follows the sources as they are
/// added to the list and removed from it.
pub trait CacheSourceListCombineEx {
/// The type of the items.
type Item: Clone + 'static;
/// The type of the keys.
type Key: Eq + Hash + Clone + 'static;
/// The items that are in every source of the list; none while the list is empty
/// (`And(IObservableList<..>)`).
fn and_cache(&self) -> Rc<dyn IObservable<ChangeSet<Self::Item, Self::Key>>>
where
Self::Item: Deref;
/// The items that are in any source of the list (`Or(IObservableList<..>)`).
fn or(&self) -> Rc<dyn IObservable<ChangeSet<Self::Item, Self::Key>>>
where
Self::Item: Deref;
/// The items that are in exactly one source of the list (`Xor(IObservableList<..>)`).
fn xor(&self) -> Rc<dyn IObservable<ChangeSet<Self::Item, Self::Key>>>
where
Self::Item: Deref;
/// The items of the first source of the list that are in none of the others
/// (`Except(IObservableList<..>)`).
fn except(&self) -> Rc<dyn IObservable<ChangeSet<Self::Item, Self::Key>>>
where
Self::Item: Deref;
}
/// `Combine(IObservableList<..>, CombineOperator)`, with `new DynamicCombiner(..).Run()`.
fn combine_list<S: CacheChangeSetSource + Clone + PartialEq>(list: &dyn IObservableList<S>, kind: CombineOperator) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>>
where
S::Item: Deref,
{
dynamic_combiner(list.connect(), Rc::new(|source: &S| source.source_change_sets()), kind, same_reference())
}
/// The forms of a list of sources, for one type of list.
macro_rules! cache_source_list_combine_impl {
($list:ident) => {
impl<S: CacheChangeSetSource + Clone + PartialEq> CacheSourceListCombineEx for $list<S> {
type Item = S::Item;
type Key = S::Key;
fn and_cache(&self) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>>
where
Self::Item: Deref,
{
combine_list(self, CombineOperator::And)
}
fn or(&self) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>>
where
Self::Item: Deref,
{
combine_list(self, CombineOperator::Or)
}
fn xor(&self) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>>
where
Self::Item: Deref,
{
combine_list(self, CombineOperator::Xor)
}
fn except(&self) -> Rc<dyn IObservable<ChangeSet<S::Item, S::Key>>>
where
Self::Item: Deref,
{
combine_list(self, CombineOperator::Except)
}
}
};
}
cache_source_list_combine_impl!(SourceList);
cache_source_list_combine_impl!(ObservableList);