Skip to content

Commit 43f2f60

Browse files
committed
fix(sdk): Remove Deref impl for SlidingSyncRoom.
This implementation is wrong in the sense of its semantics is not about dereferencing a thin pointer to something, but just to give access to one specific field of the entire structure. That's not how `Deref` is supposed to be used. Moreover, it creates conflict between the `SlidingSyncRoom.timeline` field, and `SlidingSyncRoom.inner.timeline` field, which both exist, but not for the same purposes. It creates confusion in the code. Finally, it's better to expose proper getters to the outside world, so that we control _and_ test _and_ know exactly what API we provide.
1 parent ae21a56 commit 43f2f60

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,23 @@ impl SlidingSyncRoom {
130130
}
131131

132132
pub fn is_dm(&self) -> Option<bool> {
133-
self.inner.is_dm
133+
self.inner.is_dm()
134134
}
135135

136136
pub fn is_initial(&self) -> Option<bool> {
137-
self.inner.initial
137+
self.inner.is_an_initial_response()
138138
}
139+
139140
pub fn is_loading_more(&self) -> bool {
140141
self.inner.is_loading_more()
141142
}
142143

143144
pub fn has_unread_notifications(&self) -> bool {
144-
!self.inner.unread_notifications.is_empty()
145+
self.inner.has_unread_notifications()
145146
}
146147

147148
pub fn unread_notifications(&self) -> Arc<UnreadNotificationsCount> {
148-
Arc::new(self.inner.unread_notifications.clone().into())
149+
Arc::new(self.inner.unread_notifications().clone().into())
149150
}
150151

151152
pub fn full_room(&self) -> Option<Arc<Room>> {

crates/matrix-sdk/src/sliding_sync.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use std::{
1717
collections::BTreeMap,
1818
fmt::Debug,
19-
ops::Deref,
19+
ops::{Deref, Not},
2020
sync::{
2121
atomic::{AtomicBool, AtomicU8, Ordering},
2222
Arc, Mutex,
@@ -34,9 +34,12 @@ use matrix_sdk_base::{deserialized_responses::SyncTimelineEvent, sync::SyncRespo
3434
use ruma::{
3535
api::client::{
3636
error::ErrorKind,
37-
sync::sync_events::v4::{
38-
self, AccountDataConfig, E2EEConfig, ExtensionsConfig, ReceiptConfig, ToDeviceConfig,
39-
TypingConfig,
37+
sync::sync_events::{
38+
v4::{
39+
self, AccountDataConfig, E2EEConfig, ExtensionsConfig, ReceiptConfig,
40+
ToDeviceConfig, TypingConfig,
41+
},
42+
UnreadNotificationsCount,
4043
},
4144
},
4245
assign,
@@ -275,6 +278,26 @@ impl SlidingSyncRoom {
275278
self.inner.name.as_deref()
276279
}
277280

281+
/// Is this a direct message?
282+
pub fn is_dm(&self) -> Option<bool> {
283+
self.inner.is_dm
284+
}
285+
286+
/// Was this an initial response.
287+
pub fn is_an_initial_response(&self) -> Option<bool> {
288+
self.inner.initial
289+
}
290+
291+
/// Is there any unread notifications?
292+
pub fn has_unread_notifications(&self) -> bool {
293+
self.inner.unread_notifications.is_empty().not()
294+
}
295+
296+
/// Get unread notifications.
297+
pub fn unread_notifications(&self) -> &UnreadNotificationsCount {
298+
&self.inner.unread_notifications
299+
}
300+
278301
fn update(&mut self, room_data: &v4::SlidingSyncRoom, timeline: Vec<SyncTimelineEvent>) {
279302
let v4::SlidingSyncRoom {
280303
name,
@@ -331,13 +354,6 @@ impl SlidingSyncRoom {
331354
}
332355
}
333356

334-
impl Deref for SlidingSyncRoom {
335-
type Target = v4::SlidingSyncRoom;
336-
fn deref(&self) -> &Self::Target {
337-
&self.inner
338-
}
339-
}
340-
341357
type ViewState = Mutable<SlidingSyncState>;
342358
type SyncMode = Mutable<SlidingSyncMode>;
343359
type StringState = Mutable<Option<String>>;

0 commit comments

Comments
 (0)