From 5dea64b0efd755d011675f207ef1a770bd12e5ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Wed, 19 Nov 2025 13:40:44 +0100 Subject: [PATCH] feat(linked-chunk): Add method to get the items of an Update This patch adds a convenience function for the Update enum. If one only cares about the items contained in the Update, then they can chose to use this method to extract them out of the enum. --- .../src/linked_chunk/updates.rs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/crates/matrix-sdk-common/src/linked_chunk/updates.rs b/crates/matrix-sdk-common/src/linked_chunk/updates.rs index 8dc915450..e13cd8714 100644 --- a/crates/matrix-sdk-common/src/linked_chunk/updates.rs +++ b/crates/matrix-sdk-common/src/linked_chunk/updates.rs @@ -114,6 +114,31 @@ pub enum Update { Clear, } +impl Update { + /// Get the items from the [`Update`] if any. + /// + /// This function is useful if you only care about the items from the + /// [`Update`] and not what kind of update it was and where the items + /// should be placed. + /// + /// [`Update`] variants which don't contain any items will return an empty + /// [`Vec`]. + pub fn into_items(self) -> Vec { + match self { + Update::NewItemsChunk { .. } + | Update::NewGapChunk { .. } + | Update::RemoveChunk(_) + | Update::RemoveItem { .. } + | Update::DetachLastItems { .. } + | Update::StartReattachItems + | Update::EndReattachItems + | Update::Clear => vec![], + Update::PushItems { items, .. } => items, + Update::ReplaceItem { item, .. } => vec![item], + } + } +} + /// A collection of [`Update`]s that can be observed. /// /// Get a value for this type with [`LinkedChunk::updates`]. @@ -408,6 +433,7 @@ mod tests { use futures_util::pin_mut; use super::{super::LinkedChunk, ChunkIdentifier, Position, UpdatesInner}; + use crate::linked_chunk::Update; #[test] fn test_updates_take_and_garbage_collector() { @@ -926,4 +952,23 @@ mod tests { drop(linked_chunk); assert_matches!(updates_subscriber1.as_mut().poll_next(&mut context1), Poll::Ready(None)); } + + #[test] + fn test_update_into_items() { + let updates: Update<_, u32> = + Update::PushItems { at: Position::new(ChunkIdentifier(0), 0), items: vec![1, 2, 3] }; + + assert_eq!(updates.into_items(), vec![1, 2, 3]); + + let updates: Update = Update::Clear; + assert!(updates.into_items().is_empty()); + + let updates: Update = + Update::RemoveItem { at: Position::new(ChunkIdentifier(0), 0) }; + assert!(updates.into_items().is_empty()); + + let updates: Update = + Update::ReplaceItem { at: Position::new(ChunkIdentifier(0), 0), item: 42 }; + assert_eq!(updates.into_items(), vec![42]); + } }