Struct relm4::factory::collections::DynamicIndex
source · [−]pub struct DynamicIndex { /* private fields */ }
Expand description
A dynamic index that updates automatically when items are shifted inside a Factory
.
For example a FactoryVecDeque
has an insert
method that allows users
to insert data at arbitrary positions.
If we insert at the front all following widgets will be moved by one which would
invalidate their indices.
To allow widgets in a Factory
to still send messages with valid indices
this type ensures that the indices is always up to date.
Never send an index as usize
but always as DynamicIndex
or even better as WeakDynamicIndex
to the update function because messages can be queued up and stale by the time they are handled.
DynamicIndex
is a smart pointer so cloning will work similar to Rc
and will create
a pointer to the same data.
In short: only call current_index
from the update function
where you actually need the index as usize
.
Panics
Sending a DynamicIndex
to a different thread and accessing it will panic.
Implementations
sourceimpl DynamicIndex
impl DynamicIndex
sourcepub fn current_index(&self) -> usize
pub fn current_index(&self) -> usize
Get the current index number.
This value is updated by the Factory
and might change after each update function.
Examples found in repository
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
fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
match msg {
AppMsg::AddFirst => {
self.counters.push_front(Counter {
value: self.received_messages,
});
}
AppMsg::RemoveLast => {
self.counters.pop_back();
}
AppMsg::CountAt(weak_index) => {
if let Some(index) = weak_index.upgrade() {
if let Some(counter) = self.counters.get_mut(index.current_index()) {
counter.value = counter.value.wrapping_sub(1);
}
}
}
AppMsg::RemoveAt(weak_index) => {
if let Some(index) = weak_index.upgrade() {
self.counters.remove(index.current_index());
}
}
AppMsg::InsertBefore(weak_index) => {
if let Some(index) = weak_index.upgrade() {
self.counters.insert(
index.current_index(),
Counter {
value: self.received_messages,
},
);
}
}
AppMsg::InsertAfter(weak_index) => {
if let Some(index) = weak_index.upgrade() {
self.counters.insert(
index.current_index() + 1,
Counter {
value: self.received_messages,
},
);
}
}
}
self.received_messages += 1;
true
}
More examples
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
fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
match msg {
AppMsg::AddFirst => {
self.counters.push_front(Counter {
value: self.received_messages,
});
}
AppMsg::RemoveLast => {
self.counters.pop_back();
}
AppMsg::CountAt(weak_index) => {
if let Some(index) = weak_index.upgrade() {
if let Some(counter) = self.counters.get_mut(index.current_index()) {
counter.value = counter.value.wrapping_sub(1);
}
}
}
AppMsg::RemoveAt(weak_index) => {
if let Some(index) = weak_index.upgrade() {
self.counters.remove(index.current_index());
}
}
AppMsg::InsertBefore(weak_index) => {
if let Some(index) = weak_index.upgrade() {
self.counters.insert(
index.current_index(),
Counter {
value: self.received_messages,
},
);
}
}
AppMsg::InsertAfter(weak_index) => {
if let Some(index) = weak_index.upgrade() {
self.counters.insert(
index.current_index() + 1,
Counter {
value: self.received_messages,
},
);
}
}
}
self.received_messages += 1;
true
}
sourcepub fn downgrade(&self) -> WeakDynamicIndex
pub fn downgrade(&self) -> WeakDynamicIndex
Creates a WeakDynamicIndex
for sending in messages.
Examples found in repository
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
fn init_view(&self, index: &DynamicIndex, sender: Sender<AppMsg>) -> FactoryWidgets {
let hbox = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
.spacing(5)
.build();
let counter_button = gtk::Button::with_label(&self.value.to_string());
let index: DynamicIndex = index.clone();
let remove_button = gtk::Button::with_label("Remove");
let ins_above_button = gtk::Button::with_label("Add above");
let ins_below_button = gtk::Button::with_label("Add below");
hbox.append(&counter_button);
hbox.append(&remove_button);
hbox.append(&ins_above_button);
hbox.append(&ins_below_button);
{
let sender = sender.clone();
let index = index.clone();
counter_button.connect_clicked(move |_| {
send!(sender, AppMsg::CountAt(index.downgrade()));
});
}
{
let sender = sender.clone();
let index = index.clone();
remove_button.connect_clicked(move |_| {
send!(sender, AppMsg::RemoveAt(index.downgrade()));
});
}
{
let sender = sender.clone();
let index = index.clone();
ins_above_button.connect_clicked(move |_| {
send!(sender, AppMsg::InsertBefore(index.downgrade()));
});
}
ins_below_button.connect_clicked(move |_| {
send!(sender, AppMsg::InsertAfter(index.downgrade()));
});
FactoryWidgets {
hbox,
counter_button,
}
}
Trait Implementations
sourceimpl Clone for DynamicIndex
impl Clone for DynamicIndex
sourceimpl Debug for DynamicIndex
impl Debug for DynamicIndex
sourceimpl PartialEq<DynamicIndex> for DynamicIndex
impl PartialEq<DynamicIndex> for DynamicIndex
sourcefn eq(&self, other: &DynamicIndex) -> bool
fn eq(&self, other: &DynamicIndex) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &DynamicIndex) -> bool
fn ne(&self, other: &DynamicIndex) -> bool
This method tests for !=
.
impl Eq for DynamicIndex
impl StructuralEq for DynamicIndex
impl StructuralPartialEq for DynamicIndex
Auto Trait Implementations
impl !RefUnwindSafe for DynamicIndex
impl Send for DynamicIndex
impl Sync for DynamicIndex
impl Unpin for DynamicIndex
impl !UnwindSafe for DynamicIndex
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more