Skip to content

Commit 149950c

Browse files
authored
sliding sync: Use RangeInclusive<u32> instead of raw start/end UInt values (#1877)
* chore: use RangeInclusive instead of raw start/end integers for ranges in sliding sync * chore: have timeline_limit use a u32 instead of a Ruma UInt * chore: Remove all the `Into<u32>` generics on timeline_limit and ranges APIs * chore: introduce a `Bound` type alias for u32 Signed-off-by: Benjamin Bouvier <public@benj.me>
1 parent a849607 commit 149950c

File tree

6 files changed

+134
-156
lines changed

6 files changed

+134
-156
lines changed

bindings/matrix-sdk-ffi/src/sliding_sync.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use matrix_sdk::ruma::{
88
v4::RoomSubscription as RumaRoomSubscription,
99
UnreadNotificationsCount as RumaUnreadNotificationsCount,
1010
},
11-
assign, IdParseError, OwnedRoomId, RoomId, UInt,
11+
assign, IdParseError, OwnedRoomId, RoomId,
1212
};
1313
pub use matrix_sdk::{
1414
room::timeline::Timeline, ruma::api::client::sync::sync_events::v4::SyncRequestListFilters,
@@ -533,9 +533,9 @@ impl SlidingSyncListBuilder {
533533
Arc::new(builder)
534534
}
535535

536-
pub fn add_range(self: Arc<Self>, from: u32, to: u32) -> Arc<Self> {
536+
pub fn add_range(self: Arc<Self>, from: u32, to_included: u32) -> Arc<Self> {
537537
let mut builder = unwrap_or_clone_arc(self);
538-
builder.inner = builder.inner.add_range(from, to);
538+
builder.inner = builder.inner.add_range(from..=to_included);
539539
Arc::new(builder)
540540
}
541541

@@ -631,15 +631,15 @@ impl SlidingSyncList {
631631
/// Remember to cancel the existing stream and fetch a new one as this will
632632
/// only be applied on the next request.
633633
pub fn set_range(&self, start: u32, end: u32) -> Result<(), SlidingSyncError> {
634-
self.inner.set_range(start, end).map_err(Into::into)
634+
self.inner.set_range(start..=end).map_err(Into::into)
635635
}
636636

637637
/// Set the ranges to fetch
638638
///
639639
/// Remember to cancel the existing stream and fetch a new one as this will
640640
/// only be applied on the next request.
641641
pub fn add_range(&self, start: u32, end: u32) -> Result<(), SlidingSyncError> {
642-
self.inner.add_range((start, end)).map_err(Into::into)
642+
self.inner.add_range(start..=end).map_err(Into::into)
643643
}
644644

645645
/// Reset the ranges
@@ -654,7 +654,7 @@ impl SlidingSyncList {
654654

655655
/// The current timeline limit
656656
pub fn get_timeline_limit(&self) -> Option<u32> {
657-
self.inner.timeline_limit().map(|limit| u32::try_from(limit).unwrap_or_default())
657+
self.inner.timeline_limit()
658658
}
659659

660660
/// The current timeline limit
@@ -664,7 +664,7 @@ impl SlidingSyncList {
664664

665665
/// Unset the current timeline limit
666666
pub fn unset_timeline_limit(&self) {
667-
self.inner.set_timeline_limit::<UInt>(None)
667+
self.inner.set_timeline_limit(None)
668668
}
669669
}
670670

crates/matrix-sdk/src/sliding_sync/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ let list_builder = SlidingSyncList::builder("main_list")
7979
v4::SyncRequestListFilters::default(), { is_dm: Some(true)}
8080
)))
8181
.sort(vec!["by_recency".to_owned()])
82-
.set_range(0u32, 9u32);
82+
.set_range(0u32..=9);
8383
```
8484

8585
Please refer to the [specification][MSC], the [Ruma types][ruma-types],
@@ -438,7 +438,7 @@ let full_sync_list = SlidingSyncList::builder(&full_sync_list_name)
438438
439439
let active_list = SlidingSyncList::builder(&active_list_name) // the active window
440440
.sync_mode(SlidingSyncMode::Selective) // sync up the specific range only
441-
.set_range(0u32, 9u32) // only the top 10 items
441+
.set_range(0u32..=9) // only the top 10 items
442442
.sort(vec!["by_recency".to_owned()]) // last active
443443
.timeline_limit(5u32) // add the last 5 timeline items for room preview and faster timeline loading
444444
.required_state(vec![ // we want to know immediately:

crates/matrix-sdk/src/sliding_sync/list/builder.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
use std::{
44
convert::identity,
55
fmt,
6+
ops::RangeInclusive,
67
sync::{Arc, RwLock as StdRwLock},
78
};
89

910
use eyeball::unique::Observable;
1011
use eyeball_im::ObservableVector;
11-
use ruma::{api::client::sync::sync_events::v4, events::StateEventType, UInt};
12+
use ruma::{api::client::sync::sync_events::v4, events::StateEventType};
1213
use tokio::sync::mpsc::Sender;
1314

1415
use super::{
15-
super::SlidingSyncInternalMessage, SlidingSyncList, SlidingSyncListInner,
16+
super::SlidingSyncInternalMessage, Bound, SlidingSyncList, SlidingSyncListInner,
1617
SlidingSyncListRequestGenerator, SlidingSyncMode, SlidingSyncState,
1718
};
1819

@@ -28,9 +29,9 @@ pub struct SlidingSyncListBuilder {
2829
full_sync_batch_size: u32,
2930
full_sync_maximum_number_of_rooms_to_fetch: Option<u32>,
3031
filters: Option<v4::SyncRequestListFilters>,
31-
timeline_limit: Option<UInt>,
32+
timeline_limit: Option<Bound>,
3233
name: String,
33-
ranges: Vec<(UInt, UInt)>,
34+
ranges: Vec<RangeInclusive<Bound>>,
3435
once_built: Arc<Box<dyn Fn(SlidingSyncList) -> SlidingSyncList + Send + Sync>>,
3536
}
3637

@@ -130,8 +131,8 @@ impl SlidingSyncListBuilder {
130131
}
131132

132133
/// Set the limit of regular events to fetch for the timeline.
133-
pub fn timeline_limit<U: Into<UInt>>(mut self, timeline_limit: U) -> Self {
134-
self.timeline_limit = Some(timeline_limit.into());
134+
pub fn timeline_limit(mut self, timeline_limit: Bound) -> Self {
135+
self.timeline_limit = Some(timeline_limit);
135136
self
136137
}
137138

@@ -143,24 +144,24 @@ impl SlidingSyncListBuilder {
143144
}
144145

145146
/// Set the ranges to fetch.
146-
pub fn ranges<U: Into<UInt>>(mut self, range: Vec<(U, U)>) -> Self {
147-
self.ranges = range.into_iter().map(|(a, b)| (a.into(), b.into())).collect();
147+
pub fn ranges(mut self, ranges: Vec<RangeInclusive<Bound>>) -> Self {
148+
self.ranges = ranges;
148149
self
149150
}
150151

151-
/// Set a single range fetch.
152-
pub fn set_range<U: Into<UInt>>(mut self, from: U, to: U) -> Self {
153-
self.ranges = vec![(from.into(), to.into())];
152+
/// Set a single range to fetch.
153+
pub fn set_range(mut self, range: RangeInclusive<Bound>) -> Self {
154+
self.ranges = vec![range];
154155
self
155156
}
156157

157158
/// Set the ranges to fetch.
158-
pub fn add_range<U: Into<UInt>>(mut self, from: U, to: U) -> Self {
159-
self.ranges.push((from.into(), to.into()));
159+
pub fn add_range(mut self, range: RangeInclusive<Bound>) -> Self {
160+
self.ranges.push(range);
160161
self
161162
}
162163

163-
/// Set the ranges to fetch.
164+
/// Reset the ranges to fetch.
164165
pub fn reset_ranges(mut self) -> Self {
165166
self.ranges.clear();
166167
self

0 commit comments

Comments
 (0)