Skip to content

Commit d7dff5b

Browse files
committed
refactor(ui): Add AllRemoteEvents::get_by_event_id_mut.
Having a mutable iterator can be dangerous and is probably too generic regarding the safety we want to add around the `AllRemoteEvents` type. This patch removes `iter_mut` and replaces it by its only use case: `get_by_event_id_mut`.
1 parent cabde8e commit d7dff5b

File tree

1 file changed

+8
-15
lines changed
  • crates/matrix-sdk-ui/src/timeline/controller

1 file changed

+8
-15
lines changed

crates/matrix-sdk-ui/src/timeline/controller/state.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
// limitations under the License.
1414

1515
use std::{
16-
collections::{
17-
vec_deque::{Iter, IterMut},
18-
HashMap, VecDeque,
19-
},
16+
collections::{vec_deque::Iter, HashMap, VecDeque},
2017
future::Future,
2118
num::NonZeroUsize,
2219
sync::{Arc, RwLock},
@@ -745,11 +742,8 @@ impl TimelineStateTransaction<'_> {
745742
}
746743

747744
TimelineItemPosition::UpdateDecrypted { .. } => {
748-
if let Some(event) = self
749-
.meta
750-
.all_remote_events
751-
.iter_mut()
752-
.find(|e| e.event_id == event_meta.event_id)
745+
if let Some(event) =
746+
self.meta.all_remote_events.get_by_event_id_mut(event_meta.event_id)
753747
{
754748
if event.visible != event_meta.visible {
755749
event.visible = event_meta.visible;
@@ -1156,12 +1150,6 @@ impl AllRemoteEvents {
11561150
self.0.iter()
11571151
}
11581152

1159-
/// Return a front-to-back iterator over all remote events as mutable
1160-
/// references.
1161-
pub fn iter_mut(&mut self) -> IterMut<'_, EventMeta> {
1162-
self.0.iter_mut()
1163-
}
1164-
11651153
/// Remove all remote events.
11661154
pub fn clear(&mut self) {
11671155
self.0.clear();
@@ -1186,6 +1174,11 @@ impl AllRemoteEvents {
11861174
pub fn last(&self) -> Option<&EventMeta> {
11871175
self.0.back()
11881176
}
1177+
1178+
/// Get a mutable reference to a specific remote event by its ID.
1179+
pub fn get_by_event_id_mut(&mut self, event_id: &EventId) -> Option<&mut EventMeta> {
1180+
self.0.iter_mut().rev().find(|event_meta| event_meta.event_id == event_id)
1181+
}
11891182
}
11901183

11911184
/// Full metadata about an event.

0 commit comments

Comments
 (0)