Skip to main content

ferroui_dynamic_data/list/
observable_list.rs

1//! `AnonymousObservableList<T>` (`List/Internal/AnonymousObservableList.cs`): a list that can be
2//! observed and not edited, made of a sequence of change sets.
3
4use super::{ChangeSet, IChangeSet, IObservableList, SourceList};
5use ferroui_system::{IDisposable, IObservable};
6use std::rc::Rc;
7
8/// A read-only list of the items of a sequence of change sets (`AnonymousObservableList<T>`, the
9/// result of `AsObservableList`).
10pub struct ObservableList<T> {
11    source_list: SourceList<T>,
12}
13
14impl<T> Clone for ObservableList<T> {
15    fn clone(&self) -> Self {
16        Self { source_list: self.source_list.clone() }
17    }
18}
19
20impl<T: Clone + PartialEq + 'static> ObservableList<T> {
21    /// The list of the items of `source` (`AnonymousObservableList(IObservable<IChangeSet<T>>)`).
22    pub fn from_source<C: IChangeSet<T>>(source: Rc<dyn IObservable<C>>) -> Self {
23        Self { source_list: SourceList::from_source(source) }
24    }
25
26    /// A read-only view of `list` (`AnonymousObservableList(ISourceList<T>)`).
27    pub fn from_list(list: &SourceList<T>) -> Self {
28        Self { source_list: list.clone() }
29    }
30}
31
32impl<T: Clone + PartialEq + 'static> IObservableList<T> for ObservableList<T> {
33    fn count(&self) -> usize {
34        self.source_list.count()
35    }
36
37    fn count_changed(&self) -> Rc<dyn IObservable<usize>> {
38        self.source_list.count_changed()
39    }
40
41    fn items(&self) -> Vec<T> {
42        self.source_list.items()
43    }
44
45    fn connect_with(&self, predicate: Option<Rc<dyn Fn(&T) -> bool>>) -> Rc<dyn IObservable<ChangeSet<T>>> {
46        self.source_list.connect_with(predicate)
47    }
48
49    fn preview_with(&self, predicate: Option<Rc<dyn Fn(&T) -> bool>>) -> Rc<dyn IObservable<ChangeSet<T>>> {
50        self.source_list.preview_with(predicate)
51    }
52}
53
54impl<T: Clone + PartialEq + 'static> IDisposable for ObservableList<T> {
55    fn dispose(&self) {
56        self.source_list.dispose();
57    }
58}