Skip to content

Commit 35db26b

Browse files
committed
paginator: use a generic type instead of a boxed trait
Who has two thumbs and wants to make a trait not use async_trait, and thus get rid of trait object safety?
1 parent b95c189 commit 35db26b

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

crates/matrix-sdk-ui/src/timeline/inner/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ mod state;
8282

8383
/// Data associated to the current timeline focus.
8484
#[derive(Debug)]
85-
enum TimelineFocusData {
85+
enum TimelineFocusData<P: RoomDataProvider> {
8686
/// The timeline receives live events from the sync.
8787
Live,
8888

@@ -92,7 +92,7 @@ enum TimelineFocusData {
9292
/// The event id we've started to focus on.
9393
event_id: OwnedEventId,
9494
/// The paginator instance.
95-
paginator: Paginator,
95+
paginator: Paginator<P>,
9696
/// Number of context events to request for the first request.
9797
num_context_events: u16,
9898
},
@@ -108,7 +108,7 @@ pub(super) struct TimelineInner<P: RoomDataProvider = Room> {
108108
state: Arc<RwLock<TimelineInnerState>>,
109109

110110
/// Inner mutable focus state.
111-
focus: Arc<RwLock<TimelineFocusData>>,
111+
focus: Arc<RwLock<TimelineFocusData<P>>>,
112112

113113
/// A [`RoomDataProvider`] implementation, providing data.
114114
///
@@ -240,7 +240,7 @@ impl<P: RoomDataProvider> TimelineInner<P> {
240240
let (focus_data, is_live) = match focus {
241241
TimelineFocus::Live => (TimelineFocusData::Live, LiveTimelineUpdatesAllowed::All),
242242
TimelineFocus::Event { target, num_context_events } => {
243-
let paginator = Paginator::new(Box::new(room_data_provider.clone()));
243+
let paginator = Paginator::new(room_data_provider.clone());
244244
(
245245
TimelineFocusData::Event { paginator, event_id: target, num_context_events },
246246
LiveTimelineUpdatesAllowed::None,

crates/matrix-sdk/src/event_cache/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ struct RoomEventCacheInner {
543543
///
544544
/// It's protected behind a lock to avoid multiple accesses to the paginator
545545
/// at the same time.
546-
pagination: RoomPaginationData,
546+
pagination: RoomPaginationData<WeakRoom>,
547547
}
548548

549549
impl RoomEventCacheInner {
@@ -560,7 +560,7 @@ impl RoomEventCacheInner {
560560
all_events,
561561
sender,
562562
pagination: RoomPaginationData {
563-
paginator: Paginator::new(Box::new(weak_room)),
563+
paginator: Paginator::new(weak_room),
564564
waited_for_initial_prev_token: Mutex::new(false),
565565
token_notifier: Default::default(),
566566
},

crates/matrix-sdk/src/event_cache/pagination.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ use tokio::{
2525
use tracing::{debug, instrument, trace};
2626

2727
use super::{
28-
paginator::{PaginationResult, Paginator, PaginatorState},
28+
paginator::{PaginableRoom, PaginationResult, Paginator, PaginatorState},
2929
store::Gap,
3030
BackPaginationOutcome, Result, RoomEventCacheInner,
3131
};
3232
use crate::event_cache::{linked_chunk::ChunkContent, store::RoomEvents};
3333

3434
#[derive(Debug)]
35-
pub(super) struct RoomPaginationData {
35+
pub(super) struct RoomPaginationData<PR: PaginableRoom> {
3636
/// A notifier that we received a new pagination token.
3737
pub token_notifier: Notify,
3838

3939
/// The stateful paginator instance used for the integrated pagination.
40-
pub paginator: Paginator,
40+
pub paginator: Paginator<PR>,
4141

4242
/// Have we ever waited for a previous-batch-token to come from sync? We do
4343
/// this at most once per room, the first time we try to run backward

crates/matrix-sdk/src/event_cache/paginator.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ impl From<Option<String>> for PaginationToken {
9494
/// forward from it.
9595
///
9696
/// See also the module-level documentation.
97-
pub struct Paginator {
97+
pub struct Paginator<PR: PaginableRoom> {
9898
/// The room in which we're going to run the pagination.
99-
room: Box<dyn PaginableRoom>,
99+
room: PR,
100100

101101
/// Current state of the paginator.
102102
state: SharedObservable<PaginatorState>,
@@ -113,7 +113,7 @@ pub struct Paginator {
113113
}
114114

115115
#[cfg(not(tarpaulin_include))]
116-
impl std::fmt::Debug for Paginator {
116+
impl<PR: PaginableRoom> std::fmt::Debug for Paginator<PR> {
117117
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118118
// Don't include the room in the debug output.
119119
f.debug_struct("Paginator")
@@ -186,9 +186,9 @@ impl Drop for ResetStateGuard {
186186
}
187187
}
188188

189-
impl Paginator {
189+
impl<PR: PaginableRoom> Paginator<PR> {
190190
/// Create a new [`Paginator`], given a room implementation.
191-
pub fn new(room: Box<dyn PaginableRoom>) -> Self {
191+
pub fn new(room: PR) -> Self {
192192
Self {
193193
room,
194194
state: SharedObservable::new(PaginatorState::Initial),
@@ -701,7 +701,7 @@ mod tests {
701701
#[async_test]
702702
async fn test_start_from() {
703703
// Prepare test data.
704-
let room = Box::new(TestRoom::new(false, *ROOM_ID, *USER_ID));
704+
let room = TestRoom::new(false, *ROOM_ID, *USER_ID);
705705

706706
let event_id = event_id!("$yoyoyo");
707707
let event_factory = &room.event_factory;
@@ -749,7 +749,7 @@ mod tests {
749749
#[async_test]
750750
async fn test_start_from_with_num_events() {
751751
// Prepare test data.
752-
let room = Box::new(TestRoom::new(false, *ROOM_ID, *USER_ID));
752+
let room = TestRoom::new(false, *ROOM_ID, *USER_ID);
753753

754754
let event_id = event_id!("$yoyoyo");
755755
let event_factory = &room.event_factory;
@@ -780,7 +780,7 @@ mod tests {
780780
#[async_test]
781781
async fn test_paginate_backward() {
782782
// Prepare test data.
783-
let room = Box::new(TestRoom::new(false, *ROOM_ID, *USER_ID));
783+
let room = TestRoom::new(false, *ROOM_ID, *USER_ID);
784784

785785
let event_id = event_id!("$yoyoyo");
786786
let event_factory = &room.event_factory;
@@ -852,7 +852,7 @@ mod tests {
852852
#[async_test]
853853
async fn test_paginate_backward_with_limit() {
854854
// Prepare test data.
855-
let room = Box::new(TestRoom::new(false, *ROOM_ID, *USER_ID));
855+
let room = TestRoom::new(false, *ROOM_ID, *USER_ID);
856856

857857
let event_id = event_id!("$yoyoyo");
858858
let event_factory = &room.event_factory;
@@ -896,7 +896,7 @@ mod tests {
896896
#[async_test]
897897
async fn test_paginate_forward() {
898898
// Prepare test data.
899-
let room = Box::new(TestRoom::new(false, *ROOM_ID, *USER_ID));
899+
let room = TestRoom::new(false, *ROOM_ID, *USER_ID);
900900

901901
let event_id = event_id!("$yoyoyo");
902902
let event_factory = &room.event_factory;
@@ -967,7 +967,7 @@ mod tests {
967967

968968
#[async_test]
969969
async fn test_state() {
970-
let room = Box::new(TestRoom::new(true, *ROOM_ID, *USER_ID));
970+
let room = TestRoom::new(true, *ROOM_ID, *USER_ID);
971971

972972
*room.prev_batch_token.lock().await = Some("prev".to_owned());
973973
*room.next_batch_token.lock().await = Some("next".to_owned());
@@ -1107,7 +1107,7 @@ mod tests {
11071107

11081108
#[async_test]
11091109
async fn test_abort_while_starting_from() {
1110-
let room = Box::new(AbortingRoom::default());
1110+
let room = AbortingRoom::default();
11111111

11121112
let paginator = Arc::new(Paginator::new(room.clone()));
11131113

@@ -1140,7 +1140,7 @@ mod tests {
11401140

11411141
#[async_test]
11421142
async fn test_abort_while_paginating() {
1143-
let room = Box::new(AbortingRoom::default());
1143+
let room = AbortingRoom::default();
11441144

11451145
// Assuming a paginator ready to back- or forward- paginate,
11461146
let paginator = Paginator::new(room.clone());

0 commit comments

Comments
 (0)