|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | use std::{ |
16 | | - collections::{HashMap, VecDeque}, |
| 16 | + collections::{ |
| 17 | + vec_deque::{Iter, IterMut}, |
| 18 | + HashMap, VecDeque, |
| 19 | + }, |
17 | 20 | future::Future, |
18 | 21 | num::NonZeroUsize, |
19 | 22 | sync::{Arc, RwLock}, |
@@ -715,7 +718,7 @@ impl TimelineStateTransaction<'_> { |
715 | 718 | // Returns its position, in this case. |
716 | 719 | fn event_already_exists( |
717 | 720 | new_event_id: &EventId, |
718 | | - all_remote_events: &VecDeque<EventMeta>, |
| 721 | + all_remote_events: &AllRemoteEvents, |
719 | 722 | ) -> Option<usize> { |
720 | 723 | all_remote_events.iter().position(|EventMeta { event_id, .. }| event_id == new_event_id) |
721 | 724 | } |
@@ -924,7 +927,7 @@ pub(in crate::timeline) struct TimelineMetadata { |
924 | 927 | /// |
925 | 928 | /// This is useful to get this for the moment as it helps the `Timeline` to |
926 | 929 | /// compute read receipts and read markers. |
927 | | - pub all_remote_events: VecDeque<EventMeta>, |
| 930 | + pub all_remote_events: AllRemoteEvents, |
928 | 931 |
|
929 | 932 | /// State helping matching reactions to their associated events, and |
930 | 933 | /// stashing pending reactions. |
@@ -1139,6 +1142,52 @@ impl TimelineMetadata { |
1139 | 1142 | } |
1140 | 1143 | } |
1141 | 1144 |
|
| 1145 | +/// A type for all remote events. |
| 1146 | +/// |
| 1147 | +/// Having this type helps to know exactly which parts of the code and how they |
| 1148 | +/// use all remote events. It also helps to give a bit of semantics on top of |
| 1149 | +/// them. |
| 1150 | +#[derive(Clone, Debug, Default)] |
| 1151 | +pub(crate) struct AllRemoteEvents(VecDeque<EventMeta>); |
| 1152 | + |
| 1153 | +impl AllRemoteEvents { |
| 1154 | + /// Return a front-to-back iterator over all remote events. |
| 1155 | + pub fn iter(&self) -> Iter<'_, EventMeta> { |
| 1156 | + self.0.iter() |
| 1157 | + } |
| 1158 | + |
| 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 | + |
| 1165 | + /// Remove all remote events. |
| 1166 | + pub fn clear(&mut self) { |
| 1167 | + self.0.clear(); |
| 1168 | + } |
| 1169 | + |
| 1170 | + /// Insert a new remote event at the front of all the others. |
| 1171 | + pub fn push_front(&mut self, event_meta: EventMeta) { |
| 1172 | + self.0.push_front(event_meta) |
| 1173 | + } |
| 1174 | + |
| 1175 | + /// Insert a new remote event at the back of all the others. |
| 1176 | + pub fn push_back(&mut self, event_meta: EventMeta) { |
| 1177 | + self.0.push_back(event_meta) |
| 1178 | + } |
| 1179 | + |
| 1180 | + /// Remove one remote event at a specific index, and return it if it exists. |
| 1181 | + pub fn remove(&mut self, event_index: usize) -> Option<EventMeta> { |
| 1182 | + self.0.remove(event_index) |
| 1183 | + } |
| 1184 | + |
| 1185 | + /// Return a reference to the last remote event if it exists. |
| 1186 | + pub fn back(&self) -> Option<&EventMeta> { |
| 1187 | + self.0.back() |
| 1188 | + } |
| 1189 | +} |
| 1190 | + |
1142 | 1191 | /// Full metadata about an event. |
1143 | 1192 | /// |
1144 | 1193 | /// Only used to group function parameters. |
|
0 commit comments