Struct relm4::factory::collections::WeakDynamicIndex
source · [−]pub struct WeakDynamicIndex { /* private fields */ }
Expand description
A weak version of DynamicIndex
.
Use this to send messages to the update function and call upgrade
to receive the actual DynamicIndex
.
A weak index is preferred for sending in messages because messages can be stale by the time they are handled and the element already deleted. A weak reference doesn’t keep the index alive if the element was deleted which allows you to properly handle invalid indices.
Panics
Sending a WeakDynamicIndex
to a different thread and accessing it will panic.
Implementations
sourceimpl WeakDynamicIndex
impl WeakDynamicIndex
sourcepub fn upgrade(&self) -> Option<DynamicIndex>
pub fn upgrade(&self) -> Option<DynamicIndex>
Attempts to upgrade the WeakDynamicIndex
to a DynamicIndex
.
Returns None
if the index has since been dropped.
Examples found in repository
relm4-examples/examples/factory_advanced_manual.rs (line 43)
≺ ≻
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
relm4-examples/examples/factory_advanced.rs (line 43)
≺ ≻
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
}
Trait Implementations
sourceimpl Clone for WeakDynamicIndex
impl Clone for WeakDynamicIndex
Auto Trait Implementations
impl !RefUnwindSafe for WeakDynamicIndex
impl Send for WeakDynamicIndex
impl Sync for WeakDynamicIndex
impl Unpin for WeakDynamicIndex
impl !UnwindSafe for WeakDynamicIndex
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)
🔬 This is a nightly-only experimental API. (
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more