Skip to main content

ferroui_dynamic_data/binding/
sort_and_bind_options.rs

1//! `SortAndBindOptions` (`Binding/SortAndBindOptions.cs`).
2
3use super::BindingOptions;
4use ferroui_reactive::concurrency::IScheduler;
5use std::fmt;
6use std::rc::Rc;
7
8/// The options of `SortAndBind` (`SortAndBindOptions`).
9#[derive(Clone)]
10pub struct SortAndBindOptions {
11    /// The number of changes above which the target is loaded again; 0 never (`ResetThreshold`).
12    pub reset_threshold: usize,
13    /// Whether an update that keeps its position replaces the item in place
14    /// (`UseReplaceForUpdates`).
15    pub use_replace_for_updates: bool,
16    /// Whether the positions are found by a binary search, which needs a comparer that tells every
17    /// item apart (`UseBinarySearch`).
18    pub use_binary_search: bool,
19    /// The initial capacity of the collection that `SortAndBind` creates; `None` for the default
20    /// (`InitialCapacity`, `-1`).
21    pub initial_capacity: Option<usize>,
22    /// Whether the first change set loads the target with a reset (`ResetOnFirstTimeLoad`).
23    pub reset_on_first_time_load: bool,
24    /// The scheduler on which the changes are observed, if any (`Scheduler`).
25    pub scheduler: Option<Rc<dyn IScheduler>>,
26}
27
28impl Default for SortAndBindOptions {
29    /// `new SortAndBindOptions()`.
30    fn default() -> Self {
31        Self {
32            reset_threshold: BindingOptions::DEFAULT_RESET_THRESHOLD,
33            use_replace_for_updates: BindingOptions::DEFAULT_USE_REPLACE_FOR_UPDATES,
34            use_binary_search: false,
35            initial_capacity: None,
36            reset_on_first_time_load: false,
37            scheduler: None,
38        }
39    }
40}
41
42impl PartialEq for SortAndBindOptions {
43    /// The parts compare by value and the scheduler by identity (the record of upstream).
44    fn eq(&self, other: &Self) -> bool {
45        self.reset_threshold == other.reset_threshold
46            && self.use_replace_for_updates == other.use_replace_for_updates
47            && self.use_binary_search == other.use_binary_search
48            && self.initial_capacity == other.initial_capacity
49            && self.reset_on_first_time_load == other.reset_on_first_time_load
50            && match (&self.scheduler, &other.scheduler) {
51                (None, None) => true,
52                (Some(a), Some(b)) => std::ptr::addr_eq(Rc::as_ptr(a), Rc::as_ptr(b)),
53                _ => false,
54            }
55    }
56}
57
58impl fmt::Debug for SortAndBindOptions {
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        f.debug_struct("SortAndBindOptions")
61            .field("reset_threshold", &self.reset_threshold)
62            .field("use_replace_for_updates", &self.use_replace_for_updates)
63            .field("use_binary_search", &self.use_binary_search)
64            .field("initial_capacity", &self.initial_capacity)
65            .field("reset_on_first_time_load", &self.reset_on_first_time_load)
66            .field("scheduler", &self.scheduler.is_some())
67            .finish()
68    }
69}