Skip to content

Commit d2b7dc6

Browse files
pixlwaveHywan
authored andcommitted
ffi: Export TimelineDiff as uniffi:Enum to match RoomDirectorySearchEntryUpdate & RoomListEntriesUpdate.
The difference in API shape has been weird for long enough.
1 parent 1089a25 commit d2b7dc6

File tree

4 files changed

+22
-83
lines changed

4 files changed

+22
-83
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindings/matrix-sdk-ffi/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ All notable changes to this project will be documented in this file.
3232

3333
### Breaking changes:
3434

35+
- `TimelineDiff` is now exported as a true `uniffi::Enum` instead of the weird `uniffi::Object` hybrid. This matches
36+
both `RoomDirectorySearchEntryUpdate` and `RoomListEntriesUpdate` and can be used in the same way.
37+
([#5474](https://github.com/matrix-org/matrix-rust-sdk/pull/5474))
3538
- The `creator` field of `RoomInfo` has been renamed to `creators` and can now contain a list of
3639
user IDs, to reflect that a room can now have several creators, as introduced in room version 12.
3740
([#5436](https://github.com/matrix-org/matrix-rust-sdk/pull/5436))

bindings/matrix-sdk-ffi/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ sentry = ["dep:sentry", "dep:sentry-tracing"]
3838

3939
[dependencies]
4040
anyhow.workspace = true
41-
as_variant.workspace = true
4241
extension-trait = "1.0.1"
4342
eyeball-im.workspace = true
4443
futures-util.workspace = true

bindings/matrix-sdk-ffi/src/timeline/mod.rs

Lines changed: 19 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use std::{collections::HashMap, fmt::Write as _, fs, panic, sync::Arc};
1616

1717
use anyhow::{Context, Result};
18-
use as_variant::as_variant;
1918
use eyeball_im::VectorDiff;
2019
use futures_util::pin_mut;
2120
use matrix_sdk::{
@@ -64,7 +63,6 @@ use crate::{
6463
client::ProgressWatcher,
6564
error::{ClientError, RoomError},
6665
event::EventOrTransactionId,
67-
helpers::unwrap_or_clone_arc,
6866
ruma::{
6967
AssetType, AudioInfo, FileInfo, FormattedBody, ImageInfo, Mentions, PollKind,
7068
ThumbnailInfo, VideoInfo,
@@ -252,17 +250,14 @@ impl Timeline {
252250
// handled by the caller. See #3535 for details.
253251

254252
// First, pass all the items as a reset update.
255-
listener.on_update(vec![Arc::new(TimelineDiff::new(VectorDiff::Reset {
256-
values: timeline_items,
257-
}))]);
253+
listener.on_update(vec![TimelineDiff::new(VectorDiff::Reset { values: timeline_items })]);
258254

259255
Arc::new(TaskHandle::new(get_runtime_handle().spawn(async move {
260256
pin_mut!(timeline_stream);
261257

262258
// Then forward new items.
263259
while let Some(diffs) = timeline_stream.next().await {
264-
listener
265-
.on_update(diffs.into_iter().map(|d| Arc::new(TimelineDiff::new(d))).collect());
260+
listener.on_update(diffs.into_iter().map(TimelineDiff::new).collect());
266261
}
267262
})))
268263
}
@@ -790,26 +785,26 @@ pub enum FocusEventError {
790785

791786
#[matrix_sdk_ffi_macros::export(callback_interface)]
792787
pub trait TimelineListener: SyncOutsideWasm + SendOutsideWasm {
793-
fn on_update(&self, diff: Vec<Arc<TimelineDiff>>);
788+
fn on_update(&self, diff: Vec<TimelineDiff>);
794789
}
795790

796791
#[matrix_sdk_ffi_macros::export(callback_interface)]
797792
pub trait PaginationStatusListener: SyncOutsideWasm + SendOutsideWasm {
798793
fn on_update(&self, status: RoomPaginationStatus);
799794
}
800795

801-
#[derive(Clone, uniffi::Object)]
796+
#[derive(Clone, uniffi::Enum)]
802797
pub enum TimelineDiff {
803798
Append { values: Vec<Arc<TimelineItem>> },
804799
Clear,
805800
PushFront { value: Arc<TimelineItem> },
806801
PushBack { value: Arc<TimelineItem> },
807802
PopFront,
808803
PopBack,
809-
Insert { index: usize, value: Arc<TimelineItem> },
810-
Set { index: usize, value: Arc<TimelineItem> },
811-
Remove { index: usize },
812-
Truncate { length: usize },
804+
Insert { index: u32, value: Arc<TimelineItem> },
805+
Set { index: u32, value: Arc<TimelineItem> },
806+
Remove { index: u32 },
807+
Truncate { length: u32 },
813808
Reset { values: Vec<Arc<TimelineItem>> },
814809
}
815810

@@ -820,14 +815,18 @@ impl TimelineDiff {
820815
Self::Append { values: values.into_iter().map(TimelineItem::from_arc).collect() }
821816
}
822817
VectorDiff::Clear => Self::Clear,
823-
VectorDiff::Insert { index, value } => {
824-
Self::Insert { index, value: TimelineItem::from_arc(value) }
825-
}
826-
VectorDiff::Set { index, value } => {
827-
Self::Set { index, value: TimelineItem::from_arc(value) }
818+
VectorDiff::Insert { index, value } => Self::Insert {
819+
index: u32::try_from(index).unwrap(),
820+
value: TimelineItem::from_arc(value),
821+
},
822+
VectorDiff::Set { index, value } => Self::Set {
823+
index: u32::try_from(index).unwrap(),
824+
value: TimelineItem::from_arc(value),
825+
},
826+
VectorDiff::Truncate { length } => {
827+
Self::Truncate { length: u32::try_from(length).unwrap() }
828828
}
829-
VectorDiff::Truncate { length } => Self::Truncate { length },
830-
VectorDiff::Remove { index } => Self::Remove { index },
829+
VectorDiff::Remove { index } => Self::Remove { index: u32::try_from(index).unwrap() },
831830
VectorDiff::PushBack { value } => {
832831
Self::PushBack { value: TimelineItem::from_arc(value) }
833832
}
@@ -843,67 +842,6 @@ impl TimelineDiff {
843842
}
844843
}
845844

846-
#[matrix_sdk_ffi_macros::export]
847-
impl TimelineDiff {
848-
pub fn change(&self) -> TimelineChange {
849-
match self {
850-
Self::Append { .. } => TimelineChange::Append,
851-
Self::Insert { .. } => TimelineChange::Insert,
852-
Self::Set { .. } => TimelineChange::Set,
853-
Self::Remove { .. } => TimelineChange::Remove,
854-
Self::PushBack { .. } => TimelineChange::PushBack,
855-
Self::PushFront { .. } => TimelineChange::PushFront,
856-
Self::PopBack => TimelineChange::PopBack,
857-
Self::PopFront => TimelineChange::PopFront,
858-
Self::Clear => TimelineChange::Clear,
859-
Self::Truncate { .. } => TimelineChange::Truncate,
860-
Self::Reset { .. } => TimelineChange::Reset,
861-
}
862-
}
863-
864-
pub fn append(self: Arc<Self>) -> Option<Vec<Arc<TimelineItem>>> {
865-
let this = unwrap_or_clone_arc(self);
866-
as_variant!(this, Self::Append { values } => values)
867-
}
868-
869-
pub fn insert(self: Arc<Self>) -> Option<InsertData> {
870-
let this = unwrap_or_clone_arc(self);
871-
as_variant!(this, Self::Insert { index, value } => {
872-
InsertData { index: index.try_into().unwrap(), item: value }
873-
})
874-
}
875-
876-
pub fn set(self: Arc<Self>) -> Option<SetData> {
877-
let this = unwrap_or_clone_arc(self);
878-
as_variant!(this, Self::Set { index, value } => {
879-
SetData { index: index.try_into().unwrap(), item: value }
880-
})
881-
}
882-
883-
pub fn remove(&self) -> Option<u32> {
884-
as_variant!(self, Self::Remove { index } => (*index).try_into().unwrap())
885-
}
886-
887-
pub fn push_back(self: Arc<Self>) -> Option<Arc<TimelineItem>> {
888-
let this = unwrap_or_clone_arc(self);
889-
as_variant!(this, Self::PushBack { value } => value)
890-
}
891-
892-
pub fn push_front(self: Arc<Self>) -> Option<Arc<TimelineItem>> {
893-
let this = unwrap_or_clone_arc(self);
894-
as_variant!(this, Self::PushFront { value } => value)
895-
}
896-
897-
pub fn reset(self: Arc<Self>) -> Option<Vec<Arc<TimelineItem>>> {
898-
let this = unwrap_or_clone_arc(self);
899-
as_variant!(this, Self::Reset { values } => values)
900-
}
901-
902-
pub fn truncate(&self) -> Option<u32> {
903-
as_variant!(self, Self::Truncate { length } => (*length).try_into().unwrap())
904-
}
905-
}
906-
907845
#[derive(uniffi::Record)]
908846
pub struct InsertData {
909847
pub index: u32,

0 commit comments

Comments
 (0)