From 3b4ef219664bed8dc020c0b85fda8642afeb9aad Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Thu, 29 Feb 2024 12:01:08 +0100 Subject: [PATCH] !drop --- crates/matrix-sdk/src/event_cache/store.rs | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/crates/matrix-sdk/src/event_cache/store.rs b/crates/matrix-sdk/src/event_cache/store.rs index 031528d30c4..82fdbc41589 100644 --- a/crates/matrix-sdk/src/event_cache/store.rs +++ b/crates/matrix-sdk/src/event_cache/store.rs @@ -500,6 +500,32 @@ pub mod experimental { } } + impl Drop for LinkedChunk { + fn drop(&mut self) { + // Take the latest chunk. + let mut current_chunk_ptr = self.last.or(Some(self.first)); + + // As long as we have another chunk… + while let Some(chunk_ptr) = current_chunk_ptr { + // Disconnect the chunk by updating `previous_chunk.next` pointer. + let previous_ptr = unsafe { chunk_ptr.as_ref() }.previous; + + if let Some(mut previous_ptr) = previous_ptr { + unsafe { previous_ptr.as_mut() }.next = None; + } + + // Re-box the chunk, and let Rust does its job. + let _chunk_boxed = unsafe { Box::from_raw(chunk_ptr.as_ptr()) }; + + // Update the `current_chunk_ptr`. + current_chunk_ptr = previous_ptr; + } + + // At this step, all chunks have been dropped, including + // `self.first`. + } + } + /// The position of a chunk in a [`LinkedChunk`]. /// /// 0 represents the latest chunk. @@ -868,6 +894,16 @@ pub mod experimental { } } + #[test] + fn test_empty() { + let events = Events::::new(); + + assert_eq!(events.len(), 0); + + // This test also ensures that `Drop` for `LinkedChunk` works when + // there is only one chunk. + } + #[test] fn test_push_events() { let mut events = Events::::new();