ferroui_system/collections/specialized/
notify_collection_changed_event_args.rs1use std::rc::Rc;
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
8pub enum NotifyCollectionChangedAction {
9 Add,
10 Remove,
11 Replace,
12 Move,
13 Reset,
14}
15
16pub struct NotifyCollectionChangedEventArgs<'a, T> {
21 pub action: NotifyCollectionChangedAction,
22 pub new_items: &'a [T],
23 pub old_items: &'a [T],
24 pub new_starting_index: i32,
25 pub old_starting_index: i32,
26}
27
28impl<T: Clone> NotifyCollectionChangedEventArgs<'_, T> {
29 pub fn share(&self) -> SharedNotifyCollectionChangedEventArgs<T> {
33 SharedNotifyCollectionChangedEventArgs(Rc::new(SharedNotifyCollectionChangedData {
34 action: self.action,
35 new_items: self.new_items.to_vec(),
36 old_items: self.old_items.to_vec(),
37 new_starting_index: self.new_starting_index,
38 old_starting_index: self.old_starting_index,
39 }))
40 }
41}
42
43struct SharedNotifyCollectionChangedData<T> {
44 action: NotifyCollectionChangedAction,
45 new_items: Vec<T>,
46 old_items: Vec<T>,
47 new_starting_index: i32,
48 old_starting_index: i32,
49}
50
51pub struct SharedNotifyCollectionChangedEventArgs<T>(Rc<SharedNotifyCollectionChangedData<T>>);
55
56impl<T> Clone for SharedNotifyCollectionChangedEventArgs<T> {
57 fn clone(&self) -> Self {
58 Self(self.0.clone())
59 }
60}
61
62impl<T> PartialEq for SharedNotifyCollectionChangedEventArgs<T> {
63 fn eq(&self, other: &Self) -> bool {
64 Rc::ptr_eq(&self.0, &other.0)
65 }
66}
67
68impl<T> SharedNotifyCollectionChangedEventArgs<T> {
69 pub fn action(&self) -> NotifyCollectionChangedAction {
71 self.0.action
72 }
73
74 pub fn new_items(&self) -> &[T] {
76 &self.0.new_items
77 }
78
79 pub fn old_items(&self) -> &[T] {
81 &self.0.old_items
82 }
83
84 pub fn new_starting_index(&self) -> i32 {
86 self.0.new_starting_index
87 }
88
89 pub fn old_starting_index(&self) -> i32 {
92 self.0.old_starting_index
93 }
94
95 pub fn as_args(&self) -> NotifyCollectionChangedEventArgs<'_, T> {
97 NotifyCollectionChangedEventArgs {
98 action: self.0.action,
99 new_items: &self.0.new_items,
100 old_items: &self.0.old_items,
101 new_starting_index: self.0.new_starting_index,
102 old_starting_index: self.0.old_starting_index,
103 }
104 }
105}