From b4b2ee471654bbdbd923217fa26e74fe174238f7 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 22 May 2024 10:24:36 +0200 Subject: [PATCH] chore(sdk): Move code inside the same module. --- .../src/event_cache/linked_chunk/as_vector.rs | 3 +- .../src/event_cache/linked_chunk/updates.rs | 90 +++++++++---------- 2 files changed, 46 insertions(+), 47 deletions(-) diff --git a/crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs b/crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs index c48f8b35f..4d50815c5 100644 --- a/crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs +++ b/crates/matrix-sdk/src/event_cache/linked_chunk/as_vector.rs @@ -62,9 +62,8 @@ impl AsVector { Item: Clone, { let mut updates = self.updates.write().unwrap(); - let updates = updates.take_with_token(self.token); - self.mapper.map(updates) + self.mapper.map(updates.take_with_token(self.token)) } } diff --git a/crates/matrix-sdk/src/event_cache/linked_chunk/updates.rs b/crates/matrix-sdk/src/event_cache/linked_chunk/updates.rs index 2ca602aa2..61fa283d5 100644 --- a/crates/matrix-sdk/src/event_cache/linked_chunk/updates.rs +++ b/crates/matrix-sdk/src/event_cache/linked_chunk/updates.rs @@ -123,6 +123,51 @@ pub struct Updates { pub(super) inner: Arc>>, } +impl Updates { + /// Create a new [`Self`]. + pub(super) fn new() -> Self { + Self { inner: Arc::new(RwLock::new(UpdatesInner::new())) } + } + + /// Push a new update. + pub(super) fn push(&mut self, update: Update) { + self.inner.write().unwrap().push(update); + } + + /// Take new updates. + /// + /// Updates that have been taken will not be read again. + pub(super) fn take(&mut self) -> Vec> + where + Item: Clone, + Gap: Clone, + { + self.inner.write().unwrap().take().to_owned() + } + + /// Subscribe to updates by using a [`Stream`]. + pub(super) fn subscribe(&mut self) -> UpdatesSubscriber { + // A subscriber is a new update reader, it needs its own token. + let token = self.new_reader_token(); + + UpdatesSubscriber::new(Arc::downgrade(&self.inner), token) + } + + /// Generate a new [`ReaderToken`]. + pub(super) fn new_reader_token(&mut self) -> ReaderToken { + let mut inner = self.inner.write().unwrap(); + + // Add 1 before reading the `last_token`, in this particular order, because the + // 0 token is reserved by `MAIN_READER_TOKEN`. + inner.last_token += 1; + let last_token = inner.last_token; + + inner.last_index_per_reader.insert(last_token, 0); + + last_token + } +} + /// A token used to represent readers that read the updates in /// [`UpdatesInner`]. pub(super) type ReaderToken = usize; @@ -249,51 +294,6 @@ impl UpdatesInner { } } -impl Updates { - /// Create a new [`Self`]. - pub(super) fn new() -> Self { - Self { inner: Arc::new(RwLock::new(UpdatesInner::new())) } - } - - /// Push a new update. - pub(super) fn push(&mut self, update: Update) { - self.inner.write().unwrap().push(update); - } - - /// Take new updates. - /// - /// Updates that have been taken will not be read again. - pub(super) fn take(&mut self) -> Vec> - where - Item: Clone, - Gap: Clone, - { - self.inner.write().unwrap().take().to_owned() - } - - /// Subscribe to updates by using a [`Stream`]. - pub(super) fn subscribe(&mut self) -> UpdatesSubscriber { - // A subscriber is a new update reader, it needs its own token. - let token = self.new_reader_token(); - - UpdatesSubscriber::new(Arc::downgrade(&self.inner), token) - } - - /// Generate a new [`ReaderToken`]. - pub(super) fn new_reader_token(&mut self) -> ReaderToken { - let mut inner = self.inner.write().unwrap(); - - // Add 1 before reading the `last_token`, in this particular order, because the - // 0 token is reserved by `MAIN_READER_TOKEN`. - inner.last_token += 1; - let last_token = inner.last_token; - - inner.last_index_per_reader.insert(last_token, 0); - - last_token - } -} - /// A subscriber to [`Updates`]. It is helpful to receive updates via a /// [`Stream`]. pub(super) struct UpdatesSubscriber {