Skip to main content

ferroui_base/collections/
i_ferro_read_only_list.rs

1use super::{CollectionChangedHandler, FerroList};
2use crate::data::model::{INotifyCollectionChanged, INotifyPropertyChanged};
3use std::rc::Rc;
4
5/// A read-only notifying list.
6///
7/// The members of the read-only list contract it extends (count, indexer,
8/// enumeration) are part of the trait, together with the typed form of the
9/// collection changed event, which carries the items;
10/// [`INotifyCollectionChanged`] is its untyped form.
11pub trait IFerroReadOnlyList<T>: INotifyCollectionChanged + INotifyPropertyChanged {
12    /// The number of items in the list.
13    fn count(&self) -> usize;
14
15    /// The item at `index`. Panics if out of range.
16    fn get(&self, index: usize) -> T;
17
18    /// The items, as a snapshot that stays unchanged if the list is
19    /// modified (enumeration).
20    fn snapshot(&self) -> Rc<Vec<T>>;
21
22    /// Subscribes to changes. Returns a token for
23    /// [`remove_collection_changed`](Self::remove_collection_changed).
24    fn add_collection_changed(&self, handler: Rc<CollectionChangedHandler<T>>) -> u64;
25
26    /// Unsubscribes a handler.
27    fn remove_collection_changed(&self, token: u64) -> bool;
28}
29
30impl<T: Clone> IFerroReadOnlyList<T> for FerroList<T> {
31    fn count(&self) -> usize {
32        FerroList::count(self)
33    }
34
35    fn get(&self, index: usize) -> T {
36        FerroList::get(self, index)
37    }
38
39    fn snapshot(&self) -> Rc<Vec<T>> {
40        FerroList::snapshot(self)
41    }
42
43    fn add_collection_changed(&self, handler: Rc<CollectionChangedHandler<T>>) -> u64 {
44        FerroList::add_collection_changed(self, handler)
45    }
46
47    fn remove_collection_changed(&self, token: u64) -> bool {
48        FerroList::remove_collection_changed(self, token)
49    }
50}